Fix Error Establishing a Database Connection in WordPress

Updated on

A guide to resolve Error Establishing a Database Connection in WordPress. Learn how to fix Error Establishing a Database Connection in WordPress. Also, check out solutions that worked for other people to get rid of this error.

The literal meaning of this error is that WordPress is unable to make a connection to the database of your website. And this is one of common WordPress errors.

In this article, I will cover Reasons of Error Establishing a Database Connection in WordPress, Possible solutions to fix this error, Tricks/tweaks that worked for others to resolve Error Establishing a Database Connection in WordPress.

First of all, let’s find out why is that happening?

how to fix error establishing a database connection in wordpress
How to Fix Error Establishing a Database Connection in WordPress

Reasons of Error Establishing a Database Connection in WordPress

This error may occur due to different reasons (causes), that’s sound weird but it is true. Those reasons may be:

  1. WordPress MySQL database has been corrupted.
  2. Wrong database credentials (database name, password etc.) in WP-Config.php
  3. MySQL Server (Web Host) is not responsive.
  4. Any other reason/ what worked for others.

As this error might cause by various reasons, we will try to find the solution based on the reason.

Solutions to fix Error Establishing a Database Connection in WordPress

WordPress MySQL database has been corrupted:

You can easily diagnose this, check if you are able to login to WordPress dashboard.
If you are not able to login to WordPress. And when you are trying to open WordPress login URL (yourdomain/wp-login.php). You are getting some error like “One or more database tables are unavailable. The database may need to be repaired.” 

Clearly, WordPress MySQL database has been corrupted and you need to repair the database in order to fix the error.

You need to add following the piece of code in wp-config.php file to repair the database.  And you should add this code below define('WP_DEBUG', false); or say just above /* That's all, stop editing! Happy blogging. */. Don’t forget to save changes.

define('WP_ALLOW_REPAIR', true);

wordpress-database-repair-and-optimize-function-wp-config-php
Once you are done with that, now you may proceed to repair database by visiting following URL:

https://yourdomain.com/wp-admin/maint/repair.php

When you open this URL, you will be prompted to database repair screen as shown below.

repair-and-optimize-options-wordpress-database

You can either go with Repair Database or you can Repair and Optimize Database at once. Of course later one will take longer than the former one. Once you click an option, it will take a while depending upon the size, issues with database and your internet speed. So, be patient.

Once you are done with repairing and optimizing database, you must remove the piece of code which you added in wp-config.php.

Leaving that code in wp-config.php will make your website vulnerable as database repair options can be accessed by anyone. Therefore you are advised to remove this function define('WP_ALLOW_REPAIR', true); from wp-config.php.

If your problem is not fixed by this database repair. Then you should continue reading following paragraphs. Which explain other possible solutions to fix Error Establishing a Database Connection in WordPress.

Wrong database credentials in WP-Config.php file:

WP-Config.php is one of the core WordPress files (installation files). In this file, we specify the credentials of the database to which WordPress need to be connected.

Sometimes database credentials get changed. This happens usually when we migrate WordPress from one hosting to another. You should make sure that you have updated new (current) database credentials in wp-config.php file.

Furthermore, you should also check that wp-config.php file is having all the necessary values as mentioned below.

/** The name of the database for WordPress */
define('DB_NAME', 'database-name');
/** MySQL database username */
define('DB_USER', 'database-username');
/** MySQL database password */
define('DB_PASSWORD', 'database-password');
/** MySQL hostname */
define('DB_HOST', 'localhost');

Although hostname ‘localhost’ is the default and works with most of the web hosting services. There are few web hosting services which use custom hostname too. You can contact your hosting company to confirm MySQL hostname. Or you can check out here.

Make sure that everything is correct in wp-config.php. And you should also double check for typographical errors. If everything is okay in wp-config.php file. Then there are fair chances that server is not responsive or something is wrong at the server side.

MySQL Server (Web Host) is not responsive:

Sometimes error to establish a database connection occur in WordPress when the web server is overloaded with a lot of traffic to the website. In such situation, the web server cannot handle that much traffic at once and it starts showing the error to some of the visitors. This usually happens when the website is hosted on a shared hosting. In such case, you should talk to your hosting company over the phone or live chat to confirm with them whether your MySQL server is responsive.

If you want to test your web server yourself. You can check if other websites which are hosted on the same server are getting the same error. If other sites on the same server are also having the issue then certainly there is something wrong at the server end.

If you have no other site hosted on the same server, you can go to cPanel (of your hosting)>phpMyAdmin. Try to access the database of your website. If it accessible that means the server is responding. Here you should make sure one more thing that the database user associated with the database have sufficient privileges. To check this, create a new file called testserverconnection.php in the directory where WordPress is installed. And paste the following code in the newly created file. You read more about this code here.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

You should replace mysql_user and mysql_password with your database username and password. Finally, Save changes and try to open this file in the browser. If you get ‘Connected successfully’ that means user have sufficient privileges. And the reason of the error is something else.

If you are getting ‘Could not connect’. Read Nicknormal’s solution in paragraphs below.

Any other reason/ what worked for others:

Knightgambit reported that his website having this issue. However, his website does connect once in a while. Further discussion revealed that the culprit was large traffic to his site. Apparently, his server was not able to handle that much traffic that is why the error was occurring. And the error got fixed itself as traffic went low.

Nicknormal reported that even after performing database repair he was getting the error, when he tried to visit /wp-admin/ page of his site. Finally, he found out that his database user’s privileges had corrupted. And he fixed it by removing the old user and adding the new user. Then connecting the new user to the database and updating wp-config.php file with new information.

I hope this guide will be helpful for you to fix Error Establishing a Database Connection in WordPress. Let me know your experiences about this issue. Also, share any other method which you have used to resolve this error via the comment section below.

You might find following posts useful…

Best Methods to Edit WordPress htaccess file
Optimize Yost SEO to Rank Higher in Search Results
Customize Facebook Like Box to Get more likes
Why You Must Optimize WordPress Database and How to Do it Easily

Photo of author
Shashank Singh
Shashank is a web addict and amateur blogger. His current interests include blogging, SEO, and WordPress. See his social profiles to know him even better.
Disclaimer: Affiliate links of some product(s) are being used on this page, if you follow the link and make a purchase, we may receive compensation from respective companies. This compensation comes at no additional cost to you.

Leave a Comment

Your email address and website details will not be published. Required fields are marked with *.