If you’re on this page, you probably are facing the “Error establishing a database connection” error on your WordPress blog.
That’s exactly, what I aim to solve in this piece, and not just the solution, but what causes it and how it can be prevented will be covered as well, so brace yourselves.
Before going for the solutions, it’s only fair we discuss what causes it, so here goes.
Why does the “Error Establishing Database Connection” Show Up?
As the Error almost self-describes itself, it’s an error with the Database. The Database here indicates the “SQL database” for your WordPress installation.
This database most often has a self-generated login username and password, although it can be manually created as well by other advanced users.
So what happens is, when the login password is changed, or altered by some error or unconscious action, the WordPress installation is unable to connect to the database, and without the Database, the site can’t load.
Or the other possible reason is your database got corrupted, or maybe it’s unresponsive because of some other server issue.
The fixes for this aren’t too hard, and anyone with basic knowledge of copy-pasting or opening file and folders can solve them if this guide is followed.
How to Fix “Error Establishing Database Connection”?
Now there are quite a few solutions to this, but the simplest and most straight-forward is fixing your Wp-Config file.
The Wp-config file, despite being probably the least code-rich, is the most important file in your complete WordPress installation (ironic, isn’t it?)
Well the good news is, most WordPress errors can be fixed by slightly altering the wp-config file.
Simply access the file from your Cpanel > WordPress Installed Directory > Wp-config.php file, and make sure the username and password for the database is correct.
If it’s correct, check if your “DB HOST” value is being shown as “Localhost”, which isn’t always correct.
If it’s “LocalHost”, you can try changing it to your hosting company’s IP address. You can get it either from intodns.com, or by mailing your hosting provider.
If it solved the problem, great. If it didn’t, worry not we’ve got other solutions lined up.
2. Temporary Traffic Issue
Another issue which may be the cause for this error is heavy traffic. Yes, if you suddenly start getting a lot of traffic, your MySQL server may fail to handle the new surge of traffic.
It’s easy verifying if that really is the cause. If you have other sites on the same server / hosting account, simply load up any of those sites.
If none of the sites in this account load up, it clearly indicates that your SQL server is down, and you’ll need to get in touch with your hosting provider for a fix.
At times, your user may not have enough permission to access your SQL server, as a result the SQL server won’t load up for them, and would result in the error.
First, you need to verify if that really is the case, to do so simply login to your Cpanel, and click on “PhpMyAdmin”. Once you successfully connect to your PhpMyAdmin, you need to create a new file, and name it “Testconnection.php”.
Once that’s done, paste the following code there, and edit the part in bold, with your SQL server details.
For the following code, “Localhost” is your “DB Host” value, “root” is your “username”, and “password” is password”.
If the connection is successful, it’ll mean that your permissions are fine, and you’ll need to refer to the next solution in this list.
If the connection isn’t successful, well in that case yes it’s a permission issue, and again you’ll need to talk to your hosting provider for a solution.
Talk to Hosting Providers
This is like the ultimate solution, considering you have really good hosts, such as Hostgator, Bluehost, Siteground etc.
You can easily find their contact information on the websites, or in some cases even live-chat, and toll-free numbers are provided (as with Bluehost).
So if you’ve checked the wp-config file, and you’re sure it’s not because of traffic, and the permissions seem fine
Different Error on WpAdmin and Front-end Website
In case the errors on your main site and Admin panel aren’t exactly the same, you’ll need to follow slightly different steps to sort this out.
What’s happening in this case is, your Database is probably corrupt or it needs a repair. Either way, it can be achieved easily, all you’ve got to do is, login to your Cpanel, and again edit the Wp-config.php file
Find the last line in the file, and just before the last line, add the following code:
Done? Now save it. Once you’ve saved it, you need to run a repair instance. Which can be done by simply going over to this URL:
“YourSite.com” here needs to be replaced with your site URL so that you can repair your database.
No login is required for this action (cause probably your WpAdmin isn’t working anyway), and hence the addition of the above code to the wp-config file acts as a validation that the admin really wants to run the instance.
Anyway, once you enter the URL, click on “Repair Database” or “Repair Database and Optimize”, note that both the options work fine, and can be used, although the latter takes slightly longer time to get done.
Once your Database is repaired, you can then move on to solve the Database issues either using the above mentioned tweaks, or by contacting your hosting company.
Another common issue with WordPress is its “Briefly unavailable for service maintenance” error, if you wish to be prepared for when it happens, or are already a victim to it, 000Webhost has an ultimate, detailed guide on how to fix it.
So that’s a wrap folks. I’ve shared as many ways as possible to help you fix the “Error establishing database connection” Error.
Although obviously there are other ways, they need some coding skills in the least, so instead of trying them out for yourselves, use the “hosting provider” option so you don’t mess things up.
Do let me know if this piece helped and if you want more of these from us.