Geeky, Hosting April 17, 2016 Skippy One comment

restart MySQL if it dies

Because MySQL is sometimes a crazy pain in the arse, and will kill itself every once in awhile without warning, and apparently without any meaningful reason, (I only know it has happened when I get a SMS from my uptime monitor), I have been looking into a better way of managing it than SSHing into the box and running service mysql restart,

This is not a clean solution! it would obviously be better to avoid the problem in the first place. Anyway. If so, you can check if the mysql service is running with service mysql status

This command will exit successfully if mysql is running and return a non 0 exit status if it is not. You can therefore start the service if it is not running with this command: `service mysql status || service mysql start`

You can add a file to  line to /etc/cron.d/ called mysql to launch this command every minute:

* * * * * service mysql status || service mysql start

One thought on “restart MySQL if it dies

  1. Monit would probably be a cleaner solution though – you can set it to detect MySQL being down and then restart/alert as appropriate. 🙂

Leave a Reply