When you start using WordPress, you’re bound to get a lots of errors, some grievous, other not so much. It’s inevitable; it can be your fault or not, at times systems break, someone fails at their job, natural calamities strike – anything could cause an error. It’s frustrating and boring sometimes.
Even something as good as an increase in traffic can break your WordPress site, leaving you with an error and a bad taste in your mouth. Its really difficult to find out the exact error at the initial stage.
Now, just like any other human-made project, WordPress is susceptible to its share of errors, none of which is as perplexed and strenuous as the “error establishing database connection” error. That’s because this error doesn’t come with details of what went wrong, despite that it can be caused by several things, which – of course – only adds salt to the wound. The longer your website is down, the more traffic you are going to loose.
What is “Error Establishing a Database Connection” error in WordPress?
First, let’s take a look at how WordPress works to display your website, and it’ll become clear why this error is so devastating.
The WordPress software is written using PHP and MySQL for database. All the information that makes up your website is stored in a MySQL database, and WordPress uses PHP to store and retrieve and update the information from the database.
Generally, whenever a page is loaded on your website, it’s built on-the-fly. PHP code is used to access your MySQL database and retrieve every bit of information needed to create the page based on the condition defined in your PHP code. There’s a list of PHP query to access the post title, another to grab the author name, another to retrieve the publication date, and so on.
Back to your error message: “Error establishing a database connection.” This means that for some reason the PHP code was unable to connect with your MySQL database to retrieve the information it needs to build that page.
That’s why this error is often all you see on an otherwise blank page. If you can’t connect to your database, there’s no way to know what to display; no information about your website is available. To sum up we can say the error is due to faulty database server or any error with the database username or password.
What Causes “Error establishing a database connection.” Error?
While the meaning of the error message is clear, the reasons behind its occurrence often aren’t.
There are many reasons why your website could be having trouble connecting to your database, but they generally fall into three categories:
- Your login credentials for your database are wrong. Your database uses separate login credentials (username and password). If those credentials have recently been changed, your website won’t be able to access the database with the old credentials.
- Your database is corrupted. Any number of things could cause your WordPress database to become corrupted, from uploading a bad plugin, to lightning strikes to the server (seriously — though luckily that isn’t too common).
- The server that hosts your database is down. Your database’s server may be down due to a problem or error on the part of your web hosting company, or you could be experiencing a big traffic spike that your server just couldn’t handle.
So with all these possibilities, how can you narrow down the problem and fix it?
- Unresponsive database server – The server where your database lives could be as dead as a dodo; caput, unresponsive or still recovering from a traffic upsurge.
- A corrupted database – Bad plugins, addons, themes and data-transfer interruptions might leave your database short of tables or completely corrupted at worst.
Does the problem occur when you try to access WordPress dashboard?
First thing you should do is to make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin). If the error message is the same on both pages “Error establishing a database connection”, then proceed onto the next step. If you are getting a different error on the wp-admin for instance something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.
You can do this by adding the following line in your wp-config.php file. Add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.
Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php
Repair database in WordPress
Remember, the user does not need to be logged in to access this functionality when this define is set. This is because its main intent is to repair a corrupted database, Users can often not login when the database is corrupt. So once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php.
If this repair did not fix the problem, or you are having trouble running the repair then you will need to dig down more to find about about the issue.