Tips to resolve an error establishing a database connection

Posted on 10th August 2020 by Samantha in Optimisation
5 minutes read
Back To Blog
error establishing a database connection
Share this article:

An ‘Error Establishing a Database Connection’ happens when your PHP code cannot get access to your MySQL database. That prevents it from getting all the resources needed to load or launch the site’s page. In this post, we’re going to look at the causes of an ‘Error Establishing a Database Connection’ for WordPress and the solutions.

Let’s start with the causes.

What causes an “error establishing a database connection” issue?

 There are several things that can contribute to this issue. Here are four of them:

  1. Corrupted Database. Installing a defective plugin or theme can lead to your database being corrupted.
  2. Corrupted WordPress Files.You could end up with this error message if you mess around with core WordPress files, which can accidentally alter the WordPress code.
  3. Unresponsive Host Server. If your web hosting provider server is damaged or down, it could result in an error message on your site.
  4. Incorrect Login Credentials.There is a specific set of information that your database uses. This information or login credentials includes a username and password that are distinct from your site. Without those credentials, your website is unable to access the database and entering it incorrectly, too many times, can lead to an error message.

6 Steps to resolve your database connection error

Let’s look at how to six the problem, in six quick and easy steps!

Step 1: Examine Your Database Credentials 

Incorrect login credential is the most common reason you get the error establishing database message. You can check the login credentials for your database by using the wp-config.php file, which you’ll usually find at the root of your WordPress site.

It has four crucial pieces of information:

  • Database name
  • Username
  • Database password
  • MySQL host name.

Here’s a sample of what you should look for:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

All four pieces of information must be correct if a successful connection is to happen.

Step 2: Verify Your Database Host File

After verifying that your database credential is correct, move on to the next step. Check if you are currently using the right database host details. This is typically your localhost. In some cases a hosting provider may choose to store databases on a separate server. To verify, contact your web hosting provider.

Step 3: Check Your Database Username and Password

To check if your username and password are correct and valid, a new PHP file has to be created.

  • Locate your WordPress installation’s root directory, create a file and then name it
    testdb.php, checkdb.php or something similar.
  • Next, replace localhost, root and password in the second line of the code with the information for your database.
  • The next step is to save the file and once that’s done, save your file and go to:
http://www.yoursite.com/testdb.php from your browser.
*Note: replace 'yoursite.com' with your website's name'

If the Database Host info is correct but your connection is not, then you can make new credentials. This can be done by creating  a new database user.

Step 4: Create a New Database User

  • To create a new database user, start by logging into your cPanel account.
  • After that, navigate to the MySQL databases area and select the Add a New User section. At this point, you’ll be prompted to make a new database user. This requires a new username and password.
  • Complete that and then assign the new database user to your website’s database by selecting the Add a User to a Database section. Click on the new database user’ name and then click on the Add button.
  • The final step is to open your website’s wp-config.php file in a code editor and substitute the old credentials with the new ones. Save the file and try opening your site again.

Step 5: Check If You Have Downtime

If all seems in order, it simply means there’s another issue. To identify it, let’s turn to the server. It’s possible that your MySQL server is down due to high traffic on your server.  To put things simpler, your Database Host isn’t capable of managing an influx of web visitors (primarily for shared hosting users).

The fastest way to review this is to phone your web host or send a ticket to support and check if your MySQL server is down. If you have other websites on the same server, now is a good time to check them. If they have the same error message, then there’s a common issue that is most likely linked to the server.

Step 6: When all else fails, contact your host

If you try all six of these steps and the problem is still not solved, contact your web hosting provider. Ask if there’s a server maintenance or upgrade in progress to secure your website. If they are, your problem is solved. Your website should be back online when they are done.

If not, your WordPress core files may be corrupted. At this point you should most definitely ask your webhost provider for assistance to do a complete backup of your site and install a new copy of WordPress.

Summary

Fixing an Error establishing a database connection issue can be a difficult the first time around. However, with the simple and straightforward steps outlined above, you should be well on your way to solving the problem.