Fix Sudden Error Establishing a Database Connection in WordPress By Following Our Logical Flow Chart Without Taking Much Sudden Headache. Causes of Sudden Headaches are not really great ! We are talking about fixing the typical appearance of white page declaring Error Establishing a Database Connection in WordPress all of a sudden in a five minutes ago working great website. This is not for those who could never establish a Database Connection or getting MySQL Gone Away Error. Many years ago we published an article – Tips to Optimize WordPress Running on Dedicated Server, although with time; the links or tips are not fully applicable now anymore but actually the basic issue is with resource allocation to MySQL database.
Error Establishing a Database Connection WordPress : For Those Who Has No Idea About Technical Parts
Those who has no idea about SQL, MySQL, PHPMyAdmin or what type of web hosting you are using, it is better to take someone other’s help. Usually your web hosting company’s support will work fine. Do not try to experiment on trial and error basis.
Fix Sudden Error Establishing a Database Connection WordPress
This typical error usually appears when working on your site or making some changes and sometimes side by side published a new post; suddenly you see the dreaded white page with the error message “Error Establishing a Database Connection”. Usually it happens with One Server running both MySQL Server and Apache2 / Ngnix Web Server Software. It is rare for 2 or multiple server setup. If you are using a server without a web hosting control panel (like Rackspace Cloud Server, Amazon or MediaTemplate’s high end servers), the fix is usually to SSH as root and simply restart MySQL server :
sudo /etc/init.d/mysql restart
Then restart Apache or perform a full reboot :
# restart apache
Usually error will go away. Reasons can be many, but usually it happens due to buggy plugins.
If the situation is to bad and a reboot does not solve, open your
wp-config.php file and add at the bottom :
If you point your browser towards your website’s this url :
you will be able to repair the corrupted database. It is important to double check the database details (unlikely they will be wrong – since 5 minutes ago they were working nicely). Sometime’s with wrong chmod value, deactivating W3 Total Cache like plugin can make the whole
wp-config.php white – instead of deleting one single line, it fully deletes all the lines. This is why we suggest to change the ownership of wp-config.php to root instead of giving it to Apache (
www-data) and chmod it to as low value as possible so that no Plugin can overwrite it.
Actually you need to optimize your WordPress installation and most importantly, tweak MySQL Server to perform better. If you have recently migrated, you can try to run this SQL command (edit the statements, url etc.) :
UPDATE YOUR DATABASE NAME.wp_options SET option_value = 'http://YOURURL' WHERE wp_options.option_id =2 LIMIT 1 ;
Plesk, cPanel, Webmin etc. web software usually will run normally (if installed), so as Apache, PHP.
Plesk, cPanel, Webmin are more related to Perl than MySQL. GoDaddy, Bluehost etc. shared, VPS face these errors due to inadequate allocated resource. Try to upgrade to better web host which are standard now like Rackspace, Amazon, HP Cloud etc. It is not that, you’ll never get Sudden Error Establishing a Database Connection on these hosts, but actually you can allocate more resource to MySQL server or use a Database as a Service. Primarily it happens out of a protective mechanism to prevent destruction of the data saved on database. With sudden spike of increased need of RAM for MySQL (just to make it simple), MySQL Server stops to execute anything.
Once you saw it, use some web software to monitor up time and ping you when the server is not working properly. Not all service will give alert for MySQL errors.