zabbix 监控mysql主从状态与延迟

环境IP主机名
主数据库192.168.32.128WJX
从数据库192.168.32.134WJX2

1 监控主从状态

1.1 部署agent

[root@WJX2 ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz
[root@WJX2 ~]# tar xf zabbix-5.0.2.tar.gz
[root@WJX2 ~]# cd zabbix-5.0.2
[root@WJX2 zabbix-5.0.2]# ./configure --enable-agent
[root@WJX2 zabbix-5.0.2]# make install

[root@WJX2 etc]# useradd -r -M -s /sbin/nologin zabbix

[root@WJX2 zabbix-5.0.2]# cd /usr/local/etc/
[root@WJX2 etc]# vim zabbix_agentd.conf
...
Server=192.168.32.133
...
ServerActive=192.168.32.133
...
Hostname=002


[root@WJX2 ~]# zabbix_agentd
[root@WJX2 ~]# systemctl stop firewalld
[root@WJX2 ~]# systemctl disable firewalld
[root@WJX2 ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128            *:10050                      *:* 
LISTEN     0      128            *:22                         *:* 
LISTEN     0      100    127.0.0.1:25                         *:* 
LISTEN     0      32            :::21                        :::* 
LISTEN     0      128           :::22                        :::* 
LISTEN     0      100          ::1:25                        :::* 

1.2 部署 MySQL环境

配置主数据库
[root@WJX ~]# yum -y install mariadb*
[root@WJX ~]# systemctl enable --now mariadb

#在主数据库里创建一个同步账号授权给从数据库使用
[root@WJX ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-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)]> create user 'repl'@'192.168.32.134' identified by 'repl123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.32.134';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

[root@WJX ~]# vim /etc/my.cnf
log-bin = mysql_bin
server-id = 10

[root@WJX ~]# systemctl restart mariadb

#查看主库的状态
[root@WJX ~]# mysql
...
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000001 |      245 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
配置从数据库
[root@WJX2 ~]# yum -y install mariadb*
[root@WJX2 ~]# vim /etc/my.cnf
server-id = 20
relay-log = myrelay_log
[root@WJX2 ~]# systemctl enable --now mariadb

[root@WJX2 ~]# mysql
...
MariaDB [(none)]> change master to \
    -> master_host='192.168.32.128',
    -> master_user='repl',
    -> master_password='repl123',
    -> master_log_file='mysql_bin.000001',
    -> master_log_pos=245;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.32.128
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: myrelay_log.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql_bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 245
              Relay_Log_Space: 819
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
1 row in set (0.00 sec)

1.3 监控mysql主从

1.3.1 编写脚本

[root@WJX2 ~]# mkdir -p /scripts/zabbix
[root@WJX2 ~]# vim /scripts/zabbix/check_replication.sh
#!/bin/bash

mysql_status=$(mysql -uroot -e 'show slave status\G' 2>/dev/null|grep 'Slave.*Running'|grep -c 'Yes')

if [ $mysql_status -ne 2 ];then
    echo '1'
else
    echo '0'
fi

[root@WJX2 ~]# vim /usr/local/etc/zabbix_agentd.conf
...
UnsafeUserParameters=1
...
UserParameter=check_replication,/bin/bash /scripts/zabbix/check_replication.sh
...

[root@WJX2 ~]# pkill zabbix
[root@WJX2 ~]# zabbix_agentd

1.3.2 添加主机加入主机组

在这里插入图片描述

1.3.3 创建监控项及触发器

在这里插入图片描述
在这里插入图片描述

1.4 触发

[root@WJX2 ~ ]# systemctl stop mariadb

在这里插入图片描述

2 监控主从延迟

2.1 配置脚本

[root@WJX2 ~]# vim /scripts/zabbix/delay_replication.sh
#!/bin/bash

delay_value=$(mysql -uroot -e 'show slave status\G' 2>/dev/null|grep 'Seconds_Behind_Master'|awk -F'[: ]+' '{print $3}')
echo $delay_value

[root@WJX2 ~]# vim /usr/local/etc/zabbix_agentd.conf
...
UserParameter=delay_replication,/bin/bash /scripts/zabbix/delay_replication.sh
...

[root@WJX2 ~]# pkill zabbix
[root@WJX2 ~]# zabbix_agentd 

2.2 添加监控和触发器

在这里插入图片描述
在这里插入图片描述

2.3 触发验证

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Zabbix中监视MySQL主从状态,需要执行以下步骤: 1. 确保MySQL主从复制已正确配置并正在运行。可以通过在主服务器上运行SHOW MASTER STATUS; 和在从服务器上运行SHOW SLAVE STATUS; 来检查复制状态。 2. 在MySQL主服务器上创建一个具有适当权限的MySQL用户以供Zabbix使用。可以使用以下命令创建用户: CREATE USER 'zabbix'@'localhost' IDENTIFIED BY 'password'; GRANT REPLICATION CLIENT ON *.* TO 'zabbix'@'localhost'; 3. 在Zabbix服务器上安装MySQL监视器模板。该模板包含用于监视MySQL服务器的预定义项和触发器。 4. 在Zabbix服务器上创建一个MySQL主服务器主机,将其与MySQL监视器模板关联,并配置主机的连接参数。这些参数应包括MySQL主服务器的IP地址、端口和上一步中创建的MySQL用户的凭据。 5. 在Zabbix服务器上创建一个MySQL从服务器主机,将其与MySQL监视器模板关联,并配置主机的连接参数。这些参数应包括MySQL从服务器的IP地址、端口和上一步中创建的MySQL用户的凭据。 6. 等待一段时间,以便Zabbix收集有关MySQL主从复制状态的数据。可以通过查看Zabbix监视器模板中的图形和报告来检查这些数据。 7. 如果需要,可以根据需要创建自定义Zabbix触发器,以便在MySQL主从复制状态出现问题时接收警报。 请注意,这只是一个基本的概述。实际的实施可能因环境和要求的不同而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值