What to do when your WordPress blog suddenly crashes with “Error establishing database connection” error:
This means all of your blog’s traffic is gone, users are not seeing your content but the error white page. Time for your to act quickly and restore your blog.
If you are using any cloud hosting like Digital Ocean (I host this blog in DO), AWS, Google cloud where you don’t have a fully managed control panel. Then you should be doing some of the basic server admin tasks like app/database restarts yourself.
When your WordPress blog which is working fine, suddenly throws up the error like the below one or some other error message that has the word “database” in it, then the first thing you should immediately do without wasting time is restarting your database engine.
Since most of the time for a perfectly working site to face this issue is due to spike in incoming traffic or spike in memory/resources. A simple restart should fix this issue. If the restart doesn’t solve, then you should go for the next steps to identify the config issue that caused the error.
“Error establishing database connection”
Since all of the WordPress sites use PHP and MySQL, the below steps should help all WordPress users.
First get the name of your OS which is installed in your server. If you know this already, then you may skip this step. When almost everything is Unix, there are couple of major Unix OS flavors like Ubuntu, CentOS etc.
Using the command below first get the name of your OS
#COMMAND TO GET THE OS NAME
If you are running Ubuntu OS then below command will help restart the MySQL services which will most of the time fix the database error that is preventing your WordPress site to load.
sudo service mysql restart
If you are running Centos OS then below command will help restart the MySQL services
CENTOS 7.0 AND BEYOND
## for mysql
systemctl restart mysqld
If your server uses Mariadb (which is also a Mysql package) then use the below command instead of the above one.
## for mariadb
systemctl restart mariadb
SIDE NOTE: Paying $35 monthly for CPanel if you are in dedicated hosting or $5-$10 in a shared hosting can definitely be saved. Setting up cloud hosting in Digital Ocean is the way to go with huge savings and options to scale up. Freelancers in sites like Fiverr or Upwork can get you the initial set up and then you can prepare list of commands that will help you maintain the server. You can have them printed for handy reference. But you should always have an admin/technical person contact available so just in case if something is needed to be fixed/done immediately on pay per hour/ task basis.
There are also reputed organizations who do server admin/maintenance for a fixed monthly cost. Even adding this on top of your cloud hosting will still be a huge savings compared to your shared/dedicated hosting bills.