Monit程序监控MySQLD失效怎么办?数据库宕机解决方法

由于之前linux服务器的MySQL数据库经常失灵,很多人也是不知道怎么样才好解决?

所以使用Monit监控程序监控MySQL,但是Monit监控MySQL经常无故失效……

Monit程序监控MySQLD失效怎么办?数据库宕机解决方法插图

Monit程序监控MySQL邮件警报通知

如果Monit监控程序MySQL数据库出现问题,会收到以下邮件通知。

monit alert — Timeout mysql

Timeout Service mysql
Date: Fri, 05 Feb 2021 04:55:47
Action: unmonitor
Host: chenweiliang.com
Description: service restarted 4 times within 4 cycles(s) – unmonitor
Your faithful employee,
Monit

monit alert — Does not exist mysql

Does not exist Service mysql
Date: Fri, 05 Feb 2021 04:47:21
Action: restart
Host: chenweiliang.com
Description: process is not running
Your faithful employee,
Monit

monit alert — Execution failed mysql

Execution failed Service mysql
Date: Fri, 05 Feb 2021 04:47:53
Action: alert
Host: chenweiliang.com
Description: failed to start (exit status 0) — no output
Your faithful employee,
Monit

如果收到以下邮件通知:

monit alert — Does not exist mysql

即表示MySQL数据库的PID文件不存在或路径不对。

此时,可通过查询mysql的pidfile命令,找到MySQL数据库进程的pidfile文件在哪里。

以下2个SSH命令,都能查看MySQL数据库进程的pidfile文件 ▼

ps -ef|grep mysqld

watch -n 1 "ps -ef | grep mysql"

SSH返回查询结果如下:

root 6206 1 0 16:19 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/CentOS-UFO.pid
mysql 6280 6206 3 16:19 ? 00:00:22 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/lib/mysql/centos-ufo.err --pid-file=/var/lib/mysql/centos-ufo.pid
root 6691 1844 0 16:29 pts/0 00:00:00 grep mysqld

登录Monit Service Manager

建议浏览器登录Monit Service Manager启动MySQL服务:

自己的域名:2812/

说明:

登录后,可查看Monit监控的服务进程:

Process Status Uptime CPU Total Memory Total
apache Running 8m 0.0% 4.6% [23.3 MB]
mysqld Not monitored 26m 1.6% 19.3% [96.4 MB]

若服务进程不处于“Running”的状态,可以点击服务名称,比如“mysqld”进入该服务进程的管理页面。

然后,点击以下右下角的“Enable monitoring”启动。

Process status

Parameter Value
Name mysqld
Pid file /var/lib/mysql/do.ufo.org.in.pid
Status Not monitored
Group database
Monitoring mode active
Monitoring status Not monitored
Start program ‘/etc/init.d/mysql start’ timeout 30 second(s)
Stop program ‘/etc/init.d/mysql stop’ timeout 30 second(s)
Process id
Parent process id
UID
Effective UID
GID
Process uptime
Children
CPU usage
Total CPU usage (incl. children)
Memory usage
Total memory usage (incl. children)
Port Response time  
Data collected Mon, 21 Aug 2017 12:39:25
Timeout If restarted 3 times within 5 cycle(s) then unmonitor
Existence If doesn’t exist then restart
Port If failed [localhost]:3306 type TCP/IP protocol DEFAULT with timeout 5 seconds then restart

当Status(状态)处于“Running”时,即表示已经成功启动该进程监控了。

更换监控mysql的配置

不过,陈沩亮之前研究发现,另外也可以通过更换监控mysql的配置,让监控mysql失灵后重新激活monit程序监控mysql,此方法比较麻烦,所以仅供参考,不推荐使用。

实际操作:

然后,进入monit日志“monit.log”的目录路经:

cd /var/log

重新启动monit程序:

service monit restart

再测试重新启动mysqld:

service mysqld restart

按“CRRL+R”刷新查看monit日志“monit.log”是否有重启mysql的记录?

有类似如下记录表示成功重新激活monit程序监控mysql:

[UTC Oct 23 06:37:55] error : ‘mysql’ process PID changed from 3521 to 4238

[UTC Oct 23 06:37:55] error : ‘mysql’ process PPID changed from 3313 to 4030

[UTC Oct 23 06:38:25] info : ‘mysql’ process PID has not changed since last cycle

[UTC Oct 23 06:38:25] info : ‘mysql’ process PPID has not changed since last cycle

下面是我用monit监控mysql配置:

monit配置监控mysql(一)

# #监控mysql(一) # check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = "/etc/init.d/mysqld start"  stop program = "/etc/init.d/mysqld stop"  if failed host localhost port 3306 for 3 times within 4 cycles then alert #若在四个周期内,三次 3306(Mysql)端口都无法连通,则邮件通知 if 5 restarts within 5 cycles then timeout

monit配置监控mysql(二)

# #监控mysql(二) # check process mysql with pidfile /var/run/mysqld/mysqld.pid  start program = "/etc/init.d/mysqld start" with timeout 10 seconds  stop program = "/etc/init.d/mysqld stop"  if failed host localhost port 3306 protocol mysql  with timeout 10 seconds  then restart  if 3 restarts within 5 cycles then timeout  if changed pid then alert  group webserver

monit配置监控mysql(三)

# #监控mysql # check process mysql with pidfile /var/run/mysqld/mysqld.pid group database start program = "/etc/init.d/mysqld start" stop program = "/etc/init.d/mysqld stop" if failed port 3306 then restart if 3 restarts within 10 cycles then timeout

后记:之前本人负责优化的网站 外星人UFO真相 经常遇到数据库奔溃、MySQL无故暂停的问题,现在经过以上的解救方案终于解决好了。

要是还没能解决,推荐参考以下教程 ▼

如何在CentOS Webpanel(CWP7)安装Monit监控软件?

在本教程中,我们将向你展示如何在CentOS 7上的CentOS Webpanel(CWP7)安装Monit监控。 Monit监控是什么? Monit监控是一个免费的开源软件,它是一个非常有用的Linux监控程序。它可以……

Monit程序监控MySQLD失效怎么办?数据库宕机解决方法插图1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注