Zabbix监控mysql主从复制

案例环境:

系统

IP地址

主机名

所需软件

硬件

Centos 7.4 1708 64bit

192.168.100.102

master.mysql.com

boost_1_59_0.tar.gz

mysql-5.7.12.tar.gz

zabbix-3.4.2.tar

内存:4G

Centos 7.4 1708 64bit

192.168.100.103

slave.mysql.com

boost_1_59_0.tar.gz

mysql-5.7.12.tar.gz

zabbix-3.4.2.tar

内存:4G

案例步骤:

  • 在master主机上安装ntp时间服务;
  • 在两台slave主机中配置同步master的ntp时间(在此配置相同,在此只列举slave1的配置);
  • 安装二台Mysql数据库服务器(在此步骤相同,在此只列举master主机一台的安装);
  • 优化配置Master主机的mysql服务;
  • 优化配置slave1主机的mysql服务;;
  • 配置master主机上的master角色,配置主节点;
  • 配置两台slave主机的slave角色,配置从节点(在此配置相同,在此只列举slave1的配置);
  • 配置Master角色指定主从复制中特定的数据库及优化;
  • 验证Slave角色的数据库复制情况;
  • 安装zabbix_master
  • 配置监控:

安装zabbix客户端:(master,slave相同)

 [[email protected] ~]# vi /etc/hosts

192.168.100.106 server.zabbix.com

192.168.100.105 agent.zabbix.com

192.168.100.153 win2k8dc1

192.168.100.101 agent.nginx.com

192.168.100.102 master.mysql.com

192.168.100.103 slave.mysql.com

[[email protected] ~]# yum –y install ntpdate

[[email protected] ~]#ntpdate ntp1.aliyun.com

[ro [email protected] ~]# ls              

boost_1_59_0.tar.gz      zabbix-3.4.2  mysql-5.7.12.tar.gz  zabbix-3.4.2.tar.gz

[[email protected] ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[[email protected] ~]# yum -y install libxml2-devel libcurl-devel pcre-devel ntpdate

[[email protected] ~]# ntpdate ntp1.aliyun.com

[[email protected] ~]# wget -O zabbix-3.4.2.tar.gz http://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/3.4.2/zabbix-3.4.2.tar.gz/download

[[email protected] ~]# tar zxvf zabbix-3.4.2.tar.gz

[[email protected] ~]# cd zabbix-3.4.2

[[email protected] zabbix-3.4.2]# ./configure --prefix=/usr/local/zabbix --enable-master --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2

[[email protected] zabbix-3.4.2]# make &&make install

[[email protected] zabbix-3.4.2]# cp misc/init.d/tru64/ zabbix_agent /etc/init.d/

[[email protected] zabbix-3.4.2]# vi /etc/init.d/ zabbix_agent

  24 DAEMON=/usr/local/zabbix/sbin/ zabbix_agent

:wq

[[email protected] zabbix-3.4.2]# chmod +x /etc/init.d/ zabbix_agent

[[email protected] zabbix-3.4.2]# cd

[[email protected] ~]# groupadd zabbix

[[email protected] ~]# useradd -r -g zabbix zabbix

[[email protected] ~]# chown -R zabbix:zabbix /usr/local/zabbix/

[[email protected] ~]# vi /usr/local/zabbix/etc/ zabbix_agent.conf

:%g/^$/d

:%g/^#/d

 LogFile=/tmp/ zabbix_agent.log

  Server=192.168.100.105            ##zabbix服务端ip

  ServerActive=192.168.100.105       ##向谁发送数据

  Hostname=master.mysql.com 

:wq

[[email protected] ~]# /etc/init.d/zabbix_agent start

Zabbix master started.

[[email protected] ~]# netstat -utpln |grep zabbix

tcp        0      0 0.0.0.0:10050     0.0.0.0:*    LISTEN      12481/zabbix_masterd

tcp        0      0 :::10050          :::*        LISTEN      12481/zabbix_masterd

创建监控MYSQL的账号

Slave上操作:

[[email protected] -uroot -p123123

mysql> grant replication client on *.* to 'zabbix'@'localhost';

mysql> flush privileges;

mysql> exit

[[email protected] ~]# mysql -u zabbix -e "show slave status\G"

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.100.102

                  Master_User: linuxfan

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: master-bin.000002

          Read_Master_Log_Pos: 869

               Relay_Log_File: relay1-log-bin.000004

                Relay_Log_Pos: 1084

        Relay_Master_Log_File: master-bin.000002

             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: 869

              Relay_Log_Space: 1457

              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: 1

                  Master_UUID: 924af1a2-b5b6-11e8-af7f-000c294d7e05

             Master_Info_File: mysql.slave_master_info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

我们在输出的信息中选择

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

这两项来监控

我测试了一下,当操作的数据出现异常的时候,Slave_SQL_Running就会变成No

当执行slave  stop的时候,两个都会变成No

脚本内容如下

[[email protected] ~]# vi /tmp/zabbix_mysql_slave.sh

#!/bin/bash

/usr/local/mysql/bin/mysql -uzabbix -e 'show slave status\G' |grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes

[[email protected] ~]# chmod +x /tmp/zabbix_mysql_slave.sh

[[email protected] ~]# /usr/local/mysql/bin/mysql -uzabbix -e 'show slave status\G' |grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes  ##测试脚本内容

2

我只写了一条命令,大概解释一下

先用zabbix这个用户来获取slave的所有状态,然后grep出这两个状态,然后再输出第二列,最后查看有几个Yes状态的

正常情况下有两个Yes状态,

其实大家会发现,这只有一条命令,但我却把它写到了一个脚本里面,这里我说一下,因为如果你把命令写到配置文件里面,每次修改完,你都得重启agent,所以写到脚本里面更方便调试修改,这应该是一个好习惯。

 

在agent里面添加监控项

[[email protected] ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf

LogFile=/tmp/zabbix_agentd.log

Server=192.168.100.105

ServerActive=192.168.100.105

Hostname=slave.mysql.com

UserParameter=mysql.replication,/tmp/zabbix_mysql_slave.sh    ##等号后面的字符串中,逗号前面是Key,后面是执行的脚本

[[email protected] ~]# /etc/init.d/zabbix_agentd restart

Zabbix服务端测试key值:

[[email protected] ~]# zabbix_get -s 192.168.100.103 -k "mysql.replication"

2

如果主从复制正常,就会返回2 (代表两个状态都是Yes),现在就说明Server已经可以从agent获取状态了,现在在管理界面中先添加监控项

Web端添加

 

添加主机:

 

添加监控项:

 

 

创建图形:

 

查看图:

 

 

添加触发器:

返回的值小于2时,说明只存在1个YES或0个YES,这时候说明MYSQL主从同步出现异常,即产生报警;

 

 

 

猜你喜欢

转载自blog.csdn.net/Richardlygo/article/details/82689412
0条评论
添加一条新回复