Actually we have discussed it for WordPress Database Connection Error. MySQL, Percona, MariaDB Will Restart on Failure With Configured my.cnf. Wrong Concepts Around Auto Restart MySQL, mysqld are discussed in this guide plus needed my.cnf
configuration shown. These two questions are frequently asked :
- Is there an option to automatically restart MySQL after a crash?
- Is there any script for automatic restart of the MySQL after a crash?
Auto Restart MySQL, mysqld : Wrong Concepts
MySQL, Percona, MariaDB – all basically same at this part. All has auto-restart system if installed from repo. At least for Deb GNU/Linux (Debian, Ubuntu) we have never seen any problem. Your concept is wrong – if mysqld crashed then mysqld_safe would restart it automatically because mysqld_safe is the control script. Provided that, your my.cnf
looks right and restarted msqld / MySQL normally like :
1 | service mysql restart |
that is what written here for mysqld_safe
:
---
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.
Restart mysql after the changes (if you need) :
1 | service mysql restart |
If still your MySQL do not auto-restart, then read the next sub header.
Auto Restart MySQL, mysqld : Check Your Error Log
Where is your error log, that you’ll know!. Suppose log is at /var/log/mysqld.log
, then cat it :
1 | cat /var/log/mysqld.log |
Here is an example of error with huge output, this is real example.
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. |
You need to fix the error of your error log. Your database may be corrupted or my.cnf
has wrong settings, that is not getting restarted.