shell監控MySQL服務是否正常

監控MySQL服務是否正常,一般的思路爲:檢查3306端口是否啓動,ps查看mysqld進程是否啓動,命令行登陸mysql執行語句返回結果,mysql

[root@hujiali1 ~]#  netstat -tunpl | grep 3306 |wc -lsql

1bash

[root@hujiali1 ~]# ps -ef | grep mysql | grep -v grep |wc -lide

2測試

[root@hujiali1 ~]#chmod +x check_mysql.shspa


[root@hujiali1 ~]# ./check_mysql.sh命令行

MySQL is runningcode

[root@hujiali1 ~]# cat check_mysql.shorm

#!/bin/bash進程

#written by mofansheng@2015-10-15


port=`netstat -nlt|grep 3306|wc -l`

if [ $port -ne 1 ]

then

 /etc/init.d/mysqld start

else

 echo "MySQL is running"

fi


[root@hujiali1 ~]#chmod +x check_mysql2.sh

[root@hujiali1 ~]#cat check_mysql2.sh

#!/bin/bash

#written by hujianli


process=`ps -ef |grep mysql|grep -v grep |wc -l`

if [ $process -ne 2 ]

then

 /etc/init.d/mysqld start

else

 echo "MySQL is running"

fi


端口號和進程都檢測到纔算mysql啓動成功

#!/bin/bash
#written by mofansheng@2015-10-15
 
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  start

fi


使用客戶端登陸mysql執行命令,查看返回結果測試服務是否啓動,理論上此方法最可靠。

[root@localhost baby] # cat check_db_client.sh
#!/bin/bash
#written by mofansheng@2015-10-15
 
mysql -uroot -p123.com -e  "select version();"  &> /dev/null
if  [ $? - ne  0 ]
then
  /etc/init .d /mysqld  start
else
  echo  "MySQL is running"
fi
相關文章
相關標籤/搜索