Recovering From The Mysterious Database Error

Posted on:

Oh. My. God.

So yesterday, I was casually working on my WordPress central hub for my content throughout my site. I was tinkering with the settings, and while on the general settings page, I happened to click "save".

And that's when all hell broke loose.

Somehow, the siteurl and home settings got changed as a result, and caused the site to stop working. If you've ever encountered this problem it's a pain in the ass. If you haven't and are a WordPress professional, you surely will someday. TL;DR - it usually results in you needing to set some consts in your wp-config file to get it fixed up, which is exactly what I ended up doing to fix it. After about 15 minutes and a few searches, and a quick SSH session, I was back in business.

Or so I thought.

The next morning, I wake up to find that I'm getting a vague "Error establishing a database connection" error on my content hub. This was causing 500 errors on my actual websites that use the hub. I spent some time looking through the usual channels to figure out the cause of the issue, and everything kept pointing me back to "the database is credentials are effed up". Which made no sense, since I was literally just using this site yesterday. The hell's goin' on here?

What made it more confusing, was that I was able to access the site using WP-CLI with no problem. I was also able to access the database using TablePlus. So I knew there shouldn't be an "error establishing a database connection" since I was clearly able to, ya know, establish a database connection.

After a few hours of trying all kinds of things, from disabling plugins, to changing themes, restoring backups, spinning up a whole new site, running the WordPress repair utility (which told me the database was perfectly healthy!) and even changing which database the site connects to, I finally concluded that it was absolutely, positively, 100% certainly not an issue with a database connection.

I started to arbitrarily dump data in the core files, hoping that maybe I could get a more-useful error message, and absolutely none of my loggers were giving me any information on why I was getting the error at all. And came up short.

I reminded myself that computers rarely just break for no reason, and that it has to somehow be related to what I had done yesterday when I was messing with the settings, so I retraced my steps in the database using TablePlus, and found the siteurl and home options. I noticed they were set to null, so on a whim I changed them to the correct value.

And it freaking worked.

I'm too relieved to be mad at WordPress at this point, but my god - Hours of my life was spent left high and dry with absolutely no guidance on how to fix the problem. The error message was incorrect, and it doesn't seem like WP had any idea what was wrong. $wpdb didn't provide any error messages, and that includes last_error. Nothing.

Like a good contributor, I opened a ticket. Maybe with some luck and a little effort we can help other people with this in the future, or at the very least get a little extra context on what on Earth I did to get myself in this weird scenario.