How to Fix the Error Establishing a Database Connection in WordPress

How to Fix the Error Establishing a Database Connection in WordPress

Error Establishing a Database Connection, if you use the internet for a while now, then you might have seen this error few times.

It is one of those errors that could occur for many reasons. As a WordPress beginner, this could be very frustrating.

Specifically, when you have not made any changes to your WordPress site.

I faced this issues today on one of my sites. It took me over 25 minutes to find and resolve the problem.

While I was searching for possible causes;

I found that there is not a single article on the web that covers everything about this issue.

That’s When I decided to write this all in one article.

In this article, You’ll learn how to fix the error establishing a database connection in WordPress. I’ve compiled a list of solutions in one place.

Note: Before making any changes to your database, make sure you have created multiple backups.

Few Reasons You Get “Fixing the Error Establishing a Database Connection” Error?

As the error itself is self-explanatory, WordPress is not able to connect to your site’s database.

Now there are so many reasons why it’s unable to connect to the database, It can be the database credentials specified in wp-config.php file is incorrect.

  • You may have changed the password for root user.
  • Maybe your website’s database is not responding at the moment.
  • It can be due to the website’s database has been corrupted.

In my 5 years of experience, most of the time it comes out that the issue is from the server end. Also there are many other issues which can lead to this error.

Let me show you how we can proceed with troubleshooting this error.

Does this error affect your WordPress admin dashboard /wp-admin/ as well?

The Answer is yes,

But you should verify if you are getting the same error on the backend ( and Front End ( of your website.

If you are getting same error “Error establishing a database connection” on both the pages, then you should proceed to the next step…z

Just in case, if you are getting any other error while accessing your wp-admin page, Example;

“One or more database tables are unavailable. The database may need to be repaired”

Then we need to perform repair on your WordPress database by following below guide.

You can repair your WordPress databases by editing the wp-congif.php file and add below mentioned code to it.

define('WP_ALLOW_REPAIR', true);

Note: Just add this line of code in your wp-config.php file. Right above “That’s all, stop editing! Happy blogging” line.

After adding the code in your wp-config.php file, to access the database repair page visit this page:

Replace with your actual domain name.

how to fix the error establishing a database connection

You don’t need to be logged in for this options to work. when the define is set in the wp-config.php file.

Because the define is created to repair the corrupted database, most of the time when the database is corrupt user won’t be able to log in.


After, you perform database optimization & database repair, please remember to remove the line of code from your wp-config.php file.

If repairing the database does not fix the issue for you or for some reason you’re unable to perform repair database, then keep reading. You may find other tested solutions which may fix your problem.

You may find other tested solutions which may fix your problem.

Checking the configuration of WP-Config file and any errors in it.

This is the single and very important file in your WordPress Installation. wp-config.php is the file which contains all your configurations details to connect to your database.

For some reason you changed your Root password or any database user’s password, then you need to update the file with new credentials else you will not able to connect to your database.

First; you should check the wp-config.php file is correct and unaltered, just check your file if it has below-mentioned code in it.

You should check the wp-config.php file is correct and unaltered, just check your file if it has below-mentioned code in it.

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

It is not necessary that the DB_HOST value will always be localhost. It depends on your hosting service provider.

For hosting service providers like; Bluehost, HostGator, Site5 it is localhost, for others, it may vary.

Many WordPress users say they have fixed their issue by changing the localhost to the server IP address.

This solution mostly works when running WordPress on a local server. like; MAMP.

The DB_HOST value changed from localhost to IP seems to work most of the time.

define('DB_HOST', '');

Note: IP address will be different for hosting service providers.

Now, If everything is mentioned correctly in your wp-config.php file (Check for any typo errors as well), now we can say that the issue is from the server end. (It is clear that it’s not configuration issue).

Verifying Your Web Host is UP And RUNNING! (MySQL Server)

A majority of the time, Error establishing database connection occurs when your site gets a lot of traffic. (Especially for shared hosting)

This happens because the host allowed utilizing a limited amount of resource for your account and your host server just cannot handle the load.

Your website will start running very slow and it will start showing errors to some of the users.

In this case; The best this to do is contact customer support of your hosting provider and ask them if your MySQL server is up & running!

In case you want to test it on your own, if your MySQL server is running, there are few things we can do.

Look for other sites hosted on the same server and check if those sites are loading fine.

if they also facing an issue like you, then it’s an issue with MySQL server.

If you don’t know or don’t have any other sites running on the same server, there’s another way to check your MySQL server status.

Open your hosting Cpanel and try to access phpMyAdmin from there.

If you are able to connect successfully then it means the MySQL server is up and running,
now we need to verify the permission of the database user you’re using in the wp-config.php file.

To check the permission of database user just follow below steps.

We need to create a testconnection.php file and just paste the below code into it.

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

 Kindly verify again if there are any typo errors in the wp-config file.

Note: Please make sure you replace the database username, password with yours.

If you’re able to connect that means the user has sufficient permission to access the database, and the issue is with something else.

In case you’re not able to access the database via phpMyAdmin, then it’s clear now that there’s something wrong at the hosting provider.

It is not necessary that your MySQL server is down, It can be due to the insufficient permission of database user.

In my case, MySQL server was running fine and my other sites were also working without any issue (hosted on the same server).

When tried to access my phpMyAdmin, I was getting below error:

#1045 – Access denied for the user ‘root’@’%'(using password: YES)

I got on the phone with my hosting support team and they found the issue. Due to some unknown reason, our user’s permission got reset.

I check with them about why that happened but it was a mystery for them as well.

But thanks to their support they quickly restored the permission for my databases user and my site is back up and running.

This made clear that for access denied issues if you can’t connect to your phpMyAdmin or the testconnection.php file is not working for you.

 You should get in touch with your hosting provider right away and they should be able to fix the issue for you.

Fixing the Error Establishing a Database Connection

Few Solutions that has been tested by many and Worked for them.

Important Note: This solution may not work in your case, please try this at your own risk,
Before making any changes please create a sufficient backup, in case anything goes wrong.

Solution #1

Nabila Shaikh says one of her clients was getting error “Database needs to be repaired.”.

They performed repair on the database, but it still shows the same error. She tried almost everything but at the end she found that the issue is with site URL.

The site URL got changed for some reason which caused this error. She ran a SQL query via phpMyadmin:

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Please replace YOUR_SITE_URL with your actual URL example:

You may find that some sites using custom database prefix such as lhr,cpg,wpd, etc.

If you are using custom database prefix, then the wp_options will change to lhr_options and so on…

Nabila says that this resolved the issue for her client and the site is up and running.

Solution #2

Sachinum also got “Error Establishing a Database Connection” but was able to connect to testconnectio.php file,
now he was sure that the issue is with the wpconfig.php file.

He changed the database user in the wpconfig.php file to root and WordPress started working fine.

Then he reverted the user to root and WordPress still works fine, he wasn’t sure what was the issue but he feels it could be a typo in the file.

Solution #3

Cutewonders, also experienced the “Error Establishing a Database Connection” was able to resolve the issue via phpMyAdmin, they made some changes in the wp_options table and edited the value for active_plugins and recently_edited. That seems to fix the problem for them.

Most of the users were able to resolve the issue by uploading a fresh copy of WordPress and that fixed the issue.

The error “Error Establishing a Database Connection” looks simple, but it’s very frustrating when your site goes down because of it.

What worked for you? Let us know in comments and I’ll be happy to add more solutions to this article.

Your comment could save a lot of time of others who are looking for a solution to this problem. Error Establishing a Database Connection, if you use the internet for a while now, then you might have seen this error few times.

Comments are closed.