Error Establishing a Database Connection – Are you seeing the ‘Error establishing a database connection’ notice on your WordPress website? It is a fatal error that makes your WordPress website inaccessible to the users.
This error occurs when WordPress is unable to make a connection to the database. A number of things can affect your WordPress database connection which makes it a bit difficult for beginners to troubleshoot.
In this article, we will show you how to easily fix the error establishing a database connection in WordPress.
First off, stop panicking!
We know that you’re probably a bit scared right now, but let us assure you that this problem can be fixed and any permanent damage can be prevented. In fact, the error establishing a database connection is quite common.
Some common reasons why this error may show up:
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 try to access your site? Hence the server is not able to handle part of those visits.
How To Fix “Error Establishing a Database Connection”
Step 1: Get In Touch With Your Web Host Provider
Start by contacting that live chat, say that you’re experiencing an “error establishing a database connection” (paste the phrase), and ask:
- Whether the database server is up and running with no problems, and
- if there isn’t any spike in activity or traffic on the server right now.
Now, if the fault is not on their end (the server is operational and there are no spikes in traffic), and they don’t want to help you, proceed to step 2:
Step 2: Check If Your Plugin or Theme Files Haven’t Been Corrupted
Your WordPress files might get corrupted as a result of a couple of misfortunate events.
For instance, maybe you’ve updated a plugin or a theme, and the whole thing collapsed? Maybe you hooked the site up to an external service? Maybe you manually edited some of the files?
Here’s what you have to do:
- First: connect to your host via FTP. You can do this with a free tool called FileZilla.
In order to connect, you need a login and a password to your FTP account. If you don’t know what/where those are, the easiest way to get them is to search through the initial emails that you got from your web host when you first signed up. Alternatively, you can ask the live chat support representative to give you those details.
Once you have these, connect to the server and navigate to the WordPress root folder. It’s usually called “public_html” or “www” or you might be in it already once you establish the FTP connection.
The rule is that if you can see sub-folders like “wp-content” and “wp-admin” then you’re in the WordPress root. Example:
- Second: Go into “wp-content” and rename the “plugins” folder to something else.
- Third: Go back to your site and see if the error is still there.
If not, you now know that it’s been caused by some plugin. Go back to FTP, rename the folder back to “plugins”. Go in it, and change the folder names of the individual plugins, one by one. The easiest way to do this is to add an underscore at the end: “_”
Keep coming back to your site to see when the error sign goes away. Once you’ve narrowed it down to one specific plugin, you have found the troublemaker. Either delete it or go online to see if the solution has been documented.
- If the error is still there even after renaming the “plugins” folder, go through the exact same process with the “themes” folder.
- If that doesn’t help, rename both folders back to “plugins” and “themes”. The error is caused by something else.
Step 3: Check If Your Database Hasn’t Been Corrupted
The WordPress database is a fairly complex thing, and there are a lot of small cogs that need to stay working together perfectly to avoid trouble. At times, something goes out of place and that’s when you see the error establishing a database connection.
The first thing to check is to go to your WordPress dashboard – YOURSITE.com/wp-admin
If you see the same main error: “error establishing a database connection”, skip this step.
Secondly, if you see something else, pay close attention to what WordPress is suggesting you should do. In its recent versions, WordPress has expanded its problem-solving solutions, so you might see some good advice there. One of the possibilities:
Another possibility is that you’ll see a message starting with “One or more database tables are unavailable […]” This means that it’s you who needs to “repair” the database.
This is actually quite simple. First, connect to your site via FTP once again. 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 (right before “That’s all, stop editing”):
Now upload the file back to your site (via FTP) overwriting the old version.
Go to YOURSITE.com/wp-admin/maint/repair.php
This is what you should see:
Click either of the buttons. Once the automatic repair finishes, go back to your site and see if the error is still there.
If no error, great! But you still need to revert your “wp-config.php” file to its previous state. Remove that new line that you just added and re-upload the file.
If the error’s still there, the next step is:
Step 4: Check Your Database Connection Credentials
As we mentioned, WordPress uses a specific username and password to connect to the database. Normally, those access credentials don’t change on their own. However, you can experience some weird scenarios after making any changes to your site files, the hosting environment, or anything else. In other words, even though it shouldn’t be happening, the access credentials sometimes do change. And with bad access credentials – “error establishing a database connection” happens!
The fix is done in the “wp-config.php” file. We’re talking about these lines specifically:
They hold all the details that WordPress needs to connect to the database. If any of these are wrong, no connection will be established.
Take those four lines from the “wp-config.php” file, simply copy them to the clipboard, contact your web host support again, paste those lines in the chat window and ask if the details are correct for you to be able to connect to your database.
The representative will check everything, and come back to you with some feedback. Most often, they will provide you with the correct details in case anything has been changed.
All you need to do is take the new credentials and put 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_HOST’, ‘localhost’); /* this is the web address of your database server */
Finally, re-upload the file via FTP.
At this stage, you can go back to your site and see if the error has been fixed.
If not, we have one final step for you:
Step 5: Restore The Default WordPress Files
As we mentioned before, the core WordPress files can get corrupted for a number of reasons. Maybe there’s been a hacking attempt on your site, maybe you modified some files by accident, maybe a rogue plugin did the modification.
Whatever it might be, you can still restore the default, native files of WordPress. The easiest way to do that is to just download a clean version of WordPress from WordPress.org and copy it over the one you have right now.
Go here, and download the newest package of WordPress to your desktop. Unpack. Browse through the unpacked archive and delete the “wp-content” folder.
Next, connect to your site via FTP, go to the main WordPress root, upload your new WordPress files. Overwrite all the previous files.
When the upload is done, go back to your site and see the effect.
This is a last resort kind of fix. If this doesn’t help, you will have no other choice than coming back to your web host support line, tell them everything that you’ve done, and asked for help.
How To Avoid This Error In the Future?
While there isn’t a 100% fail-proof way to protect your site against any error establishing a database connection, there are two main elements that you should have in your insurance puzzle to be at least 90% safe:
Quality hosting. Work only with hosts that have optimized their platforms to work with WordPress. That way, you can always count on knowledgeable support and fast help. Not to mention that you will also avoid many common WordPress problems.
Have backups. Install a simple backup plugin like UpdraftPlus (it works automatically) and you should always have a working WordPress backup that you can restore in case everything else fails.
We hope this guide helps!