How to Fix the Error Establishing a Database Connection in WordPress

Error Establishing a Database Connection in WordPress

Sometimes when you are surfing the web, you may find a website saying just “Error Establishing a Database Connection” in bold on a white page in place of actual content. Yes, this is one of the few error pages we see on the web. If you are seeing this error on your own website, there is something wrong. If WordPress doesn’t solve the issue automatically, you should be worried and think about resolving this issue. The error is related to the WordPress database and there could be several reasons why the WordPress website is throwing an error saying “Error Establishing a Database Connection.” Keep reading to learn how to fix this WordPress error.

Also read: What is Gravatar and why should you know about it?

Best Managed WordPress Hosting

WPEngine is the best and most secure managed hosting provider

Why does a website show an ‘Error Establishing a Database Connection’ error

As it already explains, the website is not able to connect to the database. WordPress software is written in PHP to use the MySQL database server. It saves and retrieves data from MySQL.When you type the URL of a page in the address bar, PHP scripts execute and fetch the content of the page from the MYSQL database.  In case the connection is failed, it shows the error saying Error Establishing a Database Connection.

The reason for this issue may not the same the all time. There may be various cases including wrong login credentials, an unresponsive database server, hacked website, and changes in login credentials. If I list possible error reasons, there are:

  • Unresponsive database server
  • Incorrect login details
  • Corrupted database or table
  • Corrupt WordPress files

Most of the time, the reason could be an “Unresponsive database server”.

How to Fix the Error Establishing a Database Connection in WordPress

In this article, I will try to explain the possible cause and possible solution to Fix the Error Establishing a Database Connection issue in WordPress.

Solution for: Unresponsive database server

This is the most common issue and most of the time, you should not care for it. MySQL database automatically restarts to fix this issue. So, in a few seconds or minutes, the database server of your website will automatically start responding to resolve this issue.

Solution for: Incorrect login details

This cause is a very rare case and it can only happen if you mistakenly added the wrong login information of MySQL at the time of WordPress installation. Another reason could be the change of MySQL credentials.

To know if the issue is due to the wrong MySQL credentials, you need to first test the login credentials. For this open WP-Config.php and you will see database credentials in the code lines below:

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

If you changed the login credentials and know the new one, you should add the new login credentials here. If you are not sure whether these login credentials are right or wrong, you need to test them.

To test these credentials, create a test file in the root of your WordPress installation and add te the following code. Replace user_name and password with the username and password mentioned in the wp-config.php file.

$link = mysql_connect('localhost', 'user_name', 'password');
if (!$link) {
die('Could not connect to database: ' . mysql_error());
echo 'Connection is right';

Now save this test file as test.php and run it from the browser.

If it shows ‘connection is right’, your database credentials are fine. Otherwise, you need to work on putting the right WordPress credentials in the wp-config.php file.

If login credentials are wrong, you can change the database user password from CPanel.

To change the login credentials, Open Panel and go to MySQL Databases section.

MySQL user password change

Now go to the Current Users section and change the Password. Now enter the new login credentials in the WP-Config.php file.

Solution for: Corrupted database or table

If login credentials are fine but WordPress is still showing the error, there may be a serious issue with the database. It is possible that a table of the whole database is corrupt. To fix this, login into your database. For this, open the CPanel and select PHPMyAdmin.

Warning: Be very careful while using PHPMyAdmin, because you will be dealing directly with your database.

In phpMyAdmin, select the database you are using for your WordPress blog. Here, select all the tables and then select repair table from the dropdown.

Repair MYSQL table

If you do not want to login into phpMyAdmin, you can try an alternate way. WordPress, allows you to repair the database. For this, add this line in the wp-config.php file.

define ( 'WP_ALLOW_REPAIR' , true);

Now, you open this URL This should open the following screen:

Repair WordPress database

Select any of these options and let WordPress do the rest of the things.

Note: Once you’re done repairing your database, be sure to remove the line of code you added to wp-config. It is because anyone can open this page and run this file.

Solution for: Corrupt WordPress files

If you tried all other things but the issue is still there, there could be some problem in the WordPress installation. To fix this issue, find the corrupt file and fix the issue. Finding the corrupt file in the WordPress installation can be a big headache. So, you can try replacing all WordPress core files with new fresh files.

But this is risky and can wipe away your entire website. So, you need to perform this carefully. If you are not sure, you can hire a developer. If you are thinking to do by yourself, you should first take the backup of the whole site.

Then download the free copy of WordPress from and unzip it on your computer. From this, delete wp-config.php and wp-contents folder. Be sure to delete both things. It ensures that you don’t lose your themes, plugins, media, and existing settings.

Now upload all these files to your WordPress root folder using FTP or the File Manager.

Once you have uploaded all fresh files, reload your website to see if the error is fixed. You should try opening the site in incognito mode.

Final thoughts

I tried to list all the possible ways to fix the Error Establishing a Database Connection issue in WordPress. It will solve the issue for sure. If you still have anything to ask, I will be happy to help you. You can contact me to fix your WordPress website. I charge a reasonable amount for this. I also work on different WordPress and application related projects.


Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.

Similar Articles


Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

© 2022 The WP Guides Developed By Deepanker