WordPress Tutorial

How to Fix Error Establishing a Database Connection on WordPress

In this guide, we’ll discuss various methods to fix Error establishing a database connection in WordPress. By the end, you’ll know all the causes and fixes for this WordPress error.

Overall, WordPress is a combination of two wonderful languages: PHP and MySQL. So whenever you choose to open a web page, it is built then at the moment. PHP code fires the queries to access the MySQL database in order to fetch all the required information which is needed to deliver a web page to the user. This way, a query is fired to write the post title, another one to fetch the author name and so on.

Thus, whenever you get an error message stating Error establishing a database connection, it simply means that due to some reasons or issues, your PHP code was not able to establish a connection with the MySql database and was not able to fetch all the details that are needed to build a page.Error establishing a database connection example

There are various reasons due to which your website might be experiencing trouble when connecting to your database. They can be broadly categorized into three major categories:

  1. Your login credentials for accessing the MySQL database are wrong. This is a no-brainer. If you have recently changed your database username and password, but have somehow not updated them in your PHP code, or your WordPress database uses a different set of credentials than those in your hosting account, your WordPress website will definitely experience trouble in connecting with your database and will throw an error.
  2. Problem with one or more plugins. At times, a problematic plugin might also be a root cause to why WordPress is facing trouble in establishing a connection.
  3. Your database server is down. There is a chance that your database server is down or is facing issues because of a few issues your web hosting company might be facing. Or, your website is suddenly facing an enormous amount of traffic. In such cases, your website will face issues in establishing a connection with your database, and in turn, would throw an error.

In this WordPress tutorial, you will learn how to find out the cause of the Error establishing a database connection in WordPress and how to fix it.

What you’ll need

Before you begin this guide you’ll need the following:

  • Access to control panel of the hosting account where WordPress is installed
  • Basic understanding of WordPress file structure

Step 1 — Determining where the error is occurring

First, what you should do is to make sure that you are getting the same error on both the front-end of the site yoursite.com, and the back-end of the site yoursite.com/wp-admin (replace yoursite.com with your WordPress homepage URL). If the error message is the same on both pages – Error establishing a database connection, then proceed to Step 2.

But if your website is throwing a slightly different error when you try to log in – One or more database tables are unavailable. The database may need to be repaired, then you might need to repair your database.

Thankfully, there is an inbuilt feature in WordPress which can help you to repair your database. To make use of this feature, all you need to have is access to the wp-config.php file which contains the installation settings and configuration of your WordPress files.

Look for the wp-config.php in your root WordPress directory.

wp-config.php file is located in the root WordPress file directory

Open the wp-config.php file and add the following line of code before /* That's all, stop editing! Happy blogging. */:

define( 'WP_ALLOW_REPAIR', true );

Save the changes. Now, you should be able to optimize and repair your database by navigating to yourwebsite.com/wp-admin/maint/repair.php (just replace yourwebsite.com with your actual URL).

Choose either option to perform the database repair.

A screen similar to the shown above should appear. There are two options – Repair Database and Repair and Optimize Database. You can choose any of them. However, if you choose to repair your database first, optimization will take longer. On the other hand, choosing to repair and optimize, both repairing and optimization will happen together.

It is important to note here that database repair page is not secure and anyone can access the URL without having to log in. Thus, as soon as you are done with the repairing process, make sure that you remove the line of code added to the wp-config.php file.

It is highly recommended to keep the wp-config.php file open and ensure that the error is resolved. If not, we will be using this file in the next step.

Step 2 — Checking database connection settings in wp-config.php

If the above step did not solve the problem, it is the time for you to check the database settings in your wp-config.php file.

In this step, we will be checking the login credentials of your database. You will need the wp-config.php file yet again. If you have closed it somehow after the last step, log in to the File Manager or FTP account, and look for the WordPress core files and locate your wp-config.php file. Open the file and check the provided login credentials of your database near the top of the file. It should look something like this:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'u694443746_aujev');

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

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

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

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

If you scroll down a little lower, you will also see a line which defines your MySQL database prefix:

$table_prefix = 'wp_';

WordPress needs the following information to access your database:

  1. The name of the database (DB_NAME)
  2. The username to login  (DB_USER)
  3. The password to login  (DB_PASSWORD)
  4. The database host (DB_HOST)
  5. The database prefix (table_prefix)

If any of these values are not correct, your website will face troubles in establishing the connection.

The best and most straightforward way to ensure the details are correct is to compare them with actual MySQL Database details. To do this, you will need to navigate to your hosting account control panel MySQL Databases section. Below steps show how to do this on Hostinger control panel and cPanel.

Hostinger control panelMySQL Database Section Hostinger

Once you’re in the MySQL Databases screen, find your List of Current MySQL Databases And Users section. It lists all the databases and users that are ready to be used for your website. The information we are seeking is located in MySQL Database, MySQL User and MySQL Host columns. Our aim is to match the values in these two columns with the DB_NAMEDB_USER and  DB_HOST values in wp-config.php file.

Check if MySQL details match wp-config.php

Simply make sure that values from wp-config.php match values listed in Hostinger MySQL databases section. If not, update your wp-config.php file with correct values and save changes.

Refresh your WordPress website. If you still get database connection error, it’s possible that password for your MySQL user password and password value in your wp-config.php mismatch. In this case,  you simply need to reset your MySQL user password. You can do this by pressing + icon near your MySQL database and pressing Change Password button.

Change MySQL Database PasswordNow update wp-config.php with the new password and refresh your WordPress site.

If nothing happened, chances are that WordPress uses an incorrect table prefix. You can check this by navigating to phpMyadmin and opening your WordPress database. Look at the table prefix and make sure it matches the value in wp-config.php file.

Fix error establishing a database connection in WordPress by checking the table prefix using phpMyadmin

If you still get the same MySQL connection error, proceed to Step 3.


cPanel MySQL Databases are located in Databases section.

Once you’re in the MySQL Databases screen, find your Current Databases section. It lists all the databases and users that are ready to be used for your website. The information we are seeking is located in Database and Privileged Users columns. Our aim is to match the values in these two columns with the DB_NAME and DB_USER values in wp-config.php file.

Match the wp-config.php values to the actual information in Current Databases screen.

You may not necessarily have to complete all the actions below to restore WordPress connectivity, therefore, as you complete each action, make sure to refresh your WordPress website to check if the error is gone.

Checking the database name value

Start with database name.

We’ll start with the database name value. If the Current Databases section does not list the database that is shown in wp-config.php file as DB_NAME value, the database is missing. This usually happens when a website is transferred to a new hosting provider and the database has not been fully moved. We recommend contacting the support team of your new hosting provider and request them to fully move/restore your WordPress database. This should entail creating a new database and filling its contents with your WordPress database backup.

Checking database login username value

Secondly, we check the Privileged Users column. In this example, we can quickly notice that the Privileged Users column lists a different username compared to the one shown in wp-config.php file – the latter missing a trailing ‘0’. Once we update the DB_USER value from somuvnet_wp67 to somuvnet_wp670, we then fix the incorrect username. Make sure to refresh your WordPress website to check if this has resolved the error.

Adding a missing privileged user

However, when performing WordPress move to other hosting provider or when installing WordPress manually, one may forget to add a user to the database. Such issue usually arises in control panels supporting advanced user privileges. In such case, you would see an empty Privileged Users field.

This database has no user added.

To fix this, we have to use Add User To Database section that is located at the bottom of the screen:

Bottom of the screen lets manage database users and add privileges.

  1. The first step is to locate the Current Users section and see if our DB_USER value from wp-config.php file is listed here.
  2. If it is listed, we choose that user in Add User To Database -> User drop-down field, choose the respective database in Database drop-down field and click Add. You will be asked to define privileges – select All Privileges, click Make Changes and then return by clicking Go Back.
  3. If it is not listed, we have to use Add New User section, which creates a MySQL username. Here you need to input a username or its suffix in the Username field and we recommend to use the Password Generator for the Pasword field.

    IMPORTANT: you should immediately update the DB_PASSWORD value in wp-config.php file with the generated new value so that it does not get lost in the process.

    Once the user is added (created), you need to perform the previous step (2.) to add it to the database.Once you perform the above, make sure to refresh your WordPress website to check if this has resolved the error.

Checking the database host value

If the above steps have not already resolved the error, there are two more pieces of information that could be incorrect – the login password or the database host. The database host value is usually provided together with the account details. We recommend double-checking this information with your hosting provider if it cannot be easily located in account setup emails or control panel. If you changed the database host value, refresh your WordPress website to check if this has resolved the error.

Checking the login password value

The login password can be quickly updated using the MySQL Databases->Current Users section in control panel.

  1. Locate the user for which you want to update the password in Users column and click Change Password in the respective row.
  2. Here you can either use the DB_PASSWORD value from wp-config.php file or generate a new password using Password Generator. If you choose to generate a new password, make sure to update the DB_PASSWORD value in wp-config.php file with the newly generated password.
  3. Click Change Password

This concludes all the checks performed through wp-config.php file. Make sure to refresh your WordPress website to check if the error has been resolved. If the error still remains, please proceed to the last step.

Checking the table prefix value

Last, but not least, make sure your table prefix is correct. Open your WordPress database using phpMyadmin and look if the values match the ones specified in wp-config.php file.

Checking WordPress database prefix in order to fix error establishing a database connection

Step 3  Disabling WordPress plugins 

If the above two steps didn’t fix the problems, it is time to check your WordPress plugins. The best way to do so is by deactivating all the plugins first, and checking if the website is back to normal after. If yes, a bad plugin might have been the real cause of the error. Once that is diagnosed, you may proceed to find out the exact plugin behind the error. Follow the following steps to disable WordPress plugins:

  1. Login to your account using an FTP client.
  2. Locate the WordPress core files, and look for the folder named wp-content. Access it. Find the folder called plugins.
  3. Rename that folder to something else like plugins-disabled. It’s just to fool WordPress that this is not the plugins folder.
    Disable Plugins for WordPress
  4. Check your website and see if this has fixed your error.

If yes, the problem was with one of the plugins. Now, to find out the real problematic plugin, all you need to do is:

  1. Rename plugins folder back to plugins and access it.
  2. Rename each plugin one by one to a different name just to ensure that WordPress doesn’t load that particular plugin. You might like to rename it as akismet-disabled, and so on.
  3. Check your website every time you disable a plugin, to see if that has fixed the problem.

However, if this didn’t bring your website back to normal, move onto the next step.

Step 4 — Replacing WordPress Core Files

IMPORTANT: Be sure to create a full backup of your website before proceeding with this step. You can find instructions how to download backup of your website here.

If the WordPress database error still persists, you can go for the nuclear option i.e. replacing your core WordPress files. This is because in some instances, corrupted or infected files can be preventing your site from establishing a database connection. In this situation, you should think about replacing these files with fresh, unadulterated copies of WordPress core files. Don’t sweat it too much – in all likelihood, you will be just fine at the end of this. But be sure to pursue this if all else fails.

To replace your core WordPress files follow these steps:

  1. To manually replace WordPress core files, first download the latest version of WordPress and unzip the folder.
  2. Delete your wp-includes and wp-admin directories on the server.
  3. Upload the new versions of these folders.

And that’s pretty much all there is to replacing core WordPress files. For more detailed instructions how to replace WordPress core files, see this tutorial. This should fix the ‘Error establishing a database connection’ issue you are encountering.

Step 5 — Contacting your hosting company

If you have followed all the troubleshooting steps mentioned above, but your website is still throwing the error – Error establishing a database connection message, chances are high that it will be an issue on your hosting service provider’s end. In such cases, it is highly recommended to contact your web hosting service provider support. Talk with them and provide all the necessary details and troubleshooting actions that you have taken.

But before you try to contact your hosting provider, there is one more thing you can do to figure out why you are getting the Error establishing a database connection. Sometimes, the database is offline at your hosting provider’s end.

This could happen due to a multitude of reasons, and isn’t as rare as you might think. If this is the case, you would have more information to convey to your hosting provider, which might speed up the solution process.

To check whether the database is offline, you need to run a rather simple PHP script. The script’s only job is to find out whether your site can form a connection with the database. We have provided the script below:

$servername = "mysql.hostinger.com";
$database = "u266072517_name";
$username = "u266072517_user";
$password = "buystuffpwd";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
echo "Connected successfully";

As you can see, the only details you need is your server name (usually in the format given above for hosting providers), the name of the specific database you are trying to connect to, your MySQL username and password. We have listed values from one of our other tutorials, which is also worth checking out, as it shows how to connect to a MySQL database through PHP.

Needless to say, you should replace these with values matching your MySQL connection details from Step 2.

Next, we create a new variable $conn, which will catch the return value of the mysqli_connect() method. If this variable is empty, we will get the message Connection failed: <error_name>. On the other hand, a successful connection will result in Connected successfully to be displayed on the screen.

This script should let you know whether the database is offline, which could be useful information for your web hosting provider.


This WordPress tutorial was intended to help you fix the Error establishing a database connection message appearing in WordPress. By applying appropriate steps we are now able to determine if the error lies either in a corrupted database or in the incorrect database connection values of the wp-config.php file. Eventually, there could be a chance that something is wrong with hosting provider but it pays to go through the above steps before contacting hosting support.

About the author

Linas L.

Linas started as a customer success agent and is now a full-stack web developer at Hostinger. He is passionate about making a positive impact on people by utilizing user-driven development techniques. As much as he enjoys coding and changing the world, he secretly dreams of becoming a rock star.


Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!