How to fix 'Error establishing a Database Connection' for Wordpress


If you see an error like the below on your Wordpress site, it's likely due to a Wordpress CMS or Plugin update. However it could also mean that your database connection details in your wp-config file have come unsynced with the details on the server. This guide will take you through rectifying this issue.

wpdberrorpic01.png

Step 1 - Log into cPanel then open the File Manager.

wpdberrorpic02.png


Step 2 - Find and click on the domain/subdomain's root folder. This will normally be the public_html folder, however if you want to be 100% certain (it can be different for addon and subdomains)
click here for a guide on finding your root folder.


wpdberrorpic03.png


Step 3 - Find the wp-config.php file, right click and select Edit (or Code Edit).

wpdberrorpic04.png


If you get this popup box, just click Edit again.

wpdberrorpic05.png


Step 4 - Take note of the below lines as they appear on your screen.

5cfcfa61e952d77ee3c0959d8c4c84e000497b9e?t=11d077567adfa3e7a7129bce608bae82


Step 5 - Keep that window open, open a new cPanel window and find the MySQL Databases option.

wpdberrorpic07.png


Step 6 - Take note of the database names.

wpdberrorpic08.png


Step 7 - Check that the database name and user matches in both the 'wp-config.php' file and the MySQL Databases section. If they don't match, change the entries in the 'wp-config.php' file to match what's in the MySQL Database section

wpdberrorpic11.png
If you see multiple databases in the MySQL Databases section, choose the database with the matching name to perform the following steps on. If no databases have a name that matches the wp-config.php file, you could either try these steps for each database one by one, or contact your website developer.



Step 8 -
Look at the Add User to Database section in MySQL Databases and the DB_USER and DB_PASSWORD entries in the wp-config file. Make sure that the database and user entries match in both cases, and then select the Add button.

wpdberrorpic12.png


Step 9 - Select All Privileges and hit the Make Changes button down the bottom of the page. Once the change completes, press the Go Back option in the next screen.

wpdberrorpic13.png


Step 10 - Look at the Database Password section in MySQL Databases and the DP_PASSWORD section in the wp-config file.

wpdberrorpic14.png

wpdberrorpic15.png


Step 11 - Click the Change Password option for the correct database user


wpdberrorpic16.png


Step 12 - Set the database password to what is in the wp-config file. It's easiest to copy/paste the contents.

wpdberrorpic17.png


You can optionally select the Repair Database option from the MySQL Databases menu to perform several checks on the database.

wpdberrorpic18.png

If this does not resolve the issue it's likely there is another part of the website files that is causing additional problems. We would recommend speaking to your web developer, following these troubleshooting steps, or restoring the site from a backup.


For more tutorials on website development and Wordpress troubleshooting, view our List of Wordpress Guides.


Thank you for your feedback on this article.