This error “Error Establishing a Database Connection” just happened today and I will share how to step by step resolved this error.
Today at home when I work for another client I got called from the office to let me know that my colleague cannot access our WordPress-based website with the error below.
The error above is usually because there is a mismatch between password and username on the database with the one recorded in the file wp-config.php however, the error is persistent, you cannot browse any page on your website with the same error message.
However, it’s different in my case, visitors sometimes still able to browse some pages and the error shows up on other pages, so the error message is not persistent.
Contacting The Hosting Company to Fix The Error
As I work for another client at that moment I straight away contact the hosting company to ask for help. After a quick check, their customer support said that the problem has been fixed by retyping the username and password on file wp-config.php
Upon checking I found that the error “Error Establishing a Database Connection” still exists although sometimes we can browse the website after pressing F5 (refresh).
The customer support now hands over the problem to their technical problem for further checking and after about 45 minutes they said that the problem is with the WordPress database and asked me to optimize the database.
After they hand over the problem back to me 🙁 I found another error message “Warning:mysqlli_real_connect(): User ‘xxxx’ has exceeded the “max_user_connections’ resource (current value: 15) in /home/u439972631/domains/domainname/publich_html/wp-includes/wp-db.php on line 1626.
User ‘username’ has exceeded the ‘max_user_connections’ resource (current value:15)
This error is not because of too many connections from visitors to the website, but it’s a database user that makes too many connections to the database, usually, they only make one connection or less than 10 connections one time.
I also found the error below on every page I browse on the website.
I realized that they already activated the debugging mode on WordPress to “true” so I can see more detailed errors above. I also knew that I need to replace the theme to fix the error above.
Back to the problem, I am still unable to access the website because of “Error Establishing a Database Connection”, need several times to press F5 key to be able to see the webpage. Realized that the website visitor will see this error therefore I install a maintenance plugin to let the visitor know that we are under maintenance.
Surprisingly after activating the maintenance mode, I found that now I can browse the backend site smoothly.
Optimizing Database
I open phpMyAdmin from CPANEL and found that the database contains 90 tables, while the other website only has 23 tables. This is because some plugins that have already been deleted still have the tables on the database.
After making some database backups I still hesitate to delete the unused tables. Finally, I use the WP Optimize plugin to optimize the database and delete unused tables left by already deleted plugins.
After deleting some unused tables I turn off the maintenance mode, hoping that the error will disappear.
The Solution
I feel disappointed to find the error “Error Establishing a Database Connection” still exists after optimizing the database. I suspect a plugin that still using the old version but cannot have the updated version because I got it from a friend. After I deactivated the plugin (WP Rocket ver 2.27) I found that I can browse normally only slower because the plugin that causes problems is to cache the website to make it faster.
I am not saying that WP Rocket plugin is bad actually, it is a very good plugin to speed up your WordPress website. I made mistake by installing the old version of WP Rocket which cannot be upgraded.
I then replace the plugin with the LiteSpeed Cache plugin and glad I did it because now the website is back to normal.
The lessons:
- The first step to isolating the problem is to stop incoming traffic that accesses the database by activating the maintenance plugin.
- Don’t use plugins that cannot be updated because in the future it can be a problem because the code is not up to date.
- Always back up the database before making any changes to prevent unwanted problems so you can restore the working database in case anything happened.
- I need to replace the WordPress theme as it shows “Deprecated” syntax error which means that the developer did not update the theme or I have a theme that cannot be updated for some reason.
I hope that this posting is useful for you, and you don’t have to waste hours solving the problem. The error “Error Establishing a Database Connection” may have another solution to fix, but this is mine.