How to Fix Database Connection Error in WordPress

How to Fix Database Connection Error in WordPress

If you are a WordPress user, no matter a beginner or an experienced developer, you are bound to encounter a lot of errors that may take your website down. Previously, we have listed the top 10 common WordPress errors in this page. Among them, the database connection error is surely a scary one. After all, once you are in trouble with it, you cannot get some hints of what is wrong, thus feel hard to fix this issue.

In this post, we’d like to come out a detailed tutorial about this complicated error, telling you what it is and how to fix database connection error in WordPress.

What Does This Error Mean?

To explain in simple words, this error means that your WordPress site cannot connect with the database to retrieve the information needed for the loading and display of the webpages. What you and your readers can check are not the decent contents, but a blank page containing the below error message.

Database Connection Error

If you want to get the inner understanding, you firstly need to break WordPress down into two components – PHP and MySQL. The former one is in charge of all the WordPress functions such as posting blog posts, while the latter one is used for data storage. When your webpage is loaded, PHP will fetch and retrieve the related data from the MySQL database for the display of content. However, once PHP fails to enter the database, it means you are encountering this WordPress error.

Now, you might be wondering what causes this error and how to fix it. In the following, we have listed all the common reasons and their corresponding resolutions.

Reason 1 – The Server of Your Database is Down

When encountering this database error, you firstly need to contact your web host to figure out whether the database server is down due to the malfunction and over-consumption of server resources.

Especially, if you are using a shared WordPress hosting that requires you to share a single server machine with multiple users, the server is likely to be down, for your neighborhoods might overload the machine. Even, if your website is under a traffic upsurge, you may encounter the database connection issue as you are overloading the database server.


If your site is hosted using a web hosting package and the website is under the fast growing, you can consider upgrading your shared plan to the VPS one, or migrate your site to a truly reliable WordPress hosting provider.

If your site is a small one with no sudden traffic spike, you need to talk to your web host for reporting your bad situation. Generally, all the quality web hosts will resolve their server issues within a short time.

Reason 2 – Your Database is Corrupted

This error also may happen due to your corrupted database. To figure out this, you can consider the following situations.

  • If both the back-end and the front-end of your site showcase the database connection error, you can pass this part and proceed for the next step.
  • If your website front-end works but the back-end gives the error message for the lost of some database tables, your database is surely corrupted and needs to be repaired.


To repair your database, you need to go through three steps. Firstly, you should open your wp-config.php file and add the following line of code at the bottom of the whole file.

Allow Repair Code

With the inserting of the coding stuff, you do not need to log into your phpMyAdmin for manual repairing, but can access a special database repair page.

Repair Page

In the next step, you should click either button displayed in the above screenshot. The former one only achieves the repair purpose while the latter one also optimizes your database.

As for the last step, you need to delete the line of code you have added into your WordPress configuration file previously. After all, the repair page is open to the public. Anyone can access it without the login requirement. Therefore, if you remain this insecure page, you just leave some website vulnerabilities for hackers.

Reason 3 – You Have Some Corrupted Files in Your WordPress Site

In some cases reported by WordPress users, this error also can be caused due the inserting of corrupted WordPress core files. If you can find the problematic files, you just need to delete them. Otherwise, you can re-upload the core files of WordPress.


Before doing anything, you need to make sure that you have backed up your WordPress site. Then, you should download the latest version of WordPress and unzip it on your desktop. Here, do not forget to delete the wp-config.php file and the wp-contents folder of the fresh copy.

The former one contains all the configuration information while the latter one stores the files of your uploaded items, themes and plugins. In this case, the deleting practice can ensure you that you will never accidentally break all the current website settings and will not lose your media, templates and plugins.

Now, you can upload the rest core files to the root folder of your current WordPress site using either FTP or File Manager to replace the initial copy. Then, you can reload your website to figure out whether the bad files are removed and whether the database connection error is fixed.

Reason 4 – The Login Credential for Your Database is Wrong

This is the common reason for causing this error. The login credential includes the database name, hostname, username and password. If one of them is changed or there are some improper typos and spaces, you will be presented with an error message for the database connection.

To check the login credential, you firstly need to open the wp-config.php file and look for the lines of coding stuff showcased in the following. The 4 lines refer to the name of the database, the username and password for the successful login and your database host.

Login Credential in wp-config.php

Now, to figure out whether these values are correct or not, you can use the phpMyAdmin. Here, let’s start with the database name.

Resolution for Database Name

In the very beginning, you need to log into your phpMyAdmin account via the control panel; then, click the Database tab in the navigation bar. Now, you will be presented with a list of databases you have created previously. Just find the database name that appears in your wp-config.php file.

Find Database Name

If you can find it, you also need to click the database and browse the wp_options table. After finding the right information for your website URL, name, admin email and many other settings from this table, you can be ensured that you have the correct database name in your WordPress configuration file.

wp_option Table

However, if you cannot find it, you firstly need to contact your web host to figure out whether there are some problems with their server. If there are no server issues, you should restore your database using the backup file.

Restore Database

Now, you can repeat the above-mentioned steps to find out the right database name. Then, edit your wp-config.php file with the correct information.

Resolution for Username, Password and Database Host

You can check these components in one time simply using some lines of codes. Here, you firstly need to upload a PHP file into the root folder of your WordPress site, which is the same folder that contains the wp-config.php file. You can name it as checklogin.php or anything else, and enter the following coding stuff into it.

Check Database Username

Here, do not forget to replace the “local host”, “password” and “root” with your true login credential found in the wp-config.php file.

After saving the changes for this new file, you can open your browser and navigate to the URL of this file. In our example, it is “”. If you get a success notification of “Database Connection Working”, your login credential is right.

However, if you get the error message, you need to practice the following steps.

  • Figure out whether your database host name is right. You can do this by asking your hosting provider or checking these values.
  • Create a new user and password for your database. Here, in the last step when adding this user to your database, you need to choose the right database name showcased in your wp-config.php file.

Add User to Database

Now, you can go back to the wp-config.php file and update it with the new information for hostname, username and password. Then, try to access your website to figure out whether the error is fixed.