Encountering an error while browsing can either be scary or frustrating, but there are those exceptional errors that can produce both feelings. Your reaction to “Error establishing a database connection” message will depend on whether you see it while trying to access a random site or your own.
Even if the error pops up on your site, though, there’s no reason to worry; we’re here to explain what this error is, as well as discuss how to fix it.
What the Error Is
Most common on WordPress sites, “Error establishing a database connection” occurs when the PHP code can’t get the information needed to display the page.
This information is stored in the MySQL database and can’t be retrieved if the code has problems communicating with the database. With no information, the page will be blank except for the message.
Why the Error Occurs
Reasons that can lead up to the database connection error vary from more to less troublesome, and there’s even one that can be considered good! If you are seeing this error message on your site, here are some probable causes.
1. Login Credentials
To access the database, WordPress must have specific login information that could change in certain situations, like moving the site to a different hosting platform.
2. File or Database Corruption
The database and core WordPress files can become corrupted due to several factors, including failed updates, hosting issues, and hacker attacks.
3. Database Server Is Down
If your website files are stored on a different machine than the database, they will run on two separate servers. The one running the database could just be down, which can happen for several reasons.
4. Traffic Spike
This is the one reason you may feel good about – too much traffic can overwhelm the server, causing the database to become unresponsive. If people are visiting your site in such numbers that the server can’t handle it, of course, you still have a problem that needs solving, but the situation can also be a cause to celebrate.
How the Error Is Fixed
There are several options at your disposal to remedy the problem, and they will, depending on the cause, take more or less time and effort.
Here’s how you can fix the error establishing a database connection:
Contact Your Host
The first thing you should do if you encounter this error is to get in touch with your hosting provider. Once you explain the problem, they can tell you if there’s something wrong on their side of the connection. You can find out if the database is up and running if there’s a spike in traffic, or if they’ve noticed some suspicious activity on the server.
You’ll find out if the problem is on their part or yours. If there’s something they can do about it, chances are they’ll fix the error, but even if you need to take care of it yourself, the server host support might be able and willing to help you.
Check Your Credentials
If the error’s caused by bad login credentials, it should be an easy fix. Four values need to be correct for WordPress to connect to the database: database name, MySQL database username, MySQL database password, and MySQL hostname. These credentials can be found in the wp-config.php file.
You should open the file and copy the credentials, then check them with your hosting provider. They’ll either confirm the values or give you the correct ones. In the latter case, you should input the new credentials in the wp-config.php file and then save and reupload it.
Since incorrect credentials are the most common reason for the Error establishing a database connection, the problem will most likely be solved.
Search for Potential Corruption
It could be that the error is caused by corrupted WordPress files or database corruption. We’ll start with the database problem since it’s both less likely to happen and somewhat easier to solve.
For a corrupted database, WordPress has a database repair mode. You can access it by adding the line “define (‘WP_ALLOW_REPAIR’, true);” at the very end of the wp-config.php file.
Then you need to go to https://domainname.com/wp-admin/maint/repair.php (where “domainname” should be the name of your site) where you can select the option to repair or repair and optimize the database.
In this case, it’s better to go with just Repair. After the repair, remember to remove the line added to the wp-config.php file.
For corrupted WordPress files, there is a quick fix and a slower, more thorough one. The quick route is to simply download a fresh version of WordPress and upload it to your site – all except for the wp-content folder and the wp-config-sample.php file. Once uploaded, the new files and folders will overwrite the old.
The thorough method is to first log in to your server by using your FTP account. You can find your password for that in an email you got from the web host or contact them for the credentials. Once you’ve done that, you’ll have to go to your WordPress root folder, then to wp-content, and check every subfolder one by one.
The way to do that is to rename a subfolder, i.e. by adding “_” at the end and launch your site to see if it’s loading properly. If you see that the site is functioning normally after renaming a folder, use the same method, only one step deeper – revert the folder name to the default and try to rename its subfolders.
This method might take you a while, but it’ll help you pinpoint the exact folder, plugin, or theme that’s causing the error.
Prevent It from Happening Again
To avoid the error in the future, you should make sure to always have your credentials set up as they should be. Another thing that can save you the headache is having a backup.
Thankfully, several plugins do just that – select one that seems best and rest assured that, no matter what, you’ll have a working version of your site to revert to.
Finally, going with a web hosting provider optimized for WordPress will remove many problems that might occur otherwise, and you’ll probably get competent customer support for any unexpected issues.