Fix : Error establishing a database connection in WordPress


If you surfed the web for some time, you saw this error at least a few times. Error establishing a database connection in WordPress is one of the causes of many reasons. This could be terribly frustrating as a WordPress beginner, especially if it happened on its own without you changing anything. It took a little more than 20 minutes to detect the problem and fix it.

Before doing the database changes make sure you’ve backup your site data.

Why this Error Occurs?

Well, in short, you get this error because WordPress cannot connect to a database. Now, the reason why WordPress cannot connect to a database may vary. You may have incorrect or changed your database login credentials. Your database server might be unsatisfactory. Your database could have been corrupted.

In our experience, this error happens most of the time because of some kind of server error, but there could also be other factors. Let’s look at how this problem can be resolved.

Checking the WP-Config file

WP-Config.php is probably the single most important file for the entire installation of WordPress. This is where you specify the WordPress details for connecting your database. If you have changed your root password or your user password for the database, you must also change this file. You should always check to see if everything is the same in your wp-config.php file.

define('DB_NAME', 'database-name');

define('DB_USER', 'database-username');

define('DB_PASSWORD', 'database-password');

define('DB_HOST', 'localhost');

You may not always remember your DB Host value as localhost. It will be different depending on the host.


Some people suggested that they replace their localhost with the IP. This kind of problem is common when you run WordPress in a local server environment. For example, on MAMP, the DB Host value may seem to work when changed to the IP.

define('DB_HOST', '');

IP ‘s for web hosting services will vary. If everything in this file is correct( check for typos), it is fair to say that something is wrong at the end of the server.

Check Web Host (MySQL Server)

You will often notice this error in connecting the database when your site is swarming with a lot of traffic. Basically, the load cannot be handled by your host server (especially when you are on shared hosting). Your site will get really slow and even make the error for some users. So the best thing you should do is contact your hosting provider on the phone or live chat and ask if your MySQL server is responsive.

You can do a few things for those users who want to test if the MySQL server runs on their own. Test other sites on the same server to find out if they have a problem. If you also have the same error, you certainly have something wrong with your MySQL server. If you have no other site on this same hosting account, just go to your cPanel and try accessing phpMyAdmin and connecting the database. If you can connect, we must check that your database user has enough permission. Create and paste the following code into a new file called testconnection.php:

$link = mysqli_connect( 'localhost' , 'username' , 'password' )'
if (! $link ) {
die ( 'could not connect: ' . mysqli_error() );
echo&nbsp; 'Connected successfully' ;
mysqli_close( $link );

Keep the username and password replaced. If the script was successfully connected, it means your user has enough permission and something else is wrong. Return to your wp-config file to ensure that everything is correct (types are scanned again).

If you can’t connect to the database by going to phpMyAdmin, you know that your server has something to offer. This doesn’t mean your MySQL server is down. It might mean that your user hasn’t enough permission.

So, if you get the denied access error when you connect to your phpMyAdmin or testconnection.php results, you should contact your host immediately to fix it.

If you like this Article, then Share it. your one small share at a time, and will continue encouraging me to do so. So I appreciate your support, my dear reader if you share this page on your social profile.

You May Also Like


We at WPbloggerbasic, are Team Of Professionals, We Write With Passion Serve With Creativity and Understand Your Problems With Care.


Leave a Reply

Your email address will not be published. Required fields are marked *