Monitoring the MySQL service is normal, the general idea is : check whether the 3306 port is started, PS to see if the mysqld process starts, command line login MySQL execution statement return results, PHP or JSP program detection (Need developer development Program) and so on;
Method 1: Listen for 3306 ports
#!/bin/bash#written by [email protected]port= ' netstat-nlt|grep 3306|wc-l ' if [$port-ne 1]then/etc/init.d/mysqld start else echo "MySQL is running" fi
Method 2: View the MYSQLD process
Note: If you use process filtering, script name if it contains MySQL, script execution has a pit, remember!!! Because the script will also be grep once, resulting in inaccurate results;
[email protected] baby]# cat Check_mysql.sh#!/bin/bash#written by [email protected]process= ' Ps-ef |grep mysql|grep-v gr EP |wc-l ' If [$process-ne 2]then/etc/init.d/mysqld startelse echo "MySQL is running" fi
The results of the implementation are as follows:
[Email protected] baby]# sh check_mysql.sh
Starting MySQL success!
Renaming is performed after the normal result:
[Email protected] baby]# mv check_mysql.sh check_db.sh
[Email protected] baby]# sh check_db.sh
MySQL is running
Method 3: Double insurance, process and port success count MySQL service OK
#!/bin/bash#written by [email protected]port= ' netstat-nlt|grep 3306|wc-l ' process= ' ps-ef |grep mysql|grep-v grep |wc-l ' If [$port-eq 1] && [$process-eq 2]then echo "MySQL is running" Else/etc/init.d/mysqld Startfi
4: Use the client to log in to MySQL to execute the command to see if the test service is started, theoretically this method is the most reliable.
[email protected] baby]# cat Check_db_client.sh#!/bin/bash#written by [email protected]mysql-uroot-p123456-e "Select V Ersion (); "&>/dev/nullif [$?-ne 0]then/etc/init.d/mysqld startelse echo" MySQL is running "fi
The results of the implementation are as follows:
[Email protected] baby]# sh check_db_client.sh
MySQL is running
This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://mofansheng.blog.51cto.com/8792265/1703285
Shell script: Monitor MySQL service is normal