Functions of the script:
First of all, detect whether the MySQL port is normal, the port does not exist on the implementation of the start of the MySQL service, of course, the location of the startup script according to your installation path will be different, and then if the port exists, there is a MySQL service running, then detect the MySQL state, can not connect If the normal connection to the normal MySQL, do not do any processing, if not normal connection, then kill all the MySQL process, and then start the MySQL service, why to kill the process without a normal restart it is because sometimes MySQL can not normally shut down, so can not normal restart, So the insurance practice is to kill the process and then start the service.
#!/bin/bash
/usr/bin/nmap localhost |grep 3306
if [ $? -eq 0 ]
then
/usr/local/mysql/bin/mysql -uxxxxx -pxxxxxx --connect_timeout=5 -e "show databases;"
if [ $? -ne 0 ]
then
/bin/ps aux |grep mysql |grep -v grep | awk ’{print $2}’ | xargs kill -9
/usr/local/mysql/bin/mysqld_safe --user=mysql & > /dev/null
fi
else
/usr/local/mysql/bin/mysqld_safe --user=mysql & > /dev/null
fi