How to Quickly Fix “error establishing a database connection” in WordPress

“error establishing a database connection” is probably one of the most common and scariest errors that WordPress users may encounter. It is definitely closely related to the White Screen of Death (WSOD). This error means that your site is no longer communicating or accessible to the WordPress database and the entire site is down.

For such an error, webmasters should never take things lightly and should immediately try to fix this problem that directly affects traffic, SEO, etc. But don’t worry, today we will discuss some common scenarios that lead to this error and the corresponding solutions.

What is “error establishing a database connection”?

All information on a WordPress site, such as publishing data, page data, meta information, plugin settings, login information, etc., is stored in a MySQL database. The only data that is not stored is media content, such as images, and theme/plugin/core files, such as index.php, wp-login.php, etc. When someone visits your site, PHP executes code on the page and queries the information from the database, which is then displayed to the visitor’s browser.

If for some reason this does not work properly, there will be an error establishing the database connection, as shown below. The entire page goes blank because the connection does not work properly and no data can be retrieved to render the page. This not only breaks the front end of the site, but also makes it impossible to access the WordPress dashboard.

However, visitors may not immediately see this error on the front-end. That’s because the site cache is still available for access until the cache expires.

How to Fix “error establishing a database connection” in WordPress

Common scenarios that lead to “error establishing a database connection”

The most common problem is incorrect database login credentials

The WordPress site needs to use the database related login information to connect to its corresponding MySQL database.

The database is corrupted.

Since themes, plugins can be installed and uninstalled so much, the constant removal and installation over the years can sometimes lead to database corruption. This may be due to missing or individually corrupted tables, or some information may have been accidentally deleted.

READ ALSO:   How To Fix Error 1020 On Your Website ? 4 Solutions To Fix Error 1020

There may be corrupted files in the WordPress installation.

Database server problems

There can be many problems on the web host side, such as the database being overloaded due to traffic spikes or not responding due to too many concurrent connections. In fact, with shared hosting, this is quite common, as many sites share the resources of one host.

How to fix “Error establishing a database connection”

Before you begin troubleshooting the error, it is recommended that you first make a backup of your database, as many of the following suggestions involve manipulating the information in your database. Regardless of your skill level, you should always perform a backup before attempting to modify your WordPress website database.

You can use a WordPress backup plugin such as VaultPress or WP Time Capsule to back up files and databases.

If you are a cPanel user, it’s very easy to backup your website files and database. Go to cPanel dashboard, find “Backup” in the menu, then backup the entire database and website files manually.

How to Fix “error establishing a database connection” in WordPress

Of course, you can also perform a manual backup of your website files by accessing the server through tools such as SFTP, and a manual backup of your MySQL database through phpMyAdmin. Once the backup is successful, it is time to start troubleshooting the “Error establishing a database connection” problem.

Check the database login credentials

The first thing to do is to check to make sure that the database login credentials for the WordPress configuration file are correct. This is by far the most common cause of “Error establishing a database connection” errors, especially after migrating a site to a new hosting provider. The connection details for a WordPress site are stored in the wp-config.php file, which is usually located in the root directory.

It contains four important pieces of information that must be correct for the connection to the database to be successful.

Name of the database

// ** MySQL settings ** // 
/** The name of the database for WordPress */ 
define('DB_NAME', 'xxxxxx');

Database username

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

Database password

/** MySQL database password */ 
define('DB_PASSWORD', 'xxxxxxxxx');

MySQL hostname

/** MySQL hostname */ 
define('DB_HOST', 'localhost');

To access the wp-config.php file, you can connect to the site via SFTP and browse to the site’s root directory. Or, if you are using cPanel, you can go to the cPanel dashboard and click on “File manager”, browse to the site’s root directory, and then right-click to edit the file.

READ ALSO:   How to Clear Cache in WordPress (Ultimate Guideline)

The following is an example of what the wp-config.php file looks like.

How to Fix “error establishing a database connection” in WordPress

Check the database credentials in the cPanel panel

The first thing to check is the database name. For this, you need to go to the cPanel control panel and log into phpMyAdmin .

Find the name of the database corresponding to your website in the left menu. Ignore the “information_schema” database, as this is the database used by the host. Then, compare that name with the DB_NAME value in the wp-config.php file. If they match, then this is not a problem. If they do not match, then you need to update the wp-config.php file.

How to Fix “error establishing a database connection” in WordPress

You can also verify that this is the correct database by making sure that the database contains the URL of your site. Simply click on the database and then click on the wp_options table (for security purposes, this name may be slightly different, e.g. wpxx_options). At the top of the table, you will see the values for the site URL and name. If these match your current site, this is the database for your site.

How to Fix “error establishing a database connection” in WordPress

If the database name is correct, but you still get an error when establishing the database connection, then you also need to check the username and password. To do this, you need to create a new PHP file in the root directory of your WordPress site (name it randomly, e.g. checkdb.php) and enter the following code, which needs to be replaced with the values of db_user and db_password using the values from the wp-config.php file.

$test = mysqli_connect(‘localhost’, ‘db_user’, ‘db_password’);
if (!$test) {
die(‘MySQL Error: ‘ . mysqli_error());
echo ‘Database connection is working properly!’;

Visit with your browser. If you receive “MySQL Error: Access denied”, your username or password is incorrect and you will need to Continue to the next step to reset the credentials.

READ ALSO:   How to Solve WordPress ERR_TOO_MANY_REDIRECTS Errors
How to Fix “error establishing a database connection” in WordPress

However, if it shows “Database connection is working properly.”, then the database username and password are correct. Make sure to delete this file when you are done testing.

How to Fix “error establishing a database connection” in WordPress

If you are sure that the database user name and password are incorrect, you will need to reset them. In the cPanel, click on “MySQL Database”.

How to Fix “error establishing a database connection” in WordPress

Scroll down and create a new MySQL user. Try to select a unique username and password (you can use the provided password generator tool to generate a strong password) so that it is not easily guessed. Then click on “Create User”. Alternatively, you can change the password for the current database user on this screen.

How to Fix “error establishing a database connection” in WordPress

Then scroll down and add the new user to the database. The next screen will ask you for the privileges you want to assign, select “All Privileges”

How to Fix “error establishing a database connection” in WordPress

Then use these new credentials and update to the wp-config.php file (update the DB_USER and DB_PASSWORD values). At this point, you can also verify that the database credential information is correct using the test file mentioned earlier. However, if this fails to resolve the problem, it may be that the host name (DB_HOST) is wrong. Some hosts use different values, you can refer to some common database host values list, or check the host related documentation and contact the hosting provider to provide. Some people may also use instead of localhost.


As you can see, there are many ways to fix the “Error establishing a database connection” problem in WordPress. The most common error is that the database credentials set in the wp-config.php file are incorrect, so checking to make sure they are correct should be the first step. Regardless of what is causing the “error establishing a database connection”, hopefully the above steps will help you get your website back up and running. Remember, always make a backup of your site to restore it if needed.

5 Best Online Alarm Clock to Start Off Your Morning
How to Quickly Fix WordPress 500 Internal Server Error