您的位置:首页 > 数据库 > SQL

shell监控MySQL服务是否正常

2015-11-05 22:02 691 查看
监控MySQL服务是否正常,通常的思路为:检查3306端口是否启动,ps查看mysqld进程是否启动,命令行登录mysql执行语句返回结果,
[root@hujiali1~]#netstat-tunpl|grep3306|wc-l
1
[root@hujiali1~]#ps-ef|grepmysql|grep-vgrep|wc-l
2
[root@hujiali1~]#chmod+xcheck_mysql.sh
[root@hujiali1~]#./check_mysql.sh
MySQLisrunning
[root@hujiali1~]#catcheck_mysql.sh
#!/bin/bash
#writtenbymofansheng@2015-10-15
port=`netstat-nlt|grep3306|wc-l`
if[$port-ne1]
then
/etc/init.d/mysqldstart
else
echo"MySQLisrunning"
fi
[root@hujiali1~]#chmod+xcheck_mysql2.sh
[root@hujiali1~]#catcheck_mysql2.sh
#!/bin/bash
#writtenbyhujianli
process=`ps-ef|grepmysql|grep-vgrep|wc-l`
if[$process-ne2]
then
/etc/init.d/mysqldstart
else
echo"MySQLisrunning"
fi
端口号和进程都检测到才算mysql启动成功
#!/bin/bash
#writtenbymofansheng@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
"MySQLisrunning"
else
/etc/init
.d
/mysqld
start
fi
使用客户端登录mysql执行命令,查看返回结果测试服务是否启动,理论上此方法最可靠。
[root@localhostbaby]
#catcheck_db_client.sh
#!/bin/bash
#writtenbymofansheng@2015-10-15
mysql-uroot-p123.com-e
"selectversion();"
&>
/dev/null
if
[$?-
ne
0]
then
/etc/init
.d
/mysqld
start
else
echo
"MySQLisrunning"
fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 监控