How to Fix Error Establishing a Database Connection in WordPress?

Error Establishing a Database Connection

You open browser and type your website URL. You’re unable to access the WordPress website but present with a message “ Error establishing a database connection ”.

Don’t panic, there are ways to fix this error and get back your website to normal in a few steps.

The error establishing database connection with WordPress is the most common and scariest error for WordPress owners. 

In this article we will explore the reasons for error establishing a database connection and possible solutions to fix this error.

Let’s see how your WordPress site works from back-end.

Why you get this error?

Any WordPress based website uses programming language PHP and MySQL database for data storage. Whenever any visitor comes to your site WordPress pulls out information from backend database usually MySQL with PHP code and then display information you requested.


Sometimes, your WordPress gets into trouble connecting to the back-end database for various reasons. That’s when WordPress throws out default error information with “Error Establishing a Database Connection” on your site.

Let’s look at the possible reasons for this specific WordPress error.

  1. Your database credentials are wrong or changed.

  2. Database corrupted.

  3. Database server is down.

  4. Your database server unable to handle traffic the site has been receiving it.

Having said that, let's see how you can fix error "Error Establishing a Database Connection" on your own by following simple steps.

Let's dive in!

Error Confirmation and Database Repair

Before fixing the error, let’s see if you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin). If the error message is the same on both pages “Error establishing a database connection”, then proceed to the next step.

If you see a different error on the /wp-admin page for example “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

To repair database, you need to edit WordPress configuration file wp-config.php. Locate wp-config.php file in your WordPress main folder and add following entry at the end the of file.

define( ‘WP_ALLOW_REPAIR’, true );

The above entry enables the WordPress to launch database repair wizard when you load URL (replace “” with your actual URL)

WordPress Database Repair

You will be presented with options. We suggest you to select the “Repair and Optimize Database” option. This repair page can be accessed without the need to login, so it creates a security issue.

Once the repair and optimization has finished, remove the line of code you added to wp-config.php file.

Hope above method fixes your problem related to error “One or more database tables are unavailable. The database may need to be repaired”.

If not move onto next step to fix error “Error establishing a database connection”.

Fix database login credentials in wp-config file

The most important file in WordPress is wp-config file. It contains all the high level configuration settings, database connections details and etc.

When you make any changes to your WordPress database, modify database user and passwords, you must update latest logins in wp-config also.

Otherwise you will get error ““Error establishing a database connection” if the database and it’s user details not in sync with wp-config file.

The following database settings in wp-config file are very important to run your WordPress based site smoothly. So, make sure to keep it up to date.

// ** MySQL settings - You can get this info from your web host ** //
// ** 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' );

If everything in wp-config file is correct, then you can conclude that something wrong on the server end.

If you are using multiple web sites with same hosting company, you can open other sites and see if you receive same error or not.

In fact, you can check to see if the problem is with web hosting by creating a simple php page with below code and executing it.

$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Databse Could not connect: ' . mysql_error());
echo 'Connected successfully';

When you see error “Database Could not connect” , It’s time to contact your web hosting provider for help.

Other Possible Fixes

The possible reasons could be, 1) database server is down and not responding. 2) Your site getting heavy traffic and database server is unable to handle your site requests.

So get in touch with web host to fix above two issue. They should be able to help you.

We would love to hear your database connection error stories.

  • by Raju
  • |
  • May 15, 2017
Click Here to Leave a Comment Below

Leave a Comment:

Get a Free Hosting Trial and Test Our Services. It Costs Nothing! Get it Here!