Howdy readers! Today I am going to help WordPress website owners to fix another issue. That is about the message saying “Error establishing a database connection.”
Only those words are displayed. No design, no WordPress elements.
But don’t worry people! First I can assure you that this is a fixable issue. So you don’t have to worry about any permanent damage. And also, this error is quite common.
Understanding the problem
As the message says; it’s an error establishing a database connection. Let’s learn about the error from the beginning. First of all WordPress uses two main parts of technology to function. Those are PHP and MySQL.
- PHP is a programming language in which the core WordPress files are written.
- MySQL is a database technology. WordPress uses the database to store all of your website content including posts, pages, and even smaller elements like the site title, widget layout, color settings likewise. Technically MySQL has all the information about your website.
So in a nutshell, WordPress uses PHP commands to connect to the MySQL database, get the needed information, and after that display everything on the screen.
The error occurs in between these processes. Basically when WordPress cannot access the information in the database via PHP commands. Then WordPress won’t be able to do anything, displays “Error establishing a database connection.”
Furthermore there can be many reasons for the error. The most common causes are,
- Incorrect login credentials You might already know that WordPress requires a specific login and password to access the database. WordPress won’t be able to get anything from the database if the login and password are changed.
- Corrupted WordPress files This can be a result of a failed update of a plugin, theme, or failure in the main WordPress software update.
- Corrupted database This may be due to a rogue plugin messing up the database internally, a hacker’s attack, a theme failing, etc.
- The database server is down The database server is the server that stores your website files, which runs on a different machine other than the database. And the database server can be down due to various reasons.
- Too much traffic Sounds relieving, right? Still a problem however. And your database might be not responding due to high traffic. There may be many visits that your server cannot handle.
How to fix “Error Establishing a Database Connection”
Now, let’s look at the solutions. For your benefit I am going to start with a method that is most likely to fix your issue quickly. We’ll proceed step by step.
Step 1: Get in Touch with your Web Host Provider
As in these types of acute problems the fault can be at the side of the web host. Usually all trusted hosts provide some form of support commonly through live chat. So you can contact them via live chat, tell them about your problem and clarify the following.
- Whether the database server is up and running with no problem
- Whether there isn’t any spike in activity or traffic on the server at this moment
In most cases, after you have explained the error, they will tell you the exact cause for the error or even fix it for you.
The most important thing is that they will tell you if your database is alright and about any suspicious activities on the server. If the server is operational and there are no spikes in traffic the problem is not on their side.
Then let’s move onto the next step.
Step 2: Check whether your Plugin or Theme files are corrupted
Corruption of WordPress files can happen due to many reasons. Like updating plugins or themes, hooking the site to an external service, hand-editing some files, etc.
If you can remember doing any of the above recently, that might be the reason. If you haven’t done anything like that, just skip this step.
- First, connect to your host via FTP. You can use FileZilla. You must need a login and password to connect to the FTP account. So that you can get them easily by going through the very first emails that you received from your web host when you first signed up. Or else you can ask those from the live chat representative of the host.
- Then connect to the server and navigate to the WordPress root folder. Often that is named as “public_html” or “www”. Already you might be in the root folder when you establish the FTP connection.
You will know for sure that you are in the root folder if you are seeing subfolders like “wp-content” and “wp-content” - Then go into wp-content and rename the plugins folder to something else.
- After that, go back to the site and check whether the error is still there.
- If the error is still there, the troublemaker is a plugin. Then go back to FTP, rename the plugin folder back to plugins.
- Enter the plugin folder, and start renaming the plugins one at a time. You can simply add an underscore at the end: “_”
- And check for the error after renaming each plugin. When you identify the culprit plugin, just delete it or check online solutions.
- If you are experiencing the error after renaming the plugins folder. Perform the same procedure with the themes folder to identify the troublemaker.
If that doesn’t help, just rename both folders back to plugins and themes and move on to the next step.
Step 3: Check if your Database hasn’t been corrupted
As WordPress database is somewhat complicated, even very small cogs need to work together perfectly to avoid problems. Sometimes those cogs can go out of place causing the Error Establishing a Database Connection.
- The very first thing to do is to go to your WordPress dashboard YOURSITE.com/wp-admin
- If the error pops up, then skip this step.
- Then you have to pay close attention to what WordPress suggests you do. In the latest versions of WordPress with expanded problem-solving features, there might be some advice like in this image.
- And also there might be a message which starts from “One or more database tables are unavailable … ” This indicates that you need to repair the database. Fortunately this is very easy.
- First of all you have to connect to your site using FTP.
- Then go to WordPress root and download the wp-config.php file to your desktop.
Edit this file in notepad, add one extra line at the end.define('WP_ALLOW_REPAIR', true);
Once it is done, upload the file back to your site via FTP, overwriting the old version.
- Then go to YOURSITE.com/wp-admin/maint/repair.php. You’ll see this.
- Click any button from those two to initiate automatic repair. Once the repair is completed, go to your site again and look for the error.
You are lucky if the error is gone. But make sure to revert your wp-config.php to its previous state. Delete the line that you added and reupload the file.
If that doesn’t help you either, let’s look at the fourth step.
Step 4: Check your Database Connection Credentials
I mentioned previously that WordPress requires a specific username and password to connect to the database. Usually they cannot change on their own. But you could face some abnormal situations after making changes to your site files, the hosting environment, or anything. That is because the access credentials change. Furthermore, bad access credentials will result in “Error Establishing a Database connection”
The method to fix the issue is via the wp-config.php file. We are going to take these lines specifically as they contain the details that allow WordPress to connect to the database.
define('DB_NAME', 'some_db');
define('DB_USER', 'some_username');
define('DB_PASSWORD', 'some_pass');
define('DB_HOST', 'localhost');
If any character of these lines is wrong, the connection won’t establish.
-
- As mentioned previously, copy those four lines from the “wp-config.php” file, and paste them in the clipboard.
- Then contact your web host support and send them the lines asking whether they are correct or not. The representative will check and provide the correct details if they were wrong.
- Then you just have to get those new credentials and paste them in your wp-config.php file where the old ones used to be.
define('DB_NAME', 'some_db'); /* this is the name of your WordPress database */
define('DB_USER', 'some_username');
define('DB_PASSWORD', 'some_pass');
define('DB_HOST', 'localhost'); /* this is the web address of your database server */
- Then reupload the file using FTP. After that, go back to your site and look for the error.
If it is still there, go to the final step.
Step 5: Restore the Default WordPress files
Another reason for a corrupted core WordPress files is a hacker’s attack or else you modified some files accidentally, maybe a rogue plugin did. Whatever that is, you can restore the default, native files of WordPress. The simplest way is to download a clean version of WordPress from WordPress.org and copy that over the existing one.
- Go here, and download the latest version to your desktop.
- Then unpack. After that browse through the unpacked archive and delete the wp-content folder.
- As the next step, connect to your site via FTP.
- Then go to the main WordPress root and upload the new WordPress files overwriting the existing ones.
- When the upload is finished, go back to the site to see the result.
Unfortunately this is the last option. Still, you are experiencing the error we strongly recommend you to contact your web host support again and ask for help.
How to avoid this error in the future
As there isn’t a 100% fail-proof method to protect your site from these kinds of errors. Moreover there are two main ailments that you should have for your safety.
- Quality hosting. Please make sure to work only with the hosts that have optimized their platforms to work with WordPress. So that you will always be in safe hands. You will also prevent many common problems. Here are our recommendations.
- Have backups. Make sure to install a simple backup plugin like UpdraftPlus which works automatically. And always have a working WordPress backup so you can restore everything when you need it.