Error Establishing a Database Connection in WordPress Fix
When you have been surfing the web for a while, you have at least seen this error a few times. Error Establishing a Database Connection is one of those curses that could be caused by many reasons like Not updates plugin, Corrupted WordPress files,
A corrupted database, and others. this could be very frustrating especially when it happened on its own without you changing anything. We ran into this issue yesterday on our own site. In this article, we will show you how to fix the error establishing a database connection in WordPress by compiling a list of solutions all in one place.
How Can Solve? Error Establishing a Database Connection
First thing you should know which error is you 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.
If you Getting WordPress white screen of death Read this
Just you need to add the following line in your wp-config.php file. Add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.
define(‘WP_ALLOW_REPAIR’, true);
Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php
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 continue reading this article as you might find another solution to work.
Checking the WP-Config file-
WP-Config.php is probably the single most important file in your entire WordPress installation. This is where you specify the details for WordPress to connect your database. If you changed your root password or the database user password, then you will need to change this file as well. First thing you should always check is if everything in your wp-config.php file is the same
define(‘DB_NAME’, ‘database-name’);
define(‘DB_USER’, ‘database-username’);
define(‘DB_PASSWORD’, ‘database-password’);
define(‘DB_HOST’, ‘localhost’);
Remember your DB_Host value not will always be localhost. Depending on the host, it will be different. For popular hosts like HostGator, GlobaliWeb, Site5, it is localhost. You can find other host values in the wp-config file.
Some People suggested that they fixed their problem by replacing localhost with the IP. It is common to see this sort of issue when running WordPress on a local server environment. For example on MAMP, the DB_Host value when changed to the IP may seem to work.
define(‘DB_HOST’, ‘127.0.0.1:8889’);
If everything in this file is correct, could be an error, there is something wrong on the server end.
Read our installation guide: How to Install WordPress in Cpanel
Check your Web Host MySQL Server
Often you will notice this Error Establishing a Database Connection your host server just cannot handle the load (especially when you are on shared hosting). Your site will get really slow and for some users even output the error. So the best thing you should do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive.
For those users who want to test if MySQL server is running yourself, you can do a few things. Test other sites on the same server to see if they are having the issue. Create a new file called testconnection.php and paste the following code in it:
<?php
$link = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>
Make sure to replace the username and password. If the connected successfully, then it means that your user has sufficient or privilege permission, and there is something else that is wrong. Go back to your wp-config file to make sure that everything there is correct.
If you cannot connect to the database by going to phpMyAdmin, then you know it is something with your server. It does not necessarily mean that your MySQL server is down. It could mean that your user does not have sufficient permission.
In our case, our MySQL server was running. All other sites on the servers were working fine. When we tried going to our phpMyAdmin, we ended up getting the error.