As a WordPress site developer or webmaster, a terrifying situation is when your site is down. You not only lose incoming traffic but your site’s ranking and reputation are also negatively affected.
One of the errors that cause your site to temporarily be inaccessible is the database connection error. You might have run into this error when you find yourself entering the URL of your site and nothing appears other than this error message: “Error establishing a database connection.”
If you’ve ever encountered this error or are in the midst of finding a solution for it then don’t fret! In this tutorial, we will show you how to fix this error very easily – the right way. By following the steps carefully, you’ll be able to retrieve your site and get back all of your traffic and subscribers in no time.
Understanding the Error Establishing a Database Connection in WordPress
WordPress stores every piece of information about your website in a MySQL database. By this, I mean post data, page data, plugin settings, login credentials, widget layout, and even color settings all are organized and stored in a MySQL database.
WordPress then uses PHP to connect to the database and retrieve this information anytime a user visits a page on the website.
If for whatever reason, WordPress can’t access your site’s database or it isn’t working properly, the result is an Error Establishing a Database Connection message.
Having said that, there can be a handful of reasons why the error occurs. Some of the more common ones:
- Incorrect login credentials. WordPress needs a specific login and password to access the database. If those have changed for any reason then WordPress will no longer be able to fetch anything from the database.
- Corrupted WordPress files. The core WordPress files can get corrupted as a result of a failed update, among others. This can include things like failed updates of the plugins, themes, and even the main WordPress software updates.
- Corrupted database. This might be a result of a rogue plugin messing up the database internally, a hacker’s attack, a theme failing, and many more.
- The database server is down. In some cases, the web server – the server that stores your website files – is run on a different machine than the database. And that separate database server can simply be down for whatever reason.
- Too much traffic. Even though this is a good problem to have, it’s still a problem. Specifically, your database might be unresponsive due to a high spike in traffic. For example, maybe one of your posts went viral and now everyone and their dog tries to access your site? Hence the server is not able to handle part of those visits.
Incorrect login credentials are the most common cause of the error, but any of these could be the culprit. For that reason, fixing the error can involve some troubleshooting.
How to Fix Error Establishing a Database Connection Error
Now that we know what the error is and what causes it, let’s make the error go away and restore your blog/ website. There are a handful of steps to do it but the best part is when you get your site working you can stop there.
Step 1: Check Your WordPress Database Credentials
Most of these cases are solved when you input the right WordPress Database credentials.
Head over to the wp-config.php file in the root directory of your WordPress installation using an FTP client or cPanel if available.
It shows the name of the database as “DB_NAME”, the login username as “DB_USER”, the login password as “DB_PASSWORD”, and the database host as “DB_HOST”. After going through all of this information, do make sure everything is correct as WordPress won’t be able to connect to the database if any of these values are incorrect.
If the error persists after you set the correct credentials, it means there is still a problem, thus continue to the next step.
Step 2: Check Your Database Host Information
In the event your credentials are correct and you still see “Error Establishing a Database Connection”, the next thing to check is your database host information.
Still in your wp-config.php, check the database host set as “DB_HOST”. By default, it is set to localhost meaning it is on the same server as your WordPress installation
However, some managed WordPress hosting companies use separate servers to host databases. In that case, your database host information will not be localhost.
You will need to contact your WordPress hosting provider to confirm your database hosting information.
Step 3: Repair Your WordPress Database
If you’re still receiving the error message, you can try repairing the database to fix possible corruption. We recommend using the built-in WordPress database repair tool for this.
To access it, open up your wp-config.php file and add the following code at the end:
define('WP_ALLOW_REPAIR', true);
Next, open a new browser tab and navigate to https://yoursitename.com/wp-admin/maint/repair.php, replacing “yoursitename” with your website’s actual domain. This will bring up the database repair tool.
Click on Repair Database and let it do its thing. You can choose Repair and Optimize Database if you like, but it takes considerably longer. Either way, when the tool is finished, load your website again and check for the error. If it’s gone, then you’ll know a corrupted database was the cause.
However, if you’re still seeing the error message, head to the next step. Before you do, make sure to head back into wp-config.php and delete the code you added to turn on the tool. If you leave it there, someone with ill intentions could gain access to your site easily.
Step 4: Fix Corrupted Files
If completely troubleshooting your database doesn’t even work, the last thing you can try is to fix any corrupted files in WordPress. Many users who failed to identify the underlying problem completely wiped all of their files and the error went away. So that’s exactly what we are going to do now as the error could be because of corrupted WordPress files.
That said, since we are talking about the main system files, you need to be very careful. The tiniest of mistakes can wipe away your entire site. Before we proceed, it’s best to backup all of your data either manually or by using a plugin. The next thing we need is new, fresh WordPress files to replace the old corrupted files that are causing problems for us.
- Download a fresh copy of the WordPress CMS from WordPress.org.
- Unzip it in your drive and delete the whole wp-contents folder and the wp-config file so there’s no chance of overwriting your current wp-config file. Plus all the hard work you’ve put into setting up themes and plugins up till now won’t be lost.
- After deleting the infected or corrupt files, paste the rest of the files into your WordPress root folder using the File Manager or an FTP client.
This will replace the corrupted files or the files which are causing the error. By following these simple steps you’ll be able to easily fix the Error Establishing a Database Connection error. Reload the page once you’re done and you are all set to go.
If you’ve followed the entire tutorial in order (and correctly) and the problem still persists, the next logical step would be to contact your web hosting service’s customer support representatives because the error may occur at the server end. Remember to tell the customer support representative about the measures you took to resolve the problem.
Final Thoughts
This Error Establishing a Database Connection error is one of the most frustrating errors you can encounter with your WordPress website. There are several different possible root causes for this error making it difficult to identify and resolve it.
We covered some of the most probable reasons behind this error and walked you through three easy-to-follow methods to fix it. If you follow the steps outlined in this tutorial extra carefully you’ll have your site up and running in no time.