Auto Restart MySQL When It Crashes Ubuntu 18.04

Good Day, long enough not blogging on this site. This is my problem when I’m moving some websites into my cloud servers. Sometimes when my traffic going very high MySQL server crashes and I need to run it manually or reboot my server. This is really bad for SEO because my servers uptime will changed.

I am googling about this problem to find solution for resolve this issue and finally I found one is working for me.  Here is the steps you need to follow :

Create file (example /etc/startifdown.sh and write this code inside it:

#!/bin/bash
#Scripts to start services if not running
ps -ef | grep nginx |grep -v grep > /dev/null
if [ $? != 0 ]
then
/etc/init.d/nginx start > /dev/null
fi
ps -ef | grep php5-fpm |grep -v grep > /dev/null
if [ $? != 0 ]
then
/etc/init.d/php5-fpm start > /dev/null
fi
ps -ef | grep mysql |grep -v grep > /dev/null
if [ $? != 0 ]
then
/etc/init.d/mysql start > /dev/null
fi
Save it and chmod 755 for this file to allow it executed. Next step, Manually stop your MySQL server and try to run run this script to make sure it’s worked. If it turn back your MySQL server on then move to next step.
To make this script run every minutes you can put it on crontab job. Type crontab -e and choose your favorites editor…
Done, This cron will run each minute to check if your MySQL server down and then restart it, If your MySQL server is run it will skip to start MySQL. That’s for today, Merry Christmas and Happy New Year everyone!

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.