监控MYSQL服务是否异常

 

问题描述:监控MYSQL服务是否正常启动,如果未正常启动,就启动MYSQL

判断mysql的方法:

1)端口判断

[root@localhost ~]# netstat -lntup | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2288/mysqld

2)进程判断

[root@localhost ~]# ps -ef | grep mysqld
mysql 2071 1 0 11:32 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe –basedir=/usr
mysql 2288 2071 0 11:32 ? 00:00:24 /usr/libexec/mysqld –basedir=/usr –datadir=/var/lib/mysql –plugin-dir=/usr/lib64/mysql/plugin –log-error=/var/log/mariadb/mariadb.log –pid-file=/var/run/mariadb/mariadb.pid –socket=/var/lib/mysql/mysql.sock
root 10097 6962 0 17:27 pts/1 00:00:00 grep –color=auto mysqld

3)返回值判断

[root@localhost ~]# mysql -uroot -proot -e “select version();” &>/dev/null
[root@localhost ~]# echo $?
0

法一:实现思路是过滤出MYSQL端口3306进行判断:

#!/bin/bash
port=`netstat -lnt|grep 3306|wc-l`
if [ $port -ne 1 ]
then
/etc/init.d/mysql start
else
echo “MySQL is running.”
fi

执行结果:
[root@localhost ~]# sh mysql1.sh
MySQL is running.

法二:实现思路是通过MYSQL进程进行判断:

#!/bin/bash
portcess=`ps -ef|grep mysql|grep -v grep|wc -l`
if [ $portcess -ne 2 ]
then
/etc/init.d/mysqld start
else
echo “MySQL is running.”
fi

执行结果:
[root@localhost ~]# sh mysql1.sh
MySQL is running.

注意:过滤的字符串‘mysql’不要在脚本名字出现,如果出现则不准
法三:实现思路是通过web连接返回值判断:

#!/bin/bash
mysql -uroot -proot -e “select version();” &>/dev/null
if [ $? -ne 0 ]
then
/etc/init.d/mysqld start
else
echo “MySQL is running.”
fi

执行结果:
[root@localhost ~]# sh mysql1.sh
MySQL is running.

摘自:http://blog.51cto.com/baishuchao/1942148

 

标签:

分类:SERVER | 发布:inzaghi | 评论:监控MYSQL服务是否异常已关闭评论 | 发表时间:2019-06-19 11:17
引用:点击这里获取该日志的TrackBack引用地址
上一篇:
下一篇:

Comments are closed.

Design By Inzaghi | 京ICP备16047555号-1