The following script uses the namp scanning tool, so if the nmap scanning tool is not installed on the system, you need to install it first.
Function of the script:
First, detect whether the mysql port exists normally. If the port does not exist, start the mysql service. Of course, the location of the startup script will be different according to your installation path. Then if the port exists, the mysql service is running. Then check the mysql status to see if it can connect normally. If it can connect normally, it means mysql is normal and no processing will be done. If it cannot connect normally, kill all mysql processes and then start the mysql service. Why do you want to kill the process instead of performing normal operations? Restarting is because sometimes mysql cannot be shut down normally, so it cannot be restarted normally, so the safest way 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