How to Solve the “Error Establishing a Database Connection” Problem on WordPress


Finding out that your site is down with a simple Error establishing a database connection displayed across what used to be the homepage of your site can be overwhelming. Even worse, there is no single cause behind this error so it can take a while to troubleshoot and solve.

But before you throw in the towel and call in the professionals, there is a good chance you can fix it all by yourself. With this in mind, in this post, we’ll cover why this error occurs and how you can troubleshoot and fix it to have your site up and running again.

What Is “Error Establishing a Database Connection” Problem?

If your PHP code isn’t able to access your MySQL database then that makes it unable to fetch all the resources that are required to load your site’s page. And when that happens, you’ll end up with an Error establishing a database connection message.

There are a number of reasons why this might happen and they can broadly be categorized under the following:

  • Incorrect Login Credentials. Your database uses a set of login credentials (username and password) that are separate from your site. Your site cannot access the database without those credentials.
  • Corrupted WordPress Files. You can run into this error by editing core WordPress files and accidentally modifying the existing WordPress code. This is more likely to be the cause if your site was functioning fine prior to the modification.
  • Corrupted Database. Your database can get corrupted if you install a defective theme/plugin or if the server hosting your site is damaged.
  • Unresponsive Host Server. Sometimes your WordPress hosting provider might have a server down which leads to an Error establishing a database connection message showing up on your site. If you are using a good hosting provider, your site should be up in no time.

With that out of the way, let’s take a look at how you can get your site back online – in four easy steps!

How to Solve the “Error Establishing a Database Connection Problem” on WordPress

In order to get things back on track, you’ll first need to identify where things went wrong.

We recommend that you call your hosting provider first to check if there is server maintenance or upgrades underway. In such a case, your site should be back online as soon as they are done.

However, if things are smooth over at the hosting provider’s end then you’ll have to troubleshoot Error establishing a database connection yourself. Since you will be tinkering with core WordPress files and the database, it’s a good idea to take a full backup of your site before stepping through the troubleshooting tutorial.

Once that is done, get your favorite code editor ready and move to the first step.

Step 1: Troubleshoot the Error Using the wp-config.php File

It’s a good idea to begin with the default WordPress database repair functionality. To get started, log in to your cPanel account and navigate to the wp-config.php file.

Open the file up in a code editor and add the following lines of code at the very bottom:

define( ‘WP_ALLOW_REPAIR’, true );

Save the file and enter the following URL in your browser:

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

You will be presented with two options – repair your database or repair and optimize it. If you choose to go with the latter, it’ll take a little bit longer to complete. Once that’s done, check to see if your site is functioning correctly. If it isn’t, move on to the next step.

Note: You don’t have to be logged in to access this page so make sure you remember to remove this line of code after you’re done.

Step 2: Check Your Database Login Credentials and Settings

Using the wp-config.php file you can also check the login credentials for your database. To do this, open up the file once again and look for the following code snippet:

// ** MySQL settings – You can get this info from your web host ** //

** The name of the database for WordPress */

define( ‘DB_NAME’, ‘your_database_name’ );

/** MySQL database username */

define( ‘DB_USER’, ‘your_database_username’ );

/** MySQL database password */

define( ‘DB_PASSWORD’, ‘your_database_password’ );

/** MySQL hostname */

define( ‘DB_HOST’, ‘localhost’ );

There are four values here – the database name, username and password for the database, and the MySQL host name. If any of the values are incorrect then WordPress cannot connect with the database.

Step 3: Test Your Database Username and Password

In order to check whether the username and password are valid, we’ll create a new PHP file. Navigate to your WordPress installation’s root directory, create a file, and name it testdb.php.

Now, add the following code to the file:

<?php

$testlink = mysql_connect(‘localhost’, ‘root’, ‘password’);

if (!$testlink) {

die(‘Connection was not successful: ‘ . mysql_error());

}

echo ‘Connection was successful!’;

mysql_close($testlink);

?>

In the second line of the code, replace localhost, root, and password with the values for your database. Once that’s done, save your file and navigate to the following URL from your browser:

http://www.your-site.com/testdb.php

If you get a successful connection message then your login credentials are working. However, if you do not get access then you will have to create new credentials by creating a new database user.

Step 4: Create a New Database User

To get started, log in to your cPanel account and navigate to the MySQL databases section. Once you find it, scroll down to the Add a New User section and you’ll be prompted to create a new database user by entering a new username and password for it.

Next, you’ll have to assign the new database user to your site’s database. As we mentioned before, the name of your database can be found from the DB_NAME attribute in the wp-config.php file.

Once you have your database’s name, you can assign the new database user to your database by scrolling down to the Add a User to a Database section. Select the name of the new database user and click the Add button.

Finally, open up your site’s wp-config.php file in a code editor and replace the old database user’s credentials with the new ones. Save the file and try accessing your site again.

If Nothing Else Works

If this four-step approach didn’t solve the Error establishing a database connection problem then you may have a case of corrupted WordPress core files. Those of you who are programming literate can backtrack all the modifications you made to the code (hopefully!) till you can isolate the culprit that is causing the error.

However, if you aren’t particularly technically inclined then you can take a complete backup of your site and download and install a fresh copy of WordPress. Replace the wp-config.php file and the wp-contents folder from the new installation with your existing ones. This will make sure you don’t lose your site’s content and configuration information while attempting to restore it.

After uploading the remaining site media to WordPress, clear your browser cache and try accessing your site.

If your site still displays the Error establishing a database connection error message then it might be time to give your hosting provider a call because the issue might be at their end. Be sure to tell them when you first encountered the error and what you did to try resolving it.

Conclusion

The Error establishing a database connection error can be a tough one to solve the first time you run into it. Thankfully, there are a few easy steps you can take to try troubleshooting and fixing it on your own:

  1. Troubleshoot the error using the wp-config.php
  2. Check your database login credentials and settings.
  3. Test your database username and password.
  4. Create a new database user.
  5. Reach out to your hosting provider and tell them everything you did to try and fix the error.

Have you ever run into the Error establishing a database connection error message? And if so, how did you tackle it? Let us know in the comments below!

Leave a Reply

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

close

Copy and paste this code to display the image on your site