This error message is quite generic and if you have ever used WordPress on your web server, you are very likely to have come across this error message at least once. Nonetheless, in this article, we will be covering the possible causes of the said error message and then proceed with providing a list of solutions that you can implement to get the issue resolved. So, let us get started.

What causes the ‘Error Establishing a Database Connection’ Error Message in WordPress?

 The reasons which cause WordPress to be not able to connect to its database server can be plenty. The most common and often ones are mentioned down below:

Solution 1: Review your wp-config.php in your WordPress installation directory

The first thing you should do while proceeding to fix this error is to go and have a look at the wp-config.php file in your WordPress directory. You should see whether the contents of the wp-config.php has been altered with or not. To do that, you can either use the file manager provided in your web hosting’s cPanel for accessing the wp-config.php or you can use an FTP application like Filezilla to connect to your web server and access the file on a text editor installed on your local computer. Once you have access to the wp-config.php file, see that there are these lines present in it: The value of the DB_HOST might not be localhost if the database server is not having the same IP address as of your web server (this can be usually the case when you have not hosted your website locally on your computer).

Solution 2: Try establishing a dummy connection to your Database server

The next thing you should do is to see if you can establish a successful connection to the database server using the username and password for your database user account. Create a new file testdatabaseconnection.php in your website’s root directory with the following contents and access it from your browser. If you get the message “Connected successfully” then there is nothing wrong with the MySQL database server and it’s working fine. Remember to change the username and password fields with the username and password for your database user account. In case, you are wondering about the username and password of the database user account then you don’t need to worry. You can see it in your cPanel of your web hosting account in the database section. Or if you had made/configured the database yourself then you will probably remember the username and password. If you get an error message instead of the connected successfully message, then you need to make sure that you are using the correct username and password to connect to your database server. An alternative here would be to make another user for your database server with all the privileges and use that same user account to access your database. After you have created a new user account, you can test that account with the above method and if it is successful then you need to change the contents of the wp-config.php to match with the username and password of the database account that you have just created.

Solution 3: Repair your database (if it’s been corrupted)

If doing the above steps fail and you are still not able to connect to your database, then you should be considering repairing your database because it’s likely that it may have been corrupted. The good news here is that WordPress has a repair database mode which you can initiate just by adding a line to your wp-config.php. It will take some time and will finish. Afterwards, you should go and check whether you are still facing this error or not.

Solution 4: Executing SQL Query in PhpMyAdmin

A user suggested that he got rid of the said error message by executing the following SQL query in PhpMyAdmin in the web host cPanel (PhpMyAdmin is a web-based application for managing different aspects of your database). Remember to change the YOUR_SITE_URL with the link of your website. After doing that, you can see if your error has been fixed or not.

Solution 5: Reinstall WordPress core files (Don’t worry your website will not get affected)

After trying out all the above solutions and getting nothing to work for you, you should try reinstalling the WordPress core files.

How to Monitor your Database Performance using Database Performance Analyzer?Fix: Wordpress HTTP Error when Uploading MediaHow to Convert a WordPress Site into an Android AppHow to Build Advanced and Professional Looking Forms For Your Wordpress Powered… How to Fix WordPress Database Connection Error - 38How to Fix WordPress Database Connection Error - 83How to Fix WordPress Database Connection Error - 67How to Fix WordPress Database Connection Error - 94How to Fix WordPress Database Connection Error - 42