数据库
首页 > 数据库> > 自定义监控MySQL主从状态

自定义监控MySQL主从状态

作者:互联网

自定义监控MySQL主从状态

zabbix自定义监控Mysql主从状态和延迟

目录

监控Mysql主从状态

主从同步

监控mysql主从状态监控的是从库,所以只用在从库是安装并启动zabbix_aength

以下启动安装可参考

zabbix介绍与部署

这里实验使用的yum安装的mariadb,实战中请2进制安装mysql

mysql主从同步可参考

mysql主从介绍与配置

主库配置

#给主库改一个名字
[root@localhost ~]# hostnamectl  set-hostname nie
[root@localhost ~]# bash

#这里实验使用yum安装mariadb,并设置开机自启立即启动
[root@nie ~]# dnf -y install mariadb*
[root@nie ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

#更改mariadb数据库密码
[root@nie ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> set password = password('123456');
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> quit
Bye
[root@nie ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

#在主库上创建同步账号
MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.118.136' identified by '123456';
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> quit
Bye

#编写主库配置文件
[root@nie ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
...
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid

log-bin = mysql_bin   //启用binlog日志
server-id=10          //数据库服务器唯一标识符,此处需要小于从库
...
[root@nie ~]# systemctl restart mariadb

#查看主库状态
[root@nie ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show nie status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000001 |      328 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

从库配置

#从库中安装并启动zabbix_agent
[root@136 ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port      Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:22             0.0.0.0:*              
LISTEN  0       128            0.0.0.0:10050          0.0.0.0:*   

#这里实验使用yum安装mariadb,并设置开机自启立即启动
[root@136 ~]# dnf -y install mariadb*
[root@136 ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

#更改mariadb数据库密码
[root@136 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> set password = password('123456');
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> quit
Bye
[root@136 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

#编写从库配置文件
[root@136 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
...
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid

relay-log = myrelay
server-id = 20
...
[root@136 ~]# systemctl restart mariadb

#配置主从
[root@136 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> change nie to
    -> nie_host='192.168.48.141',
    -> nie_user='repl',
    -> nie_password='123456',
    -> nie_log_file='mysql_bin.000001',
    -> nie_log_pos=328;
Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for nie to send event
                   nie_Host: 192.168.118.130
                   nie_User: repl
                   nie_Port: 3306
                 Connect_Retry: 60
               nie_Log_File: mysql_bin.000001
           Read_nie_Log_Pos: 328
                Relay_Log_File: myrelay.000002
                 Relay_Log_Pos: 555
         Relay_nie_Log_File: mysql_bin.000001
              Slave_IO_Running: Yes         //此处必须为yes  
             Slave_SQL_Running: Yes         //此处必须为yes
               Replicate_Do_DB: 
...

测试验证

在主数据库中创建一个runtime数据库

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)

MariaDB [(none)]> create database runtime;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| runtime            |
+--------------------+
4 rows in set (0.000 sec)

在从库中查看,这里也多了一个runtime数据库,验证成功。

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| runtime            |
+--------------------+
4 rows in set (0.000 sec)

配置监控脚本

从库配置文件与编写脚本

#编写脚本
[root@136 ~]# vim /etc/scripts/mysql_status.sh
#!/bin/bash
yes=`mysql -uroot -p123456 -e " show slave status\G" 2> /dev/null |grep "Running:"|grep -c "Yes"`
if [ $yes == 2  ]; then
        echo "0"
else    
        echo "1"
fi 
[root@136 ~]# chmod +x /etc/scripts/mysql_status.sh

#在从库修改配置文件
[root@136 ~]# vim /usr/local/etc/zabbix_agentd.conf
...
UserParameter=check_mysql_status,/bin/bash /etc/scripts/mysql_status.sh

#重启服务生效配置
[root@136 ~]# pkill zabbix_agentd 
[root@136 ~]# zabbix_agentd 

服务端检查key是否可以使用

[root@zabbix ~]# zabbix_get -s 192.168.118.136 -k check_mysql_status
0

添加监控项

1

2

3

5

6

7

手动触发报警,查看报警信息

自定义监控MySQL延迟

配置监控脚本

监控延迟,我们需要关注 Seconds_Behind_Master这一项

//在被监控端,也就是从库,首先修改配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysqlyc,/bin/bash /scripts/mysqlyc.sh
 
//重启服务生效配置
[root@localhost ~]# pkill zabbix_agentd 
[root@localhost ~]# zabbix_agentd 
 
//再编写脚本
[root@localhost ~]# vim /scripts/mysqlyc.sh
 
#!/bin/bash
yc=`mysql -uroot -p123456 -e "show slave status\G" 2> /dev/null |awk '/Seconds_Behind_Master/ {print $2}' `
echo $yc
 
[root@localhost ~]# chmod +x /scripts/mysqlyc.sh 
 
//去服务端检查key是否可用
[root@zabbix ~]# zabbix_get -s 192.168.48.151 -k check_mysqlyc
0

用户管理用户组

1

2

3

用户角色

4

5

用户

8

11

13

14

模板管理

16

18

19

20

21

23

标签:none,mariadb,自定义,MySQL,nie,mysql,MariaDB,root,主从
来源: https://www.cnblogs.com/nie123/p/16667889.html