Type to search

How To Fix Error Establishing A Database Connection In WordPress?

Getting “Error establishing a database connection” in WordPress? Don’t panic. The first thing that you need to know is whether there will be any loss in the incoming traffic of the site. 

There are some common problems that you will face if once this error gets triggered such as unable to access WordPress, loss in data, etc. This error generally gets triggered when you enter incorrect login credentials, or if the database is corrupted.

Whatever be the cause, these types of errors can negatively affect the site. Thus, you need to fix the problem at the earliest. In this article, we are going to help you with the possible solutions that can help you to get rid of the problem.

3 Easy Methods to Fix “WordPress error establishing a Database Connection”

If you are currently getting this error in your device, then you have to look out for the methods that are mentioned below.

Method 1: Repair the Database

If this WordPress error occurs, then the first thing you have to do is repair the database. Because a faulty database can be the prime reason behind the issue. You have to repair the database, if you see “One or more database tables are unavailable” message whenever you will try to visit the website.

Now, in order to repair the database, you have to first go to the wp-admin.php file. Simply follow the points mentioned below:

  • Add the “1 define( ‘WP_ALLOW_REPAIR’, true )” code in the file. This code will help you to repair the database that is running on WordPress. 
  • After running the above code, next, you have to go to the “www.yoursite.com/wp-admin/maint/repair.php ” website.
  • A page will appear next on the website. On that page, you will get two similar options.
  • First, you will get a “Repair” option which will help you to repair the database. 
  • The second option that you will get is “Repair and Optimise”. This option is for repairing and optimizing the database for protecting the WordPress.  This will help to avoid any future errors and keep the website stable.

Note: The line of code that you have put in the wp-admin.php is not protected and anyone can get its access. So, you have to remove the line of code from the file before you start the repairing process.

If this method did not fix the issue from the WordPress, you have to proceed to the next method that is mentioned below.

Method 2: Change the Login Credentials

If you are still getting the error and unable to open the WordPress even after repairing the database, then changing the login credentials is the most suitable option. The database login credentials can stop working if you change the hosting company.

To change the credentials, you have to do follow the steps mentioned below:

  • In order to change the login credentials, you have to first open the wp-config file and search for the database login information on the file. (You will find the information at the top of the wp-config.php tab.)
  • Search for the database name, it will be “DB-NAME”  and the login user name will be “DB-USER”.  The login password will be shown as “DB-PASSWORD” and the database host will be “DB-HOST”. 
  • After checking all the information make sure that all the information are correct. Because WordPress won’t open if there is an incorrect value.
  • If you see that the database name, password, and database host is not the same as mentioned above, you have to change accordingly to fix the error in WordPress.

Still unable to establish a database connection? Check out the next method to resolve it.

Method 3: Remove the Corrupted Files

If changing the login credentials did it fix the error, then you have to remove the corrupted files that exist under WordPress. Most of the WordPress users have complained that they have repeatedly faced this issue. Thus, we would recommend that eliminating the corrupted files from WordPress will help you to remove the issue.

Note: You have to back up all the data that is saved in WordPress. You can do it either manually or by using a plugin.

The first step to be taken is to replace the previous old corrupted files. Follow the steps mentioned below:

  • Firstly, you need to download a fresh copy of WordPress CMS from WordPress.org.
  • Next, you have to unzip the file in the drive and remove the wp-content folder and wp-config folder. This ensures that there is no overwriting on the current wp-config file that you have just downloaded. 
  • When you are done with the removal process of all the corrupted WordPress folders, you have to paste the rest of the folders into the WordPress root folder using the File Manager application.

Performing the above steps will replace the corrupted folders with new ones and can easily solve the issue from WordPress.

Concluding Remarks

These are the important hacks that can help you on how to fix error establishing a database connection in WordPress. There are several reasons behind this problem and we have stated outlined steps that can help you in all circumstances. Let us know which method worked for you and share your feedback through the comment section below.