How to resolve “Error Establishing Database Connection” in WordPress

You are opening your WordPress website and getting a message stating “Error Establishing Database Connection “, reasons may be one of the following:

How to resolve “Error Establishing Database Connection” in WordPress
  • The database has been corrupted.
  • The database login credentials given in wp-config.php are wrong
  • The WordPress database tables have been corrupted or not imported properly.
  • The table prefix is wrong.

Here, we will learn How to fix Error Establishing Database Connection for your WordPress-based Website.

Step 1 — Check Database Login Credentials

You’ve just migrated your WordPress-based website from previous hosting providers to elsewhere; you need to update your database connection details given in wp-config.php.

Click here to learn how to Edit the wp-config.php file in DirectAdmin or cPanel.

Locate the database connection information given in your config file.

/** 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');

Check the connection details as mentioned with correct details. If the connection detail is wrong, please update the information, click Save, and exit.

Reload the website to check if the issue is resolved or not.

Step 2 — Check the table prefix

Open PHPMyAdmin and click on your database.

How to resolve "Error Establishing Database Connection" in WordPress 1

Check the prefix given before the table name. The table prefix is given in wp-config.php, and the one showing in PHPMyAdmin must be the same.

How to resolve "Error Establishing Database Connection" in WordPress 2
table_prefix in PHPMyAdmin

How to resolve "Error Establishing Database Connection" in WordPress 3
table_prefix in wp-config.php

Step 3 — Try repairing the WordPress Database

cPanel provides you with the feature of a repair database. Please follow the steps below to repair the database.

Through cPanel

1. Login into cPanel.

2. Navigate to the DATABASE section and click on MySQL® Databases

How to resolve "Error Establishing Database Connection" in WordPress 4

3. Go to the Modify Databases section, select your database from the drop-down, and click on Repair Database.

Modify databases

4. A new GUI will appear, and the system will attempt to check/repair the database automatically.

Through PHPMyAdmin

We can repair our database using PHPMyAdmin too. Let’s see how to repair the database using PHPMyAdmin.

1. Login into cPanel.

2. Navigate to the DATABASE section and click on phpMyAdmin.

cpanle phpmyadmin

3. PHPMyAdmin will open in a new tab of your browser. Click on the database which you want to repair.

How to resolve "Error Establishing Database Connection" in WordPress 5

4. If you are sure about the table causing the issue, you can select the table or check all the tables for repair.

How to resolve "Error Establishing Database Connection" in WordPress 6

5. From the drop-down With selected: choose Repair table. This will execute the REPAIR TABLE SQL query on the selected tables or all tables if you have selected Check All, and the tables will be checked or repaired.

How to resolve "Error Establishing Database Connection" in WordPress 7

Through wp-config.php

WordPress provides built-in functions to repair the database, which is disabled by default due to a security issue.

Here, we will learn how to enable the feature, run the repairs, and then disable it to avoid this potential security threat.

Open the wp-config.php file and paste the following code:

define('WP_ALLOW_REPAIR', true);

Save and close the editor.

Open the browser and load the following address.

Note: Please replace yourdomain.com with your actual domain name

http://www.yourdomain.com/wp-admin/maint/repair.php

Press the Repair Database button, which will appear at page load.

You’ll be taken to a results page where you can see the checks and repairs happening in real-time.

After finishing the process, please open the wp-config.php file again and remove the line of code that we have pasted to disable the repair function.

If your issue is still not resolved after following the steps given above, please restore your database from the old backup you have.

Power up your business website with YouStable India’s affordable unlimited web hosting services and meet your online success! YouStable is not just a brand that provides hosting services to clients. The quality of services a customer gets from us is unparalleled and our clients know it.

Leave A Comment