Skip to main content

How To Fix The Error Establishing A Database Connection In WordPress

This is a common issue faced my regular World Wide Web surfers. Error Establishing a Database Connection can be considered as one of those curses that result from a number of reasons. WordPress beginner finds it awfully frustrating, especially when it occurs on its own. I too had faced this issue many times. During the course of fixing the error, I realized that not much has been said and written about the issue online.

Here, I will show you how to fix the error establishing a database connection in WordPress.

What causes this error?

The error occurs when WordPress was unable to establish a database connection. But why does this happen? The reasons can vary:

– Sometimes database login credentials are wrong.

– The database login credentials get changed.

– Unresponsive database server.

– Corrupted database.

– A server error.

Troubleshooting the Issue

Method 1

The first step is to make sure you are getting similar error on both front-end and the back-end of the site (WP-admin). In case, the following error message is the same for both pages, proceed to the next step:

“Error establishing a database connection”

In case, a different error comes on the wp-admin such as “One or more database tables are unavailable. The database may need to be repaired”, then you must repair the database.

This can be done via adding the following line in your wp-config.php file. Make sure you add it right before: ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

1 define(‘WP_ALLOW_REPAIR’, true);

After adding the line, have a look at the settings via visiting the following page:

http://www.yoursite.com/wp-admin/maint/repair.php

It is not necessary that the user does not require being logged in for accessing this functionality once this define is set as it primarily aims at repairing a corrupted database. So users can often not login when the database is corrupt.

After repairing and optimizing database, remove this from wp-config.php.

Still Getting Error?

In case, the above stated repair technique did not fix the problem, or you have trouble running the repair, try out the following solutions:

Method 2:

Checking the WP-Config file

WP-Config.php is the most important file in the whole WordPress installation process. This is precisely the space where you specify the details for WordPress for connecting your database. In case, you changed the root password, or database user password, change this file too. Check if everything in the wp-config.php file is the same.

1 define(‘DB_NAME’, ‘database-name’);

2 define(‘DB_USER’, ‘database-username’);

3 define(‘DB_PASSWORD’, ‘database-password’);

4 define(‘DB_HOST’, ‘localhost’);

Remember the DB_Host value may not always be localhost. So depending on the host, it will differ. It is localhost for almost all popular hosts such as BlueHost, HostGator, Site5, etc.

Method 3:

LocalHost Replacement

The error can also be fixed by replacing localhost with the IP. Actually, the issue is also faced when WordPress is run on a local server environment. For instance, when the WordPress is run on MAMP, the DB_Host value, the issue can be fixed when changed to the IP .

1 define(‘DB_HOST’, ‘127.0.0.1:8889’);

IP’s vary for online web hosting services.

In case, everything in this file is correct, including typos, then something might be wrong on the server end.

Method 4:

Check your Web Host (MySQL Server)

Most of the time, the error occurs when a website gets swarmed with a lot of traffic. This is because your host is unable to handle the amount of load. This is true especially when you are on shared hosting. Your website will get very slow and even output the error. Hence, the best way to solve this error is to call hosting provider on the phone or connect them to and enquire if your MySQL server is responsive. If you want to ask any question visit here: jiteshmanaktala.com.

Want to test if it is responsive all by yourself? Here’s what you need to do: 

Other test sites on the same server. This will let you know if the sites have similar issues. In case, they are also getting a similar error, there is something wrong with your MySQL server. If no other site on the same hosting account gets an error, go to ‘cPanel’ and access phpMyAdmin. Now connect the database. If you can connect, verify if database user has sufficient permission. Then create a new file called testconnection.php. Paste the following code in it:

1 <?php

2 $link = mysql_connect(‘localhost’, ‘root’,

‘password’);

3 if (!$link) {

4 die(‘Could not connect: ‘ . mysql_error());

5 }

6 echo ‘Connected successfully’;

7 mysql_close($link);

8 ?>

You must replace the username and password. Once the connection is established successfully, then it means that your user has sufficient permission. The root cause of the issue is something else. So go back to your wp-config file. You need to make sure everything is correct (re-scan for typos).

When it is not possible to connect to the database via visiting phpMyAdmin, the issue is with your server. It does not mean that your MySQL server is down. It may also mean that the user does not have sufficient permission.

When you get the ‘access denied error’ while connecting to your phpMyAdmin or through testconnection.php results, then you must get in touch with your host immediately to fix it.

PS: Prior to making any of the database changes, make sure you have sufficient backups.

Leave a Reply