I recently experienced the ever-so-helpful “Error establishing a database connection” message from my WordPress installation. After about 30 minutes looking through Google results and watching some terribly useless YouTube videos, the best explanation I found was: there must be a problem with your
wp-config.php file. I knew I hadn’t changed it, and none of the software was new, so I was the 1% case where that didn’t apply. So I thought, man, I know the LAMP stack pretty well, there’s no reason I can’t just cut through this and find the real error. I did, and so I’m going to share my method.
Turns out there are three places in the code that produce an “Error establishing database connection” message. If you’re receiving only that text, then we don’t care about two of them. The remaining place that generates this error is in the
dead_db() function in
/wp-includes/functions.php. You’ll notice the following code:
// If installing or in the admin, provide the verbose message. if ( defined('WP_INSTALLING') || defined('WP_ADMIN') ) wp_die($wpdb->error); ...
For security reasons, the actual MySQL error will only be displayed if you’re either (1) currently installing, or (2) an admin. If you aren’t logged in (like I wasn’t), then you definitely won’t see it. All I did was comment out the if-statement, and poof, the error was displayed, so I saw something along these lines:
One or more database tables are unavailable. The database may need to be repaired.
Easy enough. So I went into phpMyAdmin, repaired my options table, and my blog was back online. Hope that’s more helpful than the rest of the discussions on the web about this error.