1. Navigate to /root.
$ cd /root
2. Create a new file
$ vim mysql.sh
or
$ vi mysql.sh
Also give the command below to check the logs
$ touch /root/mysql.log
3. Insert the following lines to the file
-----------------------------------
#!/bin/bash
/usr/bin/mysqladmin ping| grep 'mysqld is alive' > /dev/null 2>&1
if [ $? != 0 ]
then
/etc/init.d/mysqld stop
/etc/init.d/mysqld start
echo Starting at `date`
fi
-----------------------------------
4. Give execute permission.
$ chmod 755 mysql.sh
5. Set a cron to check it at regular intervals.
$ crontab -e
*/5 * * * * sh -x mysql.sh
(This will check every 5 minutes)