WordPress Error Establishing A Database Connection - How to fix it?
The most awful and frustrating situation faced by almost each WordPress user especially, when WordPress Error Establishing a Database Connection happened on its own without you changing anything.
I know that you’re probably a bit worried right now, but let me assure you that this problem can be fixed and any damage can be anticipated. In fact, the error establishing a database connection is quite natural. So, before coming up to the solution, let me brief you more about the issue so that you can get exact idea about the problem.
Why do you get this error?
Well if I brief, you are facing this issue because of your WordPress is unable in establishing a database connection.
Now the question arises why so?
WordPress is created using PHP and MySQL. Placing both together, WordPress uses PHP commands to connect to the MySQL database, get the information and then displays it on the screen. This error occurs when, for whatever reason, WordPress is unable to access the information from the database via PHP commands.
When that happens, WordPress basically doesn’t know how to deal with it, so the only thing it can do is displays a simple long serious text, “Error establishing a database connection!”. Now it is your headache how to fix this. But need not to worry, I have the solution.
There are 5 most known reasons why this issue occurs.
1. Incorrect Login Credentials
You need to have the correct credentials to access the database. If the credentials have changed for any reason then WordPress will no longer be able to retrieve anything from the database.
2. Corrupted WordPress Files
Failed updates of plugins, themes, and WordPress software updates may lead to corrupted WordPress files.
3. Corrupted Database
Bad plugins, add-ons, themes might leave your database tables corrupted.
4. Database Server is Down
The server where your database lives could be dead. The database server can simply be down for whatever reason.
5. Too Much Traffic
Even though this is good to have, but still it is a problem. Your database might be unable to respond due to a high spike in traffic. For example, maybe one of your articles went popular and now everyone tries to access your site? Hence the server is not able to handle those requests, results in breaking.
Troubleshooting “Error Establishing Database Connection.”
We can troubleshoot EECD issue by following step by step guide as given below. Before implementing, detect the root cause of your problem
1. Is Your WordPress Database Corrupted?
Check whether the error is a result of a corrupted database. For this, you should make sure that you are getting the same error on both sides of a site front-end as well as the back-end (wp-admin).
If loading the front-end (i.e. example.com) and the back-end (i.e. example.com/wp-admin/) results in the “Error Establishing Database Connection” error, your database is healthy, and need not worry about the next step. You can skip determining whether you have the correct login credentials in your wp-config.php file.
If you are getting a different error on the back-end (i.e. example.com/wp-admin/), for example, “One or more database tables are unavailable”, then you need to repair your database.
You can do this by adding the predefined constant in your wp-config.php file. Add below line just before ‘That’s all, stop editing! Happy blogging’ line in wp-config.php.
define(‘WP_ALLOW_REPAIR’, true);
Once you have done, you can check the settings by visiting this page: http://www.example.com/wp-admin/maint/repair.php
So once you are done repairing and optimizing your database, make sure to remove the constant from your wp-config.php.
2. Checking the Database Login Credentials
wp-config.php, the most important file in your entire WordPress setup folder. This is the place where you specify your database credentials. WordPress uses a specific username and password to connect to the database.
If you ever changed your root password or database user password, you will need to change this file as well.
The first thing you should always check everything in your wp-config.php file is same.
1. define(‘DB_NAME’, ‘database-name’);
2. define(‘DB_USER’, ‘database-username’);
3. define(‘DB_PASSWORD’, ‘database-password’);
4. define(‘DB_HOST’, ‘localhost’);
Take those four lines from the wp-config.php file, simply copy them to the clipboard, contact your web host support again, paste those lines in the chat window and ask if the details are correct for you to be able to connect to your database. The representative will provide you with the correct details in case anything needs to be changed.
All you need to do is take the new credentials and put them in your “wp-config.php” file where the old ones used to be.
3. Check your Web Host (MySQL Server)
Sometimes, this error occurs when your site gets a lot of traffic. And your hosting server cannot handle this huge load of traffic especially when you are using the Shared Hosting.
So, in that case, you should ask your hosting provider about the same error. Start chatting using Live Chat and ask Is the database server running without any problems?
In most cases, they will either let you know what the cause is exactly, or they will even fix it for you. but if the fault is not on their end, (their server is operational) they are not able to help you.
4. Check If Your Plugin or Theme Files Haven’t Been Corrupted!
Sometimes the error occurs because your WordPress files might get corrupted due to unexpected events. For example, you have updated your plugin or theme and your whole WordPress collapsed! or maybe you hand-edited any file in WordPress.
if you have done any of the change, then the culprit is at your end.
In this case what you can do:
1. First, connect to your host via FTP. You need a login and a password to your FTP account.
2. Go to the WordPress root directory, includes sub-folders such as wp-content, wp-admin or wp-includes.
3. Click on wp-content and rename the plugins folder to something else.
4. Now check your site whether the error is still there or vanish.
5. If it vanishes, that’ good, it means the problem is with any of the plugins.
6. Again Go to the FTP and rename the Plugins Folder back to the original name. and Now Change the names of the individual plugins, one by one.
7. The simple way is to rename the folders with the hyphen(-) or an underscore(_).
How To Avoid EEDC Error In Future?
We are not ensuring any guarantee. But there are some ways you can protect your site against error establishing the database connection.
There are two main factors that you must keep on high priority.
1. Quality Hosting: You should always keep in mind that your host provider has optimized their platform to work with WordPress. In that way, you can always get fast support and help.
2. Backups: It is Good to keep backups. Backups are actually like a backbone when you face any trouble. You just need to install a simple backup plugin like Backup Bank or you can go for any other backup plugin like UpdraftPlus. You should always have a working WordPress backup that you can restore in case everything else fails.
I hope this guide helps!