监控mysql运行状态

本文介绍了使用简单的shell脚本/supervisor来监控mysql的运行情况,如果停止则重启。

前言

最近发现MySQL服务隔三差五就会挂掉,导致我的网站无法正常运作,WordPress提示“建立数据库连接时出错”。

虽然我自己有网站监控和邮件通知,但是好多时候还是需要我来手动连接我的服务器重新启动一下我的MySQL,这样简直太不友好了.

所以,需要一个脚本,定时监控它,如果发现mySQL挂掉了就重启它。

好了,闲言碎语不多讲,开始我们的配置之旅。

运行环境:

Distributor ID: Debian
Description:    Debian GNU/Linux 7.9 (wheezy)
Release:        7.9
Codename:       wheezy

新建bash脚本:

首先,我们要编写一个shell脚本,脚本主要执行的逻辑如下:

显示mysqld进程状态,如果判断进程未在运行,那么输出日志到文件,然后启动mysql服务,如果进程在运行,那么不执行任何操作,可以选择性输出监测结果。

可能大家对于shell脚本比较陌生,在这里推荐官方的shell脚本文档来参考一下

shell脚本的后缀为sh,在任何位置新建一个脚本文件,我选择在 /etc/mysql 目录下新建一个 listen.sh 文件。

执行如下命令:

cd /usr/local/sbin/
touch check_mysql.sh
vi check_mysql.sh

i添加如下内容:

#!/bin/bash
pgrep mysqld &> /dev/null
if [ $? -gt 0 ]
then
echo "`date` mysql is stop" >>/var/log/check_mysql.log
service mysql start
else
echo "`date` mysql running" >>/var/log/check_mysql.log
fi

其中 pgrep mysqld 是监测mysqld服务的运行状态,&> /dev/null 是将其结果输出到空文件,也就是不保存输出信息

$? 是拿到上一条命令的运行结果,-gt 0 是判断是否大于0,后面则是输出时间到日志文件,然后启动mysql,否则不启动mysql

Tips:写完之后可以在脚本所在目录下使用./check_mysql.sh执行脚本,看看log中是否有打印信息,判断脚本执行情况。

赋予脚本权限

+x 是代表可执行权限:

chmod +x check_mysql.sh

设计定时任务

加入crontab,让系统五分钟检测一次mysql状态

#crontab -e

i,调整光标,在文件的最后添加:

*/5 * * * * /usr/local/sbin/check_mysql.sh > /dev/null 2>&1

crontab命令相关基础知识,请点击

查看定时任务效果:

cat /var/log/check_mysql.log

输出效果:

Tue Feb 23 03:25:01 UTC 2016 mysql running
Tue Feb 23 03:30:01 UTC 2016 mysql running
Tue Feb 23 03:35:01 UTC 2016 mysql running
Tue Feb 23 03:40:01 UTC 2016 mysql running
Tue Feb 23 03:45:01 UTC 2016 mysql running
Tue Feb 23 03:50:01 UTC 2016 mysql running
Tue Feb 23 03:55:01 UTC 2016 mysql running
Tue Feb 23 04:00:01 UTC 2016 mysql running
Tue Feb 23 04:05:01 UTC 2016 mysql running
Tue Feb 23 04:10:01 UTC 2016 mysql running
Tue Feb 23 04:15:01 UTC 2016 mysql running
Tue Feb 23 04:20:01 UTC 2016 mysql running
Tue Feb 23 04:25:01 UTC 2016 mysql running
Tue Feb 23 04:27:21 UTC 2016 mysql running
Tue Feb 23 04:28:00 UTC 2016 mysql running
Tue Feb 23 04:28:06 UTC 2016 mysql running
Tue Feb 23 04:28:53 UTC 2016 mysql running
Tue Feb 23 04:30:01 UTC 2016 mysql running

使用supervisord进行监控(推荐)

安装supervisor

在centos中:

yum install python-setuptools
easy_install supervisor
pip install supervisor

在Debian/Ubuntu中:

apt-get install supervisor

配置supervisor

echo_supervisord_conf > /etc/supervisord.conf

vim /etc/supervisord.conf

在最后添加监控模块:

[program:mysql]
command=/etc/inint.d/mysql start
autostart=true
autorestart=true
startretries=10
stderr_logfile=/var/log/mysql/str-err.log
stdout_logfile=/var/log/mysql/str-out.log

运行与监控

运行:

supervisord -c /etc/supervisord.conf	

监控:

# 停止某一个进程,program_name 为 [program:x] 里的 x
supervisorctl stop program_name
# 启动某个进程
supervisorctl start program_name
# 重启某个进程
supervisorctl restart program_name
# 结束所有属于名为 groupworker 这个分组的进程 (start,restart 同理)
supervisorctl stop groupworker:
# 结束 groupworker:name1 这个进程 (start,restart 同理)
supervisorctl stop groupworker:name1
# 停止全部进程,注:start、restart、stop 都不会载入最新的配置文件
supervisorctl stop all
# 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
supervisorctl reload
# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
supervisorctl update