How to Solve “Error Establishing Database Connection” in WordPress

When using WordPress on a regular basis, you should be prepared to handle a few common issues. One of these is the “Error Establishing Database Connection” issue.

You can’t remember doing anything with the database, but it still says there is an error in establishing the database connection. Luckily, there are some simple steps to help you solve this problem.

When this error happens, your website will look like this –

That’s a weird error message, right?

Continue reading to know the best ways to deal with the database connection problem.

The Main Reasons for Error in Database Connection

Before we get to know the solutions, let’s take a moment to identify the main reasons behind this awkward problem. Among the various reasons, here are the most common ones –

  1. Incorrect database credentials: There are separate username and password for the database. If these are incorrect, you will see this message.
  2. Corrupted database: There are several things that can corrupt a database. If this happens, you won’t be able to log in to the admin section as well.
  3. Problems in the MySQL server: This occurs when there is a problem with the MySQL server of your hosting provider.

Now that we know what causes the database connection errors, let’s find out how to deal with the problem.

Identifying the Error Type

As you have seen above, there could be two errors in your WordPress site leading to this issue. To find out which error your website has, try to login to the backend of your website. Unless you changed it, the login URL should be –

www.yoursite.com/wp-admin

Obviously, you need to put your own URL here. If you can log into the dashboard, the problem might be in the database credentials.

Alternatively, you may find out an error message like this when you are trying to access the wp-admin page –

One or more database tables are unavailable. The database may need to be repaired.

Or this –

Error establishing a database connection.

In that case, your database is corrupted. You need to repair it. Below, I will show you how to address both of these issues.

Dealing with Incorrect Database Credentials

If you can log into your website dashboard, it is likely that the problem is in the database credentials. You will find the credentials in the wp-config.php file of your WordPress installation. You need to check and make sure that the file contains the correct information.

To access the file, log into the cPanel of your hosting provider, and open up the file manager. Navigate to the WordPress installation of your website. You will find the wp-config.php file in the root folder of your WordPress installation.

Open the file in editing mode. The database credentials are usually located at the top of the file. You are looking for the codes similar to the following –

Among this information, you need to check out four things –

  1. Database name,
  2. Database username,
  3. Database password and
  4. Database host.

If any of the information is incorrect, you will have the error in database connection message. You can verify all this information by checking out the PHPMyAdmin, MySQL Databases, or other relevant feature of your hosting provider.

Make sure all of this information is correct on the wp-config.php file. Save the file to the root folder. Check your website to see if the problem is resolved.

Repairing a Corrupted Database

If you can’t login to the admin section, and having an error message while trying to access the wp-admin page, you have to take a different route. You need to repair the database. Luckily, it’s very easy to do that.

Open up the wp-config.php file from the root folder of your WordPress installation. Look for a line that says –

/* That's all, stop editing! Happy blogging. */

Found that line? Great.

Just above that line, add the following line of code –

define('WP_ALLOW_REPAIR', true);

This will enable the repair mode for the database. To start the repair process, go to this URL from your browser –

www.yoursite.com/wp-admin/maint/repair.php

Make sure that you have replaced www.yoursite.com with your own domain name. This will take you to a page like the following –

Click the “Repaid and Optimize Database” button. This will start the repairing process. Depending on the size of your website, it may take a while to finish the whole process.

Once the process is finished, check out your website to see if the problem is resolved. Regardless of the outcome, make sure you have removed the extra line of code from the wp-config.php file.

In case you are still facing the same issue, try the next solution.

Contacting Your Host

At this point, you are confident that you are using the correct database credentials, and the database is not corrupted. That means there might be a problem with the MySQL server or the hosting provider.

Contact your hosting provider’s customer support, provide them with all the necessary details, and request them to look into the issue. Don’t forget to mention the steps you have taken so far. They should be able to find the problem and help you restore the connection.

Final Words

As it happens with any technology, WordPress is also expected to experience a few problems. Error in establishing database connection is a typical example of these issues. Now that you have read the article, you know the best ways to deal with this problem.

Do you want any other help in managing a WordPress site? Let me know by leaving a comment below.

Miguel

I started this tech blog back in 2011 as a place to write down processes I took to fix my client systems and network. Now I write some tips and tricks to help others with the tech issues that one might encounter.

You may also like...