You are opening your WordPress website and getting a message stating “Error Establishing Database Connection “, reasons may be one of the following:
- 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.
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.
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.
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.
2. Navigate to the DATABASE section and click on MySQL® Databases
3. Go to the Modify Databases section, select your database from the drop-down, and click on Repair Database.
4. A new GUI will appear, and the system will attempt to check/repair the database automatically.
We can repair our database using PHPMyAdmin too. Let’s see how to repair the database using PHPMyAdmin.
2. Navigate to the DATABASE section and click on phpMyAdmin.
3. PHPMyAdmin will open in a new tab of your browser. Click on the database which you want to repair.
4. If you are sure about the table causing the issue, you can select the table or check all the tables for repair.
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.
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:
Save and close the editor.
Open the browser and load the following address.
Note: Please replace yourdomain.com with your actual domain name
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.