MySQL Setup & Corrupted Database Has to Do With the Error Establishing a Database Connection. All know – restarting MySQL from SSH fixes the issue instantly in majority of the cases! We Will Fix WordPress Error in Database Connection in this Guide Alongwith Basic Error Log Analysis. For whom this article is intended? Advanced users or who are learning to become advanced users. This guide is for dedicated server, cloud server instance, traditional VPS etc. but not for Shared Hosting. It is meaningless to pay $15/month for a Shared Hosting when at $10/month one can get one 01 GB cloud server instance.
We expect that, the user is using Percona or MySQL or MariaDB and know how to do SSH!
Error Establishing a Database Connection is more common in DigitalOcean, Linode etc. over HP Cloud, Rackspace Cloud with everything being the same. We do not know the reason. We guess, these are related to network card or router working behind – minor issue get magnified. Rackspace Cloud cost huge for the tuning works at backend!
---
Fix WordPress Error in Database Connection : MySQL Part
WordPress database is quite pathetic in real life. If you have ever used Apache2 (you means your database) and now using Nginx, there is enough chance of getting Error Establishing a Database Connection message. First divine work is to eliminate the chance of corrupted database, it may not be so easy as we wrote here – Fix Sudden Error Establishing a Database Connection.
Above hyperlinked way is an easy method, just add :
1 | define('WP_ALLOW_REPAIR', true); |
in /wp-config.php
file and visit https://this_is_your_site.com/wp-admin/maint/repair.php
on browser and follow the web instruction. Automatic system. Wait! Read ahead. Yes, very risky without a database backup. Easy to take a backup from SSH :
1 | mysqldump -h localhost -u root -p you_wp_db_name > wpbackup.sql |
I will suggest to create a database beforehand :
1 2 3 | CREATE DATABASE backupwp; use backupwp; \. wpbackup.sql |
With that last line, from dump, database will be created. Few minutes matter. So, your backup database username is root
with your password, database name is backupwp
. Live working backup of database. Now run that database repair of WordPress. If sky breaks on the head, change credential in /wp-config.php
file to that backup’s one. Simple.
Fix WordPress Error in Database Connection : mysql_safe is Idiot
mysqld
is odd. Run mysqld --verbose --help
in SSH screen. MySQL should never die in that way throwing an abnormal error. MySQL Safe actually kept as failsafe way to not die :
1 | https://dev.mysql.com/doc/mysql-startstop-excerpt/5.5/en/mysqld-safe.html |
Basically, by default, at my.cnf
under [mysqld_safe]
, all needed are not present, socket, PID if not present, probably mysqld can not restart on crash (I am not MySQL database expert) :
1 2 3 4 5 6 | [mysqld_safe] pid-file = /var/run/mysqld/mysqld.pid socket= /var/run/mysqld/mysqld.sock nice= 0 log_error=/var/log/mysqld.log datadir=/var/lib/mysql |
In real life – you really never need an external script to restart MySQL. Yeah, the above thing kill keep MySQL server up but, it may still die at some action when you’ll perform. It is unlikely to fail to restart on a well tuned my.cnf.
Fix WordPress Error in Database Connection : Never Use Script, Software to Restart MySQL on Failure
People uses monit to auto restart MySQL on failure but without reading software engineering, I can definitely say – that Indian bloggers promoted monit to auto restart MySQL on failure is wrong approach. MySQL is very powerful software. Upon any sort of attack on MySQL, it is better if gets shut down. Do not do idiotic works. Log files are not for decoration. This is a very basic log analysis followed by fix. Our servers never need such “auto restart mysql on crush script”? Billions of WordPress are running on this Earth without such idiotic methods. If some error of MySQL can not be fixed by you, you need a good MySQL expert. Do not find a cheap Indian “MySQL Expert”. Good person around MySQL will charge the same all around the World.
Tap that /var/log/mysqld.log
:
1 | cat /var/log/mysqld.log |
Here is an example of error with huge output.
In the log, there is clear history of crushes :
1 2 | 2015-10-27 08:10:16 7841 [Note] Starting crash recovery... 2015-10-27 08:10:16 7841 [Note] Crash recovery finished. |
As you can see, for that database, there are errors which are clearly possible to fix. WordPress database need fix :
1 | Unsafe statement written to the binary log using statement format since BINLOG_FORMAT |
Another one in combination :
1 | Too many connections |
Everyone knows to fix – run locate my.cnf
and find the path of my.cnf
and open that with a text editor :
1 | nano /etc/mysql/my.cnf |
search for max_connections
and increase the value. Gracefully restart mysql (service mysql restart
for Ubuntu). If you get :
1 2 | * Stopping MySQL (Percona Server) mysqld [ OK ] * Starting MySQL (Percona Server) database server mysqld [fail] |
that means, max_connections
value was edited to too high. Lower it & gracefully restart mysql. After solving all the problems, empty the log file :
1 | echo " " > /var/log/mysqld.log |