Error Establishing a Database Connection
If you are suddenly greeted with a white page with a single line of text telling you that there has been an error establishing a database connection, don’t panic.
If you are using WordPress to drive your site or blog, there are a couple of causes (and I’ve had problems with this over the weekend myself,) and a couple of likely solutions…
Either one of the following are wrong
- Your database username
- Your database password
- Your database name or
- Your MySQL hostname
Open your wp-config.php file and check these are correct and have not been changed. If you use cPanel to manage your site you can have a look at your databases and users there. Check with your web host for the required hostname. Some prefer localhost, others may use an IP address.
The site has exceeded the maximum number of mysql connections allowed and thus it is down.
My web host told me they have found this to be a very common issue over the last year or so with WordPress in which some plugin or possibly even a core component causes frequent corruption of the wp_options table (which is clearly odd since this table is very small with under 200 records most of the time).
Once the wp_options table is corrupted, the MySQL server generally will attempt an automatic repair which just runs and runs non stop due to something odd in the table which it doesn’t seem able to resolve or correctly identify/report.
When this is occurring, every future query to the wp_options table will queue up waiting for the table to be usable, thus leading to the max_user_connections being reached pretty quickly.
They went on to say they have only ever seen this behavior within WordPress and searching their forums or google will reveal tons of discussions of this type of issue, but unfortunately so far have not found any solution beyond disabling plugins/components in WordPress until finding the one that causes it.
This seems to be a very major issue which WordPress developers should be digging deeper into but so far I have seen no indication of investigation.
Finally, back in cPanel, it might also be worth using the Repair DB button, but if the database connections still get queued up the site may go offline again.
Update: May 5 2011
I see the WP-DBManager plugin has just been updated. Since deactivating the previous version, my sites have all been running smoothly. Think I’m going to hold off on this release and use phpMyAdmin for the moment…other page