How to Fix Error Establishing Database Connection in WordPress

Updated on Nov 17, 2022

Numerous factors can cause errors when connecting to your database. It could be corrupt files on your website, for example, from themes, plugins, or the WordPress core. Knowing how to resolve these issues can help reduce the time your website is unavailable.

In this article, we'll show you what a database connection error in WordPress looks like. Then we'll go over four steps to resolve the issue.

Why Does WordPress Need a Database?

A WordPress site is made up of two parts: files and a database.

When you download WordPress, you'll find static files and PHP code files, but no pages or posts, usernames, passwords, comments, or configuration data — all of that is stored in a MySQL database.

MySQL is a database server that manages databases, tables, and the data contained within them. WordPress communicates with MySQL over the network, authenticating with a username and password before sending a SQL query instructing the database.

Loading the WordPress home page, for example, executes a PHP file — typically the home.php or index.php file, depending on how the site is configured. The code connects to the database and retrieves the home page's content, inserting it into a template that creates an HTML document.

What is a WordPress Database Connection Error?

A connection error is WordPress's way of telling you that it cannot obtain the information required to construct the page you are attempting to load. WordPress requested data, and the database either ignored it or said no. To correct the error, we must first determine why MySQL is acting strangely.

There are several possible explanations:

  • Errors with network connections — The database cannot be reached over the network. This could be due to a database server overload or a network issue with the hosting provider. WordPress attempts to authenticate with credentials that the database does not recognize, so the request is denied.
  • WordPress files are corrupted — Corrupt files can result in invalid database requests or requests with incorrect credentials — a problem frequently caused by malware or a hacked WordPress installation. Corrupt databases are less common than corrupt files, but manual database editing and poorly coded plugins can cause database corruption. On the other hand, a corrupted database will typically display an error message such as "One or more databases are unavailable" rather than "Error Establishing a Database Connection."

In summary, WordPress database errors usually indicate that WordPress cannot connect to or authenticate with the database or that the database is unable to respond with the correct information.

Common Reasons for The Error Occurrence

So, what exactly causes this? So, here are a few common reasons. We'll go over each of these in greater detail so you can figure out how to fix them. This error is usually resolved in less than 15 minutes.

  • The most common problem is that your database login information is incorrect. Your WordPress site requires a separate login to connect to its MySQL database.
  • Your database has become corrupted. Because there are so many moving parts with themes, plugins, and users constantly deleting and installing them, databases can become corrupted. This could be due to a missing or corrupted table, or it could be that some information was accidentally deleted.
  • Your WordPress installation may contain corrupt files. This can also happen as a result of hackers.
  • Problems with your database server on the web host's end, Several things could be wrong, such as overloading the database due to a traffic spike or becoming unresponsive due to too many concurrent connections. This is quite common with shared hosts because they use the same resources for many users on the same servers.

How to Fix Database Connection Errors in WordPress

Because determining the cause of the error can be difficult, the best approach is to try the most likely fixes until your WordPress site resumes normal operation.

Verify Database Credentials in cPanel

WordPress uses four pieces of information to talk to MySQL: 

  • The database name;
  • The database server’s hostname;
  • A username;
  • A password;

They are stored as variables in the wp-config.php file in the WordPress installation’s root directory, and they look like this:

/** MySQL database username */ 
define(‘DB_USER’, ‘xxxxxx’);

Comparing the values listed in the wp-config.php file to those listed in the database is the first step in verifying your database credentials. In cPanel, open phpMyAdmin and navigate to the WordPress database in the left menu. Check that the database name and the DB NAME variable in wp-config.php are the same. Check that the hostname matches the name in the database server section, which will almost certainly be "localhost."

Next, review the "Users" section of the database entry to ensure that the database's username and password match the values used by WordPress. You can change the values in the database to match those in wp-config.php, or you can change the values in wp-config.php directly using the HTML Editor in the cPanel File Manager.

If the database credentials check out but the error persists, it’s time to consider file or database corruption.

Repair the Database

Corrupt files are more likely, but minor database corruption is easier and less dangerous to repair, so it's worth a shot first. There are two approaches to WordPress database repair. The first step is to navigate to your database, select all of the tables, and then click on the "repair table" item in the dropdown menu.

The second method employs WordPress's built-in database repair mode, which you can enable by adding the following line to the wp-config.php file:

define(‘WP_ALLOW_REPAIR’, true);

Next, navigate to this URL:

Select the repair option and wait for WordPress to do its thing. It may take some time, but it should resolve the most common database corruption issues. When you're finished, remove the line of code you added to wp-config.php. Anyone can access the database repair URL, and you don't want malicious visitors to use it.

Replace Corrupt WordPress Files

WordPress files can become corrupted due to hardware issues, malware, or user error. The goal is to replace corrupt files with new ones freshly downloaded from while keeping your site's plugins and themes in place.

Download WordPress, unzip it, and go to the "wordpress" directory. Delete the wp-content folder, as well as the files wp-config.php and wp-config-sample.php.

Open the root folder of the affected WordPress site in cPanel File Manager and upload all of the remaining files and folders from your local copy of WordPress. If cPanel prompts you to overwrite existing files, answer yes.

WordPress's core files will be replaced with brand-new ones. If this does not resolve the database error, you may want to replace wp-config.php as well, in which case you will need to edit the new version to include the correct database credentials, as discussed above.

If none of these methods work, it's time to restore the WordPress site—both the files and the database—from a recent backup taken before the connection error occurred.


When you first see "Error Establishing a Database Connection" on your WordPress site, it can be frightening. Still, hopefully, you now understand what the error means and how to restore harmony between WordPress and its database.

We hope you find this article useful. Discover more about FastCloud - the top-rated Hosting Solutions for personal and small business websites in four consecutive years by the HostAdvice Community!

WordPress Hosting

  • Free WordPress Installation
  • 24/7 WordPress Support
  • Free Domain Transfer
  • Hack-free Protection
  • Fast SSD Storage
  • Free WordPress Transfer
  • Free CloudFlare CDN
  • Immediate Activation
View More