shell監控MySQL服務是否正常

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

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

1

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

2

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


[root@hujiali1 ~]# ./check_mysql.sh

MySQL is running

[root@hujiali1 ~]# cat check_mysql.sh

#!/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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章