How To Fix "Establishing A Database Connection" Error in WordPress

This error is the most common one on the internet. We our-self have faced it a couple of this. The most annoying thing about this error is that it is occurs even though you’ve not changed anything. But no worries. We have come up with a solution in this article.

Cause of this error

The cause of this error is the failure of WordPress in establishing a database connection. The reasons for this could be that your database has been corrupted, your server is unresponsive, your database login details are incorrect and many more.

Does /wp-admin/ also face the problem?

You need to check if you are getting the same error message on both sides, the front-end, and the back-end. The message will read, “Error establishing a database connection”. But if you get a different error on the back-end you need to repair your database.

To repair database add the following code to your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Now visit you will see the setting page.

remove this from your wp-config.php after repairing and optimizing your database.

In case this does not help, try the following method.

Checking the WP-Config file

The file named WP-Config.php is where you specify the details for WordPress to connect your database. Make sure if everything’s the same in your wp-config.php.

define('DB_NAME', 'your-database-name');
define('DB_USER', 'your-database-username');
define('DB_PASSWORD', 'your-database-password');
define('DB_HOST', 'localhost:3006');

The value of your DB_Host might be different depending on your host.

Check web host (MySQL Server)

If your site gets swarmed with a lot of traffic then your site will face this error and you are on a shared hosting. So when your host can’t handle the load it will slow down the site and sometimes even show the error. Now contact your hosting provider.

If other sites on the same server and if they are having the same issue. If this is the case, then something is wrong with your MySQL server. Go to cPanel in case you don’t have any other site on the same hosting account. Then try to access phpMyAdmin and connect the database. Check if your database user has sufficient permission. Create a new file and name it testdb.php and paste this code:

$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
echo 'Connected successfully';

Replace username and password.

If the connection is successful, then your user has sufficient permission. Now you need to go back and re-scan for typos.

If you are using your own VPS then open ssh  and try

$: service mysql status

And check the status of your MySQL  if it’s showing stop then use the following command

$: service mysql restart

And check your MySQL status,  if your MySQL fails to restart then check MySQL error log.

Mainly myself stop due to fewer resources and try adding swap space.


Check out the top 3 WordPress hosting services:

Was this article helpful?