WordPress Error Establishing Database Connection

Recently, on one hosting account which had some WordPress blogs on it, the hosting company did some maintenance to it. After the maintenance, one of the WordPress blogs had the “Error Establishing Database Connection” error but the others were fine. It was strange that it only applied to the one website. I had not changed anything, the wp-config.php file was still correct and the hosting company did not know what had happened.

Here is what I did. Hopefully, it will help you out if you have a similar issue…

Diagnosing the Problem

Here are some of the steps I went through to try to solve the problem or figure out what was wrong.

  • Restarted mysql. This did not fix the problem.
  • Restarting Apache did not help either.
  • I purged the cache from the CDN, this did not work.
  • Cleared my own temporary internet files (cookies and cache) in case it was my own browser that was at fault, but this also did not help.
  • Logged in via mysql on the command line using the database login from the wp-config.php file. Logged in fine and all the tables had the correct prefixes.
  • At this point I was fearing that I’d have to use a recent backup to re-build the database.
  • Tried to update the plugins using WP-CLI… this told me that the options table had crashed and the automatic repair had failed. Bingo, found the problem!

The actual new error message on WP-CLI was something like this…

mysqldump: Got error: 144: Table ‘table_name’ is marked as crashed and last (automatic?) repair failed when using LOCK TABLES

In my case, it was the “wp_options” table that had crashed.

Now we know what the problem is we just have to fix it!

Repairing the database

This is what I did to finally fix the “Error Establishing Database Connection” error…

  • Logged into mysql on the command line.
  • use database_name;
  • repair table table_name;
  • unlock tables;

Voila! Problem solved and WordPress blog running normally again!

As you can imagine, this has only strengthened my love of WP-CLI which is proving more and more useful every day.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.