监控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