In my early days managing WordPress websites, I dreaded waking up to an error like, “error establishing a database connection.”
I don’t, but, sometimes you expect things to work.
So you are in that place too, and you are scratching your head trying to figure out how to fix error establishing a database connection in your WordPress website.
As the years have stacked up, I have lost count on the number of times I have seen this error, and today I will help you sort it out.
Step by step: How to fix error establishing a database connection in your WordPress website
Troubleshooting WordPress errors
We shall use an isolation method to try and get to the bottom of the issue.
I think as a web admin when troubleshooting errors make a checklist of likely causes of an error then walk down that list until you isolate the culprit.
This is not only a good practice, but it grows your skills.
Then, over time it will take you less time to troubleshoot and fix errors because you have a systematic way of approaching issues that arise on your websites.
Fix error establishing a database connection
Modern applications like WordPress have good error handling mechanisms.
In our case, by looking at the error message printed on a web page, we can tell that we have a communication issue between the WordPress files and the database.
So let us get started.
Check the disk space on your hosting account
The most logical and often the easiest way to start is checking your hosting account to be certain that you have enough disk space.
The last 5 or so error establishing a database connection I have had to fix boiled down to this.
Your database and files sit on a server somewhere, if there is not enough space to run processes, cPanel or any other hosting software you use will halt certain services, and one of those is the database services.
To fix this, delete unnecessary files or upgrade your hosting account.
Check the configuration on the config file
Next up, take a look at your config files, a plugin, theme or a hack may have modified some of your files. If that is the case, WordPress will not be able to connect to the database.
Verify that the details in this file are the same as those of the database.
The easiest thing you can do is change the password and then come and paste it in the config file.
In case of a hack, the best thing to do may be replacing the files with a clean copy.
That is why we recommend, as a good practice to always maintain a backup program for your website.
Its times like these that restoring and fixing errors like this can be completed with a click of a button.
Fix database corruption
Sometimes the issue may not be with your host or files.
The database or some tables in it have been corrupted.
To quickly isolate that as the culprit, browse to the admin area of your website.
That means going to www.yoursite.com/wp-admin
If you get the same error, “error establishing a database connection,” there are high chances that your database settings are wrong, keenly verify them.
If you, however, see a different error “One or more database tables are unavailable. The database may need to be repaired.
We can confidently isolate the issue causing the error.
Repair a corrupted WordPress database
To repair a corrupted database, you will need to add a line of code into your wp-config.php file.
Once you have done that, remember to save and then in your browser go to http://yoursite.com/wp-admin/maint/repair.php
Following all the prompts, that should fix the corrupted database.
In case some tables fail to repair, another error is often thrown like this one;
“db_options: Table is marked as crashed.”
To fix that, on your hosting control panel go to phpMyAdmin and repair the crashed table.
Once you have repaired the table, you can run the repair script again.
Your website should be fine now.
If everything is working fine, remove the line of code we entered earlier to the wp-config file.
Contact your host’s support
Finally, if you have tried all these and you are still experiencing the same error, then there could be some other underlying issue with your host.
If that is the case, the best thing to do is contact them.
Explain to them all the things you have done to fix it.
For the lazy ones, you may skip all this and contact your host, that is if you have a managed plan with them.
But, if you will be working with WordPress a lot, it’s a good idea to get your fingers dirty every once in a while.
Finally, creating backups will save you from all sorts of nightmares, schedule an automatic backup today using our free WPvivid Backup Plugin.
You may also want to learn how to fix the 403 forbidden error in WordPress.