Error establishing a database connection

Image of error message

This morning, I logged on to my Google Analytics account and reviewed my site stats for my various sites. I was shocked when I saw that there were no hits on this site. “What’s going on?” I asked myself. Plenty, as it turns out.

When I navigated to the home page I was greeted with the all-too-common “Unable to establish a connection to the database” that one sees all over the internet, in no small part to the success of WordPress.

I had just recently experienced the same problem on another one of my sites, OneBigCalifornia. So I was able to resolve this problem fairly quickly and get this site back up and running. The problem, of course, is that I had not checked my site for a few days, so I was “offline” for an eternity (in internet terms).

The problem is caused by a database user account losing its permissions to the database which supports the website. The fix is relatively easy: reset the permissions for the database user account. The only unusual thing is that my website are running on a Windows server, not a Linux server. Fortunately, the solution is applied via the MySQL command line interface, so the commands are the same, regardless of host operation system.

First, I attempted to log in using the credentials in the wp-config.php file located at the root of my website. I launched MySQL with the “-u” and “-p” parameters.

mysql -u 'dbusername' -p

I was able to log in, but as soon as I attempted to do any work, I was greeted with a message stating that I must “SET PASSWORD“. I was unable to enumerate the tables in the database. So it was clear that I did not have proper permissions assigned to this DB user account.

Next, I logged in with the “root” account so that I could set permissions for my DB user.

mysql -u "root" -p

Once I was logged in, I proceeded to grant all permissions to my user. NOTE: This is NOT the best practice. Permissions should be the minimum required to do the job at hand.

GRANT ALL PRIVILEGES ON DBNAME.* to '<username>'@'localhost' IDENTIFIED BY '<password>'

Where <username> and <password> are the ones in the wp-config.php file.

This restores the permissions for the database user so that this account is able to connect to the database normally. So we are back to where we should be. Navigating to the home page brought the site back in all it’s canned glory.

I don’t know why the permissions failed. But I think it’s time to re-set all the passwords for the system.

Leave a Comment

Your email address will not be published. Required fields are marked *