shell脚本:监控MySQL服务是否正常

来源:转载

监控MySQL服务是否正常, 通常的思路为:检查3306端口是否启动,ps查看mysqld进程是否启动,命令行登录mysql执行语句返回结果,php或jsp程序检测(需要开发人员开发程序)等等;

方法1:监听3306端口

#!/bin/bash#written by [email protected]=`netstat -nlt|grep 3306|wc -l`if [ $port -ne 1 ]then /etc/init.d/mysqld startelse echo "MySQL is running"fi 方法2:查看mysqld进程

注意注意:如果使用进程过滤的话,脚本名称如果里面包含mysql的话,脚本执行有坑,切记!!!因为会把脚本也grep了一次,导致结果不准确;

[[email protected] baby]# cat check_mysql.sh#!/bin/bash#written by [email protected]=`ps -ef |grep mysql|grep -v grep |wc -l`if [ $process -ne 2 ]then /etc/init.d/mysqld startelse echo "MySQL is running"fi

执行结果如下:

[[email protected] baby]# sh check_mysql.sh

Starting MySQL SUCCESS!

改名之后执行为正常结果:

[[email protected] baby]# mv check_mysql.sh check_db.sh

[[email protected] baby]# sh check_db.sh

MySQL is running

方法3:双保险,进程和端口都成功才算mysql服务正常 #!/bin/bash#written by [email protected]=`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:使用客户端登录mysql执行命令,查看返回结果测试服务是否启动,理论上此方法最可靠。

[[email protected] baby]# cat check_db_client.sh#!/bin/bash#written by [email protected] -uroot -p123456 -e "select version();" &>/dev/nullif [ $? -ne 0 ]then /etc/init.d/mysqld startelse echo "MySQL is running"fi

执行结果如下:

[[email protected] baby]# sh check_db_client.sh

MySQL is running


分享给朋友:
您可能感兴趣的文章:
随机阅读: