監控MySQL服務是否正常,一般的思路爲:檢查3306端口是否啓動,ps查看mysqld進程是否啓動,命令行登陸mysql執行語句返回結果,
mysql
[root@hujiali1 ~]# netstat -tunpl | grep 3306 |wc -l
sql
1
bash
[root@hujiali1 ~]# ps -ef | grep mysql | grep -v grep |wc -l
ide
2
測試
[root@hujiali1 ~]#chmod +x check_mysql.sh
spa
[root@hujiali1 ~]# ./check_mysql.sh
命令行
MySQL is running
code
[root@hujiali1 ~]# cat check_mysql.sh
orm
#!/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