How to Fix “Error Establishing a Database Connection” in WordPress

Last modified: January 13, 2020

Cloudways

One of the biggest headaches for a WordPress developer or WordPress webmaster is trying to access a WordPress website and seeing the “Error establishing a Database Connection” error. The annoyance is even enhanced if it’s a live site that you randomly tried to access for maintenance. Every second that your site is down and can’t accommodate potential clients or customers, you are literally losing money.

So, what exactly does this WordPress database error mean and how can it be fixed?

“Error Establishing a Database Connection” Meaning

How to Fix the Error Establishing a Database Connection

 

Sites in the past used to be built statically, meaning each page on the internet was built with a file (PHP, ASP, etc.). Nowadays using CMS’ (content management systems) the site is built using server side language (and files), HTML & CSS which builds the structure and design of the site and the entire content is saved in a database using MYSQL. Whenever you read a post on a WordPress site for example, you are actually reading text that was pulled in from the database.

The “Error Establishing a Database Connection” basically means that the PHP side was unable to connect the database and serve its content. Why is that? That’s what we are here to find out.

Show More

* This button will show the rest of the post and open up an offer from a vendor

Why are We Seeing This Wordpress Database Error?

There are several reasons why you might be seeing this error but more times than none they will relate to one of the following categories:

  1. Incorrect credentials for your database – there are several credentials needed in order to connect to a MYSQL database. The most common reason for the error is using the wrong credentials.
  2. Hosting service issue – It can happen. Sometimes hosting companies’ servers crash. If you are seeing the “Error Establishing a Database Connection” error, it might not even be your fault. It could directly relate to your hosting company.
  3. Corrupt database – Inexperienced WordPress users tend to corrupt their database due to several reasons. If this is the case, the site will not be able to connect to the database until it’s fixed.

So how do you know which one of the above is the cause of the error on your site? Let’s begin…

1. Check your Database Credentials

In our experience this is the most common reason for the “Error Establishing a Database Connection” error. You may have typed the user or password wrong.

While this doesn’t usually happen on a live site, you may have accidentally made a change in the wp-config.php file, or even replaced it using your FTP. Another common reason this can happen is changing hosting companies.

In order to fix the issue:

  1. Make sure you have the correct credentials you got from your WordPress hosting company
  2. Head over to the  wp-config.php file and scroll down till you see something similar to this
    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define('DB_NAME', 'db_name');
    
    /** MySQL database username */
    define('DB_USER', 'username');
    
    /** MySQL database password */
    define('DB_PASSWORD', 'password');
    
    /** MySQL hostname */
    define('DB_HOST', 'localhost');
    
    /** Database Charset to use in creating database tables. */
    define('DB_CHARSET', 'utf8');
    
    /** The Database Collate type. Don't change this if in doubt. */
    define('DB_COLLATE', '');
    
  3. Switch the user, password or DB_NAME if needed
  4. Upload the altered file
  5. Check if the site is working now

2. Check if the Database is Corrupt

Do you have a similar error message but not quite? “One or more database tables are unavailable. The database may need to be repaired.”

If you have a feeling that you may have corrupted your database by installing a bad plugin, making some manual changes in phpMyAdmin or any other various reasons, this may be the solution for you.

Repair Corrupt WordPress Database

WordPress has a built-in database repair tool that needs to be enabled via the  wp-config.phpfile.

So, head over to wp-config.phpagain and add the following line to the bottom of the file:

define( 'WP_ALLOW_REPAIR', true );

Now head to the repair URL at: //example.com/wp-admin/maint/repair.php – switch the example.com into your site’s URL of course.

Now you should see this:

WP_ALLOW_REPAIR

As you can see, there are 2 options here and they are both a good idea however, note that it could take a while to finish and they will put the site into maintenance mode while they are running.

It’s very important to remember to remove the line of code you added, since the repair URL is not secure, anyone can access it without being logged in.

 

3.Hosting Down?

If the 2 above options didn’t work, check with your hosting company, something may be wrong on their end. When you contact them, explain exactly what you have done to troubleshoot the issue, that should help them pinpoint the issue and get your site back up as soon as possible.

Final Thoughts

The “Error Establishing a Database Connection” error in WordPress is undoubtedly one of the most annoying WordPress errors out there, however it’s not that hard to solve usually. Just use the tips on this post and you should be up and running within no time.

Recap:
Save 4 Later
Email liked links to yourself