“Error establishing database connection”
… Is the definition of a bad day if you are a WordPress website owner.
Admit it, this error message looks pretty serious. No design, no familiar WordPress site elements, just text:
First, stop panicking!
We know that you might be a little scared right now, but let us assure you this problem can be fixed and permanent damage can be prevented. In fact, errors in establishing database connections are quite common.
Understanding the Problem
As much as we don’t want to play Captain Obvious here, the nature of the error is pretty straightforward – an error establishing the database connection.
This is what it means exactly:
WordPress uses two main technologies to provide you with all the amazing capabilities: PHP and MySQL.
- PHP is a programming language. WordPress – the core WordPress files – is written in PHP. Boring, sorry.
- MySQL is a database technology. WordPress uses a MySQL database to store all of your website content. This includes your posts, pages, but also smaller elements, such as your site title, your widget layout, colour settings, and more. Basically, a MySQL database is where every, even the smallest piece of information about your website, is stored.
Putting the two together, WordPress uses PHP commands to connect to the MySQL database, get the information it needs, and then display everything on the screen.
This is where things go wrong. An error occurs when, for whatever reason, WordPress is unable to access information in the database via PHP commands. When that happens, WordPress basically doesn’t know what to do next, so the only thing it can say is, “Error establishing a database connection.”
With that being said, there are several reasons why mistakes can occur. Some of the more common:
- Incorrect login credentials. WordPress requires a special login and password to access the database. If that has changed for any reason then WordPress will no longer be able to retrieve anything from the database.
- Corrupt WordPress files. WordPress core files can get corrupted due to failed updates, among other things. This can include things like failed plugin updates, themes, and even major WordPress software updates.
- The database is damaged. This may be the result of a malicious plugin cluttering up the database internally, hacker attacks, failing themes, and more.
- Server The database server is down. In some cases, a web server – the server that stores your website files – is running on a different machine than the database. And that separate database server could just go down for whatever reason.
- Traffic Too much traffic. While this was a good problem to have, it was still a problem. In particular, your database may be unresponsive due to high traffic spikes. For example, maybe one of your posts went viral and now everyone and their dogs are trying to access your site? Hence the server was unable to handle a portion of these visits.
How to Fix “Error Establishing Database Connection”
Since we value your time, we will first recommend the most likely pathway to fix the problem for you immediately. Then, we’ll dig deep and show you how to do all the repairs yourself manually.
Step 1: Connect with your Web Host Provider
All quality hosts these days offer some form of fast-reacting support, usually via live chat. It’s safe to assume that if your site was working a few minutes ago (or a day ago) and it isn’t now then the error could be on your web host’s end.
Start by calling the live chat, saying that you had a “database connection error” (paste phrase), and ask:
- if the database server is up and running without problems, and
- if there is no activity spike of traffic on the current server.
In most cases, if you tell them what kind of error you experienced, they will tell you what the real cause is, or they will even fix it for you.
Most importantly, they will inform you if your database is fine and if there is any suspicious activity on the server or not.
If the error doesn’t happen to them (the server is up and there is no traffic spike), and they don’t want to help you, go to step 2:
Step 2: check if your plugin or theme file is not corrupted
Your WordPress file might get corrupted due to some bad events.
For example, maybe you’ve updated a plugin or theme, and they all crashed? Maybe you are linking your site to an external service? Maybe you edited some files by hand?
If any of the above rings a bell, you probably have the culprit. If you haven’t done that, skip this step.
Here’s what you can do:
- First: connect to your host via FTP. You can do this with a free tool called FileZilla.
To connect, you will need a login and password to your FTP account. If you don’t know what / where it is, the easiest way to get it is to search through the initial email you got from your web host when you first signed up. Alternatively, you can ask a live chat support representative to provide you with those details.
After that, connect to the server and navigate to the WordPress root folder. It’s usually called “public_html” or “www” or you might already be on one after establishing the FTP connection.
The rule of thumb is if you can see sub-folders like “wp-content” and “wp-admin” then you are at the root of WordPress. See below:
- Second: Go to “wp-content” and rename the “plugins” folder to something else (whichever is any other name, as long as it’s not “plugins”):
- Third: Go back to your site and see if the error is still there.
If not, you now know that it was caused by some plugin. Back to FTP, rename the folder back to “plugins”. Go in, and rename the folders of each plugin, 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 was triggered or went away. Once you’ve narrowed it down to one specific plugin, you’ve found the problem maker. Remove or go online to see if the solution has been documented.
- If the error persists even after renaming the “plugins” folder, perform the same process as the “themes” folder.
- If that doesn’t help, rename both folders back to “plugins” and “themes”. The error was caused by something else.
Step 3: Check If Your Database Is Uncorrupted
The WordPress database is a pretty complex thing, and there are lots of little cogs that need to keep working perfectly together to avoid problems. Sometimes, things are out of place and that’s when you see an error establishing the database connection.
- The first thing to check is to open your WordPress dashboard –
If you see the same main error: “Error establishing database connection”, skip this step.
- Second, if you’re looking at something else, pay close attention to what WordPress suggests. In its latest version, WordPress has expanded its troubleshooting solutions, so you might see some good suggestions there. One possibility:
Another possibility is that you will see a message starting with “One or more database tables are not available […]” This means that you need to “repair” the database.
It’s actually quite simple. First, connect to your site via FTP once again. Open the root of WordPress and download the “wp-config.php” file to your desktop.
Edit this file in notepad, adding one extra line at the end (right before “That’s all, stop editing” ):
define ('WP_ALLOW_REPAIR', true);
Now upload the file back to your site (via FTP) overwriting the old version.
Here’s what you have to see:
Click a button. After the auto repair is complete, go back to your site and see if the error is still there.
If there are no mistakes, great! But you still need to restore your “wp-config.php” file to its previous state. Delete the new line you just added and re-upload the file.
If the error still exists, the next steps are:
Step 4: Check Your Database Connection Credentials
As we mentioned, WordPress uses a specific username and password to connect to the database. Typically, these access credentials do not change on their own. However, you can run into some strange scenarios after making changes to your site files, hosting environment, or whatever. In other words, even though it shouldn’t happen, the access credentials sometimes change. And with bad access credentials – a “database connection error” occurs!
Repairs are made in the file “wp-config.php”. We talk about these lines in particular:
define ('DB_NAME', 'some_db'); define ('DB_USER', 'some_username'); define ('DB_PASSWORD', 'some_pass'); define ('DB_HOST', 'localhost');
They store all the details that WordPress needs to connect to the database. If something goes wrong, no connection will be made.
Note that there is a manual way to fix this problem, but to be honest it takes a few steps and can get really confusing based on the web host you are using and the interface they are using. In other words, whatever we say will still apply to the small number of users who happen to be on the same host as us, so we have a more universal solution for you:
Take those four lines from the “wp-config.php” file, simply copy them to the clipboard, contact your web host support again, paste the lines in the chat window and ask if the details are correct so you can connect to your database.
A representative will go over everything and get back to you with some feedback. Most often, they’ll give you the right details in case something changes.
All you need to do is take the new credentials and put them in your “wp-config.php” file where the old one was.
define ('DB_NAME', 'some_db'); / * ini adalah nama basis data WordPress Anda * / define ('DB_USER', 'some_username'); define ('DB_PASSWORD', 'some_pass'); define ('DB_HOST', 'localhost'); / * ini adalah alamat web server database Anda * /
Finally, re-upload the file via FTP.
At this point, you can return to your site and see if the error has been fixed.
If not, we have one final step for you:
Step 5: Restore Default WordPress Files
As we mentioned earlier, WordPress core files can get corrupted for several reasons. Maybe there was a hack attempt on your site, maybe you modified some files by mistake, maybe a malicious plugin did the modification.
Either way, you can still restore the original default WordPress files. The easiest way to do this is to download the clean version of WordPress from WordPress.org and copy over the one you have now.
Come here, and download the latest package to your desktop. Take apart. Browse to the unpacked archives and delete the “wp-content” folder.
Next, connect to your site via FTP, go to the main WordPress root, upload your new WordPress file. Overwrite all previous files.
After the upload is complete, return to your site and see the effect.
This is the final type of fix. If this doesn’t help, you will have no other choice but to get back on the line of support for your web host, tell them everything you’ve been up to, and ask for help. At this point, you’ve basically run out of everything you can do to fix the problem yourself and it’s the host’s turn to solve it. After all, you paid your hosting bill, didn’t you?
How To Avoid These Mistakes In The Future?
While there is no 100% fail-safe way to protect your site from errors in establishing a database connection, there are two main elements that you must have in your insurance puzzle to be at least 90% secure:
- Quality hosting. Only works with hosts that have optimized their platform to work with WordPress. That way, you can always count on extensive support and quick assistance. Not to mention that you’ll also be avoiding a lot of common WordPress problems. Here are our recommendations.
- Have a spare. Install a simple backup plugin like UpdraftPlus (it works automatically) and you should always have a working WordPress backup you can restore if all else fails.
We hope this guide has been helpful! And as always, feel free to contact us if you have any difficulties with establishing a database connection.
Mustafa Al Mahmud is the Founder and CEO of Gizmo Concept and Blogger Concept and also a professional Blogger, SEO Expert as well as an Entrepreneur. He loves to travel and enjoy the free moment with family members and friends.