If you have been using WordPress for your web design, then it is likely that you may have been confronted with the issue of “Error Establishing a Database Connection” which in itself is a curse.
A week ago, one of my clients got the same issue and I helped them to resolve it. I thought to jot down the solutions for other guys who may face the same issue at some point in time so that they don’t need to struggle to solve it.
First of all, we must understand the reason for this error because it is very important to know the root cause in order to fix any problem.
The Reason for “Error establishing a database connection”
As you can understand from the error message “Error Establishing a Database Connection”, your WordPress setup is unable to establish a proper connection with your database.
While there could be a lot of reasons for the connectivity problems of WordPress with the database, some of the common being changes in database login credentials, the database becoming unresponsive & using wrong credentials in the configuration file.
Solution #1 Check the wp-config.php file
The first & important step that you must take to fix this issue should be to check your wp-config.php file because this is the single most important file in your WordPress setup.
Just make sure that the credentials used (as shown below) in this file for the database connectivity is correct.
Do note that with most of the web hosting companies servers, DB_HOST as localhost works however there are several servers that will require website IP instead of localhost.
Solution #2 Check MySQL Server
At times, you may encounter the message “Error Establishing a Database Connection” due to the unresponsiveness of the database server which you may check with your web hosting company. Generally, it happens due to the sudden massive increase in traffic to your website.
Therefore, it would be a great idea to check with the hosting guys if your MySQL server is responsive or not. If not, they can fix this problem at their end only.
Let me add here that you can also do a preliminary check at your end before contacting your server guys, just in case they are not available (although most of the web hosting companies are available 24/7).
You can log in to your CPanel (Control Panel) and check if the username (which you have used in your wp-config.php) has sufficient permissions to connect with the database or not.
Because in my past experience, I noticed a couple of times that the user’s permissions could be reset because of any upgrades on the server. So, you may want to check and verify if the user has still got permission to connect with the database.
In order to test this, all you have to do is write a piece of code in a file that you may rename as “testingconnection.php” and run on the server to check.
If the result of this file’s execution is ‘could not connect, then you must contact your server guys definitely however if the result is ‘connected successfully’ you may like to check the wp-config.php file again to make sure that there is no typo mistake or even spaces which hinders the database connectivity.