How to Fix “Error Establishing a Database Connection” in WordPress

By September 26, 2018 October 14th, 2018 WordPress

One of the most common errors that WordPress can throw at you is “Error in Establishing a Database Connection”. It’s frustrating especially when you haven’t done anything, yet you found yourself in the midst of sean arching how to fix it. Honestly, running into this error is almost inevitable, but it can be fixed, so you really have nothing to worry about. Detecting and fixing it would last only for less than an hour as long as you know what you are doing-ish.

To help you get out of trouble, we have compiled a number of solution whenever you experience error establishing a database connection in WordPress.

What causes this error?

There are a few reasons why WordPress can’t successfully establish a database:

1. The database login credentials are wrong or have been changed
2. The database server is unresponsive
3. The database has been corrupted

The above are the most common reasons, but sometimes it’s something else… time to do some testing.

Is the WordPress admin area experiencing the error too?

If you are experiencing an error in establishing a database connection, the first thing that you should do is to check that both the front-end and the back-end (/wp-admin/) of the site are experiencing the same error. If both the front-end and back-end are experiencing the same problem, it is safe to proceed to the next step. If the back-end and the front-end are not experiencing the same problem, say, for example, one of them is getting a different error, you can’t proceed to the next step unless the other error is fixed.

To be more specific, the WordPress admin is experiencing the error in establishing a database connection and you are experiencing a different error which shows something like this “One or more database tables are unavailable. The database may need to be repaired”, then you must repair your database first.
This is done by adding the following code before the ‘That’s all, stop editing! Happy blogging’ line in your wp-config.php file.

define('WP_ALLOW_REPAIR', true);

Once you have successfully repaired the database, you can check out the setting by visiting

Definitely remove the line once it’s done.

Checking the WP-Config file

The most important file in the entire process of installing WordPress is the WP-Config.php as this specifies the details for WordPress for you to connect to the database.

If you’ve changed the root or database password, you have to change these values too.

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

Keep in mind that there are instances wherein the DB_Host value is not the localhost. Each of them is unique and greatly depends on the host.

Other people fix this error by replacing localhost with the IP which is very common when you are using WordPress on a local environment. If you have ensured that everything in the file is correct but you haven’t still established a database connection, then the problem is probably on the servers’ end.

Check your Web Hosting Provider

Error establishing database connection is most common when your site is loaded with traffic as your host server doesn’t have the capacity to handle the load. Due to the overloading traffic, your site might slow down so the best thing to do is to contact your hosting provider and find out if the server is responsive or not.

If in doubt, just get in touch with your web host, they should be able to help you out!