How To Fix Error Establishing a Database Connection In WordPress

“Error Establishing a Database Connection”

It is not only the most common but also the most dreaded error in the WordPress community.

When this error shows up on your website, this is all you and your visitors can see.

Every second you waste fixing this error can lose you hundreds of subscribers and even loyal customers in many cases.

If you are seeing this error on your website . . .

. . . first off, stop panicking.

This is a very common error in the WordPress Community. And it doesn’t mean that you have lost your data. It might in some cases, but in most, it doesn’t.

I know you want this error gone as soon as possible, so without further ado, here’s everything we know about this error and how to fix it:

What Causes The “Error Establishing a Database Connection” Error In WordPress

In simple words, this error means your website’s code is not able to connect to the database server. Your database is where everything is stored including details about the users, posts and pages.

There are a lot of reasons why you might see this error screen.

The most common one is incorrect database configuration in your wp-config.php file. It can sometimes happen after installing a new update or restoring a backup.

If this is the case, fixing this error is as simple as replacing the incorrect config details with the correct ones.

But that’s not the only reason why you might be seeing this error.

Here are a few other reasons:

  1. Your database has been corrupted. This is bad news. It might mean you have lost some data.
  2. The MySQL server hosting your database is not responding. Maybe, your webhost is repairing the server or doing maintenance. In this case, there’s not much you can do other than wait for the MySQL server to get back online.
  3. There’s some error on the side of your webhost. If you have tried everything in this guide and you still can’t get your website up, it is probably an error on your website host’s side. Your best bet is to call your web host and ask them if they know what’s causing the problem.

Whatever the error maybe, we will guide you through fixing it in this guide.

Will I Lose Any Data (Posts, Pages, and Comments)?

This is the question that comes to mind when you see this error.

While in most cases this error simply means WordPress is not able to connect to the server, in many cases, it can mean that your WordPress database is corrupt.

If you are seeing this error just because WordPress can’t connect to the Database server, then you don’t have to worry about any kind of loss in data. This is caused due to misconfiguration and can be fixed within 10 minutes.

On the other hand, if your database is corrupt, it can mean that you have lost some data. But bare in mind, in most cases, it will only be configuration data or data that is required for WordPress to run properly.

In most cases, your content is completely safe and won’t be lost even if your database has been corrupted.

If you don’t know whether or not your database has been corrupted, don’t worry, I will guide you through it in the next section:

How To Fix The “Error Establishing a Database Connection” Error

Now, we will go through some of the reasons why you might be seeing this error and how to fix them.

Corrupt Database

The reason why you are seeing this error might be that your database has been corrupted. This isn’t the case most of the time. But you should always start by checking for it.

To check if your database has been corrupted, try navigating to the wp-admin directory of your website:

http://your-site.com/wp-admin/

If you see the same “Error Establishing a Database Connection” error, then your database is fine and you can move to the next error.

But if you are seeing an error that is different and looks something like “One or more database tables are unavailable. The database may need to be repaired,” then we have some work to do.

You will see this message when WordPress can’t find an important database table or some important data in the database.

If your database is corrupt, you will need to repair it. Here’s how:

First, log into your cPanel and open File Manager:
WordPress cPanel File Manager

Navigate to your WordPress installation directory:

WordPress installation directory

Now, find the wp-config.php file and add the following code to the bottom of it:

define ( ‘WP_ALLOW_REPAIR’ , true);

What this line of code will do is enable the Database Repair functionality that comes built-in in WordPress.

Once you have added this code to your wp-config file, visit the following URL:

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

Now, you will see the following screen:
Repair and Optimize WordPress Database
You can choose any one of the two options. But keep in mind, if you choose Repair and Optimize it will take a little bit more time than just Repair.

This repair page doesn’t require a user to login because if your database is corrupt, you won’t be able to login. So, once the repair process is over, be sure to remove the line of code you just added to your wp-config.php file.

If this doesn’t fix the error, move on to the next step:

Check For Incorrect Database Login Credentials

Most of the time, this error is caused by incorrect Database Login Credentials. If this is the case, it is only a matter of replacing the incorrect database login credentials with the correct one.

There are a lot of reasons why your database credentials may now have become incorrect. One of the most common reasons of all is YOU making changes to the database name or the login credentials. Another reason can be that you have moved from one web host to another and the old login credentials aren’t valid anymore.

To check if your database login credentials are incorrect, login to your cPanel, open File Manager:

WordPress cPanel File Manager

Navigate to your WordPress installation directory:

WordPress installation directory

Now, edit the wp-config.php file.

Now, scroll through the file, until you see code that looks something like this:

/** 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’ );

In the above code, there are four things that WordPress uses to access and make changes to your database:

  • DB_NAME: It is the name of your database. This might have now become incorrect if you changed the name of your database and forgot to update it in this code.
  • DB_USER: The username of the Database User that has access to the database.
  • DB_PASSWORD: This is the password of the Database User.
  • DB_HOST: This is the address where your MySQL Database Server lives. This can become incorrect when you move web hosts.

To check if these login credentials are invalid, create a new file called db_test.php in your WordPress installation directory and place the following code in it:

<?php

$dbTest = mysqli_connect(“DB_HOST”, “DB_USER”, “DB_PASSWORD”, “DB_NAME”);

if (mysqli_connect_errno()) {

die(“Invalid Database Login Credentials“);

}

echo “Database login credentials are valid!”;

In the above code, replace DB_HOST, DB_USER, DB_PASSWORD, and DB_NAME with the same credentials as in your wp-config file.

Now, visit:

http://your-website.com/db_test.php

If you see “Database login credentials are valid!,” then move on to the next step.

On the other hand, if you are seeing “Invalid Database Login Credentials,” then it means we have some work to do.

Instead of checking if your username and password are correct, we can just create a new MySQL User and allow that user access to the database.

To create a new user, in your cPanel, select “MySQL Databases”.

WordPress MySQL Databases

Now, scroll down to the Add New User section of the page:

MySQL Add New User

Choose a username and password for the new user we are creating. Take note of these credentials because we will put them in the wp-config.php file.

Now, click the Create User button.

Click the “Go Back” link once the user is created:

Successfully created MYSQL user

Now, Go to the Add User To Database section on the same page you created the new User:

Add User To Database

Select the new User’s username and the name of your WordPress Database and click Add User to add the new user to the database.

Now, replace the old database login credentials with the new ones and check your website.

If you are still seeing the same error, then it may be because your DB_HOST value is invalid. This is the address where your Database server lives. If WordPress doesn’t know this value, it cannot connect.

The DB_HOST value will vary from host to host. WordPress documentation has an extensive list of DB_HOST values for most web hosts.

If you are still seeing the same error, it is probably due to an error on your MySQL Server. In this case, your best bet to get it fixed is to call your web hosting provider.

Fix Corrupted WordPress Files

The PHP files that make up WordPress might have been corrupted on your website. If that is the case, you will have replace them with ones that aren’t. It might sound like a daunting task, but it’s not.

No, you don’t have to replace each individual file.

All you have to do is download the latest WordPress files and upload them to your server.

To do this visit the WordPress download page and click the Big Blue Download button:

Download WordPress

After downloading the file, extract it and then delete wp-config.php file and the whole wp-content Folder:

delete wp-config php file

Now, upload all of these files and folders to your WordPress installation directory through FTP and click Replace when asked to choose between the Skip and Replace option.

This will replace the old corrupted files with new ones.

If you are still seeing the same error, move on to the next step:

Restore Latest Backup

Your site was probably working a day or two or at least a week before today. If you restore your site’s backup of that day, you might get your site back up again.

If you are using a service such as VaultPress or if your web hosting plan comes with Automatic Backups, you can just click a button and restore your website’s files and database to their previous versions.

Note: Before you go on and restore your latest backup, keep in mind that you will lose all the data that was created between the date of backup and today. Moreover, DON’T take your web host’s word for their daily backups. Most daily backups created by web hosts are corrupt and might leave your website in an even worse state. Before restoring a backup, make a backup of the current state of your website. Read more about backup issues and the best solution.

Check If Your Site URL Has Changed In The Database

There are times when the URL of your website might get changed in the database. This can happen during an update or when making changes to WordPress settings.

You can go on and check your database table to see if this is the case or you can just run the following UPDATE SQL query to update the siteurl field in the WordPress database:

UPDATE wp_options SET option_value=’http://your-site.com’ WHERE option_name=’siteurl’;

Before you run the above SQL query in PHPMyAdmin or your MySQL console, be sure to replace your-site.com with your site’s URL and http with https if your site uses a SSL.

Nothing Worked? Here’s What To Do Next…

If none of the tips from this article have worked for you, the only way left to get your website up is to hire a professional WordPress Developer.

You can either hire a freelancer or you can go with one of these specialized service providers for WordPress:

These services will not only help you get your website up and running but will also provide you support for future problems and issues with WordPress.

Conclusion

As you can see, there are a lot of reasons why your WordPress site might show you this error. Most of the reasons can be fixed within 10 minutes or less.

Have you ever faced this issue?

If so, how did you fix it?

3 thoughts on “How To Fix Error Establishing a Database Connection In WordPress

  1. Hello Ankit,

    Error in establishing connection with database is really a major issue for bloggers. In many cases I have find that this issue arise due to poor hosting or poor server configuration. That’s why I always recommend bloggers to use reliable and good hosting provider. Thanks for sharing these helpful tips.

    Have a Great Day 🙂
    Vishwajeet

  2. Hello Ankit
    Database Error is seriously a big problem. Everything just fades away when you see that error. I have faced the issue and only I know how I recovered my site. That was really a bad day. This is indeed well explained post about the issue. Bookmarked for future reference.
    Thanks for sharing.

Leave a Comment