当前位置: 移动技术网 > IT编程>数据库>Mysql > zabbix监控Mysql主从状态及延迟

zabbix监控Mysql主从状态及延迟

2020年07月23日  | 移动技术网IT编程  | 我要评论

1 . 监控Mysql主从状态

1 . 2 部署 agent 环境

数据库角色 IP 主机名
主数据库 192.168.25.132 node02-Linux.example.com
从数据库 192.168.25.133 node03-Linux.example.com

主数据库192.168.25.132已经安装了agent
从数据库安装agent

[root@node02-linux ~]# yum -y install wget vim gcc gcc-c++ pcre-devel
[root@node03-Linux ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz
[root@node03-Linux ~]# tar xf zabbix-5.0.2.tar.gz
[root@node03-Linux ~]# cd zabbix-5.0.2
[root@node03-Linux zabbix-5.0.2]# ./configure --enable-agent
[root@node03-Linux zabbix-5.0.2]# make install

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

[root@node03-Linux zabbix-5.0.2]# cd /usr/local/etc/
[root@node03-Linux etc]# vim zabbix_agentd.conf
...
Server=192.168.25.131
...
ServerActive=192.168.25.131
...
Hostname=MySQL_Slave1


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

1 . 3 部署 MySQL环境

安装配置主数据库

[root@node02-linux ~]# yum -y install mariadb*
[root@node02-linux ~]# systemctl enable --now mariadb

#在主数据库里创建一个同步账号授权给从数据库使用
[root@node02-linux ~]# 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.25.133' identified by 'repl123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.25.133';
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@node02-linux ~]# vim /etc/my.cnf
log-bin = mysql_bin
server-id = 10

#重启mysql服务
[root@node02-linux ~]# systemctl restart mariadb

#查看主库的状态
[root@node02-linux ~]# 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@node03-Linux ~]# yum -y install mariadb*
[root@node03-Linux ~]# vim /etc/my.cnf
server-id = 20
relay-log = myrelay_log
[root@node03-Linux ~]# systemctl enable --now mariadb

[root@node03-Linux ~]# mysql
...
MariaDB [(none)]> change master to \
    -> master_host='192.168.25.132',
    -> 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.25.132
                  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 . 4 监控Mysql主从状态

1 . 4 . 1 编写脚本及配置

[root@node03-Linux ~]# mkdir -p /scripts/zabbix
[root@node03-Linux ~]# 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@node03-Linux ~]# vim /usr/local/etc/zabbix_agentd.conf
...
UnsafeUserParameters=1
...
UserParameter=check_replication,/bin/bash /scripts/zabbix/check_replication.sh
...

[root@node03-Linux ~]# pkill zabbix
[root@node03-Linux ~]# zabbix_agentd

1 . 4 . 2 添加主机并加入主机组

在这里插入图片描述


在这里插入图片描述


1 . 4 . 3 创建监控项

在这里插入图片描述


1 . 4 . 4 添加触发器

在这里插入图片描述

1 . 4 . 5 触发验证

[root@node03-Linux ~]# systemctl stop mariadb

在这里插入图片描述


在这里插入图片描述

2 . 监控Mysql主从延迟

通过命令“show slave status\G;”获取Seconds_Behind_Master这一项的值,可以作为主从延迟的值

2 . 1 配置并编写监控脚本

[root@node03-Linux ~]# 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@node03-Linux ~]# vim /usr/local/etc/zabbix_agentd.conf
...
UserParameter=delay_replication,/bin/bash /scripts/zabbix/delay_replication.sh
...

[root@node03-Linux ~]# pkill zabbix
[root@node03-Linux ~]# zabbix_agentd 

2 . 2 添加监控项

在这里插入图片描述


在这里插入图片描述

2 . 3 添加触发器

在这里插入图片描述


在这里插入图片描述

为了直观的看到效果,我们把没有延迟作为告警

2 . 4 触发验证

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

本文地址:https://blog.csdn.net/qq996616/article/details/107502328

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网