[Introduction] The following script uses the namp scanning tool, so if the nmap scanning tool is not installed in 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 is
. The following script uses the namp scanning tool, so if If the nmap scanning tool is not installed, you need to install it first.
Function of the script:
First, check 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 depend on your installation path. If there is a difference, then if the port exists, there is a mysql service 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. Then start the mysql service. Why do you want to kill the process and not restart it normally? 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
The above is the detailed content of Introducing a script to detect MySQL status. For more information, please follow other related articles on the PHP Chinese website!