How to Fix the Error Establishing a Database Connection in WordPress

The title with the ominous words says it all “Error Establishing a Database Connection”. This means the site is down – a nightmare for every site owner. This is scary as you could loose subscribers, customers and ultimately revenue. In this article, we will go about understanding this error message. We will cover a list of scenarios which may be causing the error and how to troubleshoot them.

What Does “Error Establishing a Database Connection” actually mean?

In WordPress, all information is stored in a database. The information is retrieved using PHP when required and displayed. Every time a page is displayed, the associated data is fetched from the database and the page is constructed on the fly. The message “Error Establishing a Database Connection” is a straight forward message telling you that WordPress is unable to locate the database.

Are you wondering why don’t you see anything else? Well, that’s because all settings like your blog name, URL, users, widget settings, active theme, active plugins and so on are all stored in the database. Without the database there is no way to figure out what to display.

What’s causing the Error Establishing a Database Connection?

Though the message is a simple one, figuring out what is causing this error might not be so simple. There could be many reasons why your site is unable to communicate with the  database. Here we have shortlisted the most common errors causing this problem. We will go about how to troubleshoot each one of them.

  • The database is corrupted
  • Invalid database credentials – username / password / database name / database host
  • Core WordPress files are corrupted
  • Your host server is down

Before you proceed any further, make sure to have a backup in place. If you don’t, then visit your cPanel and create a complete backup with the provided tools. If you are having trouble, then contact your host support team. All hosts provide some sort of backup options. Make sure to backup all the files and the complete database. Go on now, create your backup, I’ll wait.

The database is corrupted

The first thing you should do is try and access your WordPress dashboard. Is the same error message getting displayed on both front end and back end? If not then it is an indication that the database is corrupted. Another indication of database corruption is when the site is available only on the front end or the back end but not both. You will receive a message informing you that one or more tables in the databases  are unavailable.

WordPress has a built-in tool just for this scenario. It tries and repairs the databases  if possible. This tool needs to be enabled before you can use it. To enable it, you will have to edit the WordPress configuration file “wp-config.php” which is available in the root directory. Add the following line of code to “wp-config.php”

define(‘WP_ALLOW_REPAIR’, true);

Once you have made the required changes to your configuration file, proceed to the following URL

You will land on a special page which has two options – repair the database and another option to repair and optimize the database.


Either of this options will repair the database, but optimizing will take time. I strongly recommend that you choose to repair database only as the primary goal should be to minimize downtime.

Remember that this page is intended to repair a damaged database. As users can’t log in without a working database, this page is insecure.  That mean’s everyone can access it. As soon as your database is repaired, remove the configuration setting to enable this page from “wp-config.php”

Invalid database credentials

WordPress relies on four vital pieces of information to communicate with a database

  • Database name
  • Database username
  • Database password
  • Database host

All this information is stored in “wp-config.php”.  If any on this information is not correct then your database will be unavailable. If you have changed your database password or username, then “wp-config.php” must be edited to reflect the changes.

If you have changed your host, you will need to update your database credentials with the ones provided by your new host. If you have changed your host, then double check the “host” option. Some hosting providers use “localhost”, while some others use an IP address. Then there are some which use an entirely different address.

Open your “wp-config.php” and locate “MySQL settings” which should be somewhere near the top

/** The name of the database for WordPress */

define(‘DB_NAME’, ‘database_name_here’);


/** MySQL database username */

define(‘DB_USER’, ‘username_here’);


/** MySQL database password */

define(‘DB_PASSWORD’, ‘password_here’);


/** MySQL hostname */

define(‘DB_HOST’, ‘localhost’);

Make sure you update any invalid credential. Check and then double check to be certain that there are no typos. Try and reload your site. If it’s still not loading, maybe the WordPress core files are corrupted.

Core WordPress files are corrupted

Many users have reported that they have managed to successfully get their site up and running by replacing the core WordPress files. I am not sure why this works but many suggest that this has something to do with file permissions or the core files getting corrupted somehow. Whatever the case, you need to take extreme caution before you proceed any further and make sure you have backed up everything before you replace anything!

Download a fresh copy of WordPress from the official site and extract the contents of the compressed file. You will need to delete “wp-config.php” to make sure that your database settings remain intact. You will also need to remove “wp-content” folder from the extracted contents folder. You don’t want to overwrite “wp-content” folder as stores all uploaded files, themes and plugins. Connect to your host using an FTP client and upload the files.If someone went crazy and made modifications to the core files, they will be lost as soon as you overwrite the core files.

Note: If someone went crazy and made modifications to the core files, they will be lost as soon as you overwrite the core files.

Your host server is down

If everything else was in vain, then the most likely culprit is you host server itself! If your site is drawing huge traffic and you are on a shared host, you will often receive this message. This happens because your server is unable to allocate enough resources to handle the requests. On a shared host, this can also happen when another site hosted on the same server is experiencing huge traffic and in result your site is down.

It’s really difficult to tell what’s going on in your host without any monitoring tools. Your best bet is to get on the phone or chat with them live and ask them what’s going on?

If you are experiencing a lot of headaches due to downtime of your shared server hosts, it’s best to move on to a managed WordPress hosting. Especially in cases where a WordPress powered site is all you want to run. Everything from the hardware to the software is fine-tuned to run WordPress as efficiently as possible. They even have a dedicated WordPress support team. On the down side, they might require you to disable and or remove a few plugins as they might conflict with the optimizations of the server. You will also have to compromise on the flexibility as you won’t be able to perform one click installation for any other application other than WordPress.

Testing MySQL connection

If you would like to manually test and see if your MySQL database is working properly, use the following PHP code snippet (provide your own data)

$mysqli = mysqli_init();

if ( ! $mysqli ) {

die( ‘mysqli init failed’ );


if ( ! $mysqli->real_connect( ‘host’, ‘username’, ‘password’, ‘database’ ) ) {

die( ‘Could not connect to MySQL server: ‘ . mysqli_connect_error() );


echo ‘Connected to MySQL server’;

If the above code snippet is working and you can connect to your database, then you need to check your “wp-config.php” file for any typos.

The easiest way to test your database configuration manually outside WordPress is using “PHPMyAdmin”. It’s a web based MySQL database manager, provided by all hosts. Login to your cPanel and open “PHPMyAdmin”.  If it fails to connect to the database, then MySQL server is not working properly. Maybe MySQL server is down or  your user does not have sufficient privileges to access the database. Contact your host support team and they will assist you.


The infamous “Error Establishing a Database Connection” is a tough nut to crack. Causes for this error can vary and so does the solutions. Have you managed to fix the error? Have you experienced this error and managed to fix it? How did you do it? Have you experienced some weird cases when this error occurred. Let us know in the comments section below.


Leave a Reply

Your email address will not be published. Required fields are marked *