zabbix监控mysql主从与主从延迟


文章目录

  • zabbix监控mysql主从与主从延迟
    • 一、Zabbix监控mysql主从
      • 1、部署mysql主从,使用mariadb进行操作
      • 2、将server、agent1、master、slave主机的/etc/hosts文件全部设置为
      • 3、两台主机都安装mariadb mariadb-server
      • 4、两台主机都初始化mysql数据库
      • 5、修改数据库配置文件,然后两台主机都重启mariadb服务
      • 6、进入数据库配置主从
      • 7、在slave主机中安装zabbix-agent软件包,将slave添加到zabbix web监控平台中,将server主机的zabbix.repo复制过来,接着安装zabbix-agent
      • 8、修改 /etc/zabbix/zabbix_agentd.conf,重启服务
      • 9、进入zabbix web监控平台,添加主机
      • 10、在slave主机上配置脚本
      • 11、编写一个自配置文件,里面指定上面编写的脚本的路径,然后重启服务
      • 12、去zabbix server验证状态,使用zabbix_get命令验证,需要先下载zabbix-get软件包
      • 13、在zabbix web平台配置
      • 14、测试验证,将mysql主从关闭,查看zabbix告警信息,验证邮箱是否能接收到邮件
    • 二、zabbix监控主从延迟
      • 1、配置库脚本
      • 2、配置agentd文件,并重启服务
      • 3、zabbix server主机进行脚本测试
      • 4、在zabbix web平台操作
      • 5、创建触发器

一、Zabbix监控mysql主从


1、部署mysql主从,使用mariadb进行操作

[root@master ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.89.151 server.example.com server
192.168.89.150 agent1.example.com agent1
192.168.89.10 master.example.com  master
192.168.89.20 slave.example.com slave
[root@master ~]#
[root@slave ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.89.151 server.example.com server
192.168.89.150 agent1.example.com agent1
192.168.89.10 master.example.com master
192.168.89.20 slave.example.com slave
[root@slave ~]#

2、将server、agent1、master、slave主机的/etc/hosts文件全部设置为

[root@server ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.89.151  server.example.com server
192.168.89.150  agent1.example.com agent1
192.168.89.10  master.example.com master
192.168.89.20  slave.example.com  slave
[root@server ~]#
[root@agent1 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.89.151  server.example.com server
192.168.89.150  agent1.example.com agent1
192.168.89.10  master.example.com master
192.168.89.20  slave.example.com  slave
[root@agent1 ~]#

3、两台主机都安装mariadb mariadb-server

[root@master ~]# yum -y install mariadb mariadb-server
[root@master ~]# systemctl start mariadb
[root@master ~]# systemctl enable 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.
[root@master ~]#
[root@slave ~]#  yum -y install mariadb mariadb-server
[root@slave ~]# systemctl start mariadb
[root@slave ~]# systemctl enable 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.
[root@slave ~]#

4、两台主机都初始化mysql数据库

[root@master ~]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.Enter current password for root (enter for none):
OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..... Success!By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.Remove anonymous users? [Y/n]... Success!Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n... skipping.By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.Remove test database and access to it? [Y/n] y- Dropping test database...... Success!- Removing privileges on test database...... Success!Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.Reload privilege tables now? [Y/n] y... Success!Cleaning up...All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.Thanks for using MariaDB!
[root@master ~]#
[root@slave ~]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.Enter current password for root (enter for none):
OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..... Success!By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.Remove anonymous users? [Y/n] y... Success!Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n... skipping.By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.Remove test database and access to it? [Y/n] y- Dropping test database...... Success!- Removing privileges on test database...... Success!Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.Reload privilege tables now? [Y/n] y... Success!Cleaning up...All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.Thanks for using MariaDB!
[root@slave ~]#

5、修改数据库配置文件,然后两台主机都重启mariadb服务

master配置:

[root@master ~]# vim /etc/my.cnf.d/mariadb-server.cnf16 [mysqld]17 datadir=/var/lib/mysql18 socket=/var/lib/mysql/mysql.sock19 log-error=/var/log/mariadb/mariadb.log20 pid-file=/run/mariadb/mariadb.pid21 log_bin=mysql-bin22 server_id=10
[root@master ~]# systemctl restart mariadb

slave配置:

[root@slave ~]# vi /etc/my.cnf.d/mariadb-server.cnf16 [mysqld]17 datadir=/var/lib/mysql18 socket=/var/lib/mysql/mysql.sock19 log-error=/var/log/mariadb/mariadb.log20 pid-file=/run/mariadb/mariadb.pid21 log_bin=mysql-bin22 server_id=20
[root@slave ~]# systemctl restart mariadb

6、进入数据库配置主从

[root@master ~]# mysql -uroot -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB-log MariaDB ServerCopyright (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)]> grant all privileges  on *.* to root@'%' identified by "redhat";
Query OK, 0 rows affected (0.000 sec)MariaDB [(none)]> grant replication slave on *.* to 'user'@'slave' identified by 'redhat';
Query OK, 0 rows affected (0.000 sec)MariaDB [(none)]>
[root@slave ~]# mysql -uroot -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB-log MariaDB ServerCopyright (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)]> grant all privileges  on *.* to root@'%' identified by "redhat";
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> change master to master_host='master',master_user='user',master_password='redhat';
Query OK, 0 rows affected (0.002 sec)MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Connecting to masterMaster_Host: masterMaster_User: userMaster_Port: 3306Connect_Retry: 60Master_Log_File:Read_Master_Log_Pos: 4Relay_Log_File: mariadb-relay-bin.000001Relay_Log_Pos: 4Relay_Master_Log_File:Slave_IO_Running: ConnectingSlave_SQL_Running: Yes         //此处两行分别为connecting和yes

7、在slave主机中安装zabbix-agent软件包,将slave添加到zabbix web监控平台中,将server主机的zabbix.repo复制过来,接着安装zabbix-agent

[root@slave ~]# scp root@192.168.89.151:/etc/yum.repos.d/zabbix.repo /etc/yum.repos.d/
root@192.168.89.151's password:
zabbix.repo                                                                                                                             100%  385   461.2KB/s   00:00
[root@slave ~]# cat /etc/yum.repos.d/zabbix.repo
[aliyun]
name=aliyun
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/8/x86_64/
enable=1
gpgcheck=0
[qinghua]
name=Zabbix Official Repository - $basearch
#baseurl=http://repo.zabbix.com/zabbix/3.4/rhel/7/$basearch/
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/8/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[root@slave ~]# dnf -y install zabbix-agent

8、修改 /etc/zabbix/zabbix_agentd.conf,重启服务

[root@slave ~]# vim /etc/zabbix/zabbix_agentd.conf
98 Server=192.168.89.151
139 ServerActive=192.168.89.151
150 Hostname=slave
[root@slave ~]# systemctl restart zabbix-agent.service
[root@slave ~]# systemctl enable zabbix-agent.service
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-agent.service → /usr/lib/systemd/system/zabbix-agent.service.
[root@slave ~]#

9、进入zabbix web监控平台,添加主机

[root@slave ~]# systemctl stop firewalld
[root@slave ~]# vim /etc/selinux/config
SELINUX=disabled
[root@slave ~]# setenforce 0


10、在slave主机上配置脚本

[root@slave ~]# cd /
[root@slave /]# vim mysql_slave_status.sh
#!/bin/bash
USER="root"
PASSWD="redhat"
NAME=$1function IO {Slave_IO_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`if [ $Slave_IO_Running == "Connecting" ];thenecho 0elseecho 1fi
}function SQL {Slave_SQL_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
if [ $Slave_SQL_Running == "Yes" ];then
echo 0elseecho 1fi}case $NAME inio)IO;;sql)SQL;;*)echo -e "Usage: $0 [io | sql]"
esac
[root@slave /]# chown -R zabbix.zabbix /mysql_slave_status.sh
[root@slave /]# chmod +x mysql_slave_status.sh

11、编写一个自配置文件,里面指定上面编写的脚本的路径,然后重启服务

[root@slave /]# cd /etc/zabbix/zabbix_agentd.d/
[root@slave zabbix_agentd.d]# vim mysql_slave.conf
UserParameter=mysql.slave[*],/mysql_slave_status.sh $1
[root@slave zabbix_agentd.d]# chown -R zabbix.zabbix /etc/zabbix/zabbix_agentd.d/mysql_slave.conf
[root@slave zabbix_agentd.d]# systemctl restart zabbix-agent.service

12、去zabbix server验证状态,使用zabbix_get命令验证,需要先下载zabbix-get软件包

[root@server]# dnf -y install zabbix-get#验证的结果如果是0,为正常,如果为1,则异常
[root@server ~]# zabbix_get -s 192.168.89.20 -k mysql.slave[sql]
0
[root@server ~]# zabbix_get -s 192.168.89.20 -k mysql.slave[io]
0

13、在zabbix web平台配置

新建监控项


新建触发器


创建图形

14、测试验证,将mysql主从关闭,查看zabbix告警信息,验证邮箱是否能接收到邮件

二、zabbix监控主从延迟


1、配置库脚本

[root@slave ~]# cd /etc/zabbix/
[root@slave zabbix]# mkdir script
[root@slave zabbix]# cd script/
[root@slave script]# vim mysql_delay.sh
#!/bin/bash
delay=$(mysql -uroot -predhat -e 'show slave status\G' 2> /dev/null | grep 'Seconds_Behind_Master' | awk '{print $2}')
if [ $delay == "NULL" ];then
echo 0
elif [ $delay -ge 0 ] && [ $delay -le 200 ];then
echo 0
else
echo $delay
fi
#Behind:落后主库多少秒,存在秒数则出现主库复制之间的延迟
#只要当延迟数据为NULL,以及0-200是正常的,否则其他数字输入1表示错误
[root@slave script]# chown -R zabbix.zabbix mysql_delay.sh
[root@slave script]# chmod +x mysql_delay.sh
[root@slave script]#

2、配置agentd文件,并重启服务

[root@slave ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@slave zabbix_agentd.d]# vim mysql_slave.conf
UserParameter=mysql.slave[*],/mysql_slave_status.sh $1
UserParameter=check_mysql_delay,/bin/bash /etc/zabbix/script/mysql_delay.sh
[root@slave zabbix_agentd.d]#
[root@slave zabbix_agentd.d]# systemctl restart zabbix-agent.service

测试mysql_delay.sh脚本

[root@slave zabbix_agentd.d]# cd /etc/zabbix/script/
[root@slave script]# ./mysql_delay.sh
0
[root@slave script]#

3、zabbix server主机进行脚本测试

[root@server ~]# zabbix_get -s 192.168.89.20 -k check_mysql_delay
0
[root@server ~]#

4、在zabbix web平台操作

添加监控项

5、创建触发器

[root@slave script]# mysql -uroot -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 99
Server version: 10.3.28-MariaDB-log MariaDB ServerCopyright (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)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.001 sec)MariaDB [(none)]>

zabbix监控mysql主从与主从延迟相关推荐

  1. Zabbix 监控Mysql数据库及主从数据库

    Zabbix监控mysql主从数据库在脚步出现用户名和密码是会出现如下报错"Warning: Using a password on the command line interface c ...

  2. 监控MySQL数据库的主从状态的shell脚本

    本脚本来自有学习阿铭的博文学习: 在大数的互联网公司,其数据库都使用开源的MySQL,其架构也是主从为主,所以监控数据库主从同步情况尤为重要. 提示:本文中的S全部都$符,不要问为什么,马云爸爸的社区 ...

  3. Zabbix监控Mysql数据库性能

    来源:http://os.51cto.com/art/201404/435139.htm 本文通过Zabbix Graphs实时查看的SQL语句操作情况和mysql发送接收的字节数,Zabbix还可以 ...

  4. mysql 表空间监控shell_一种通过zabbix监控mysql表空间的方法

    一种通过zabbix监控mysql表空间的方法[ 技术领域: ][0001]本发明涉及计算机自动化运维与监控领域,具体地说是一种通过ZABBIX监控MYSQL表空间的方法.[ 背景技术: ][0002 ...

  5. zabbix监控mysql最简单的方法

    该实验基于我的上一篇文章监控第一台主机的基础上 首先,因为水平有限,我选择直接关闭了防火墙和SELinux. 环境: 两台centos7,服务器端IP是192.168.200.128(以下简称主机), ...

  6. zabbix监控mysql死锁

    percona MySQL Server Template算是比较常用的zabbix监控mysql的模板了,监控项也比较齐全,但是没有监控mysql死锁的监控项,如果有需求,就需要另外创建模板或者监控 ...

  7. Zabbix 监控 MySQL

    配置Zabbix 监控 MySQL 客户端配置 服务器: ubuntu14.04 之前装过 zabbix-agent 但是看了一下版本是2.2的,已经很落后了,于是升级了一下.(配置文件不用备份,升级 ...

  8. centos7 mysql读写监控,《Centos7——zabbix监控mysql的常规操作》

    <Centos7--zabbix监控mysql的常规操作> <Centos7--zabbix监控mysql的常规操作> http://blog.csdn.net/weixin_ ...

  9. 《Centos7——zabbix监控mysql的常规操作》

    <Centos7--zabbix监控mysql的常规操作> 逻辑思维菜鸟 2020-09-22 21:21:01 40 收藏 1 分类专栏: zabbix 数据库 文章标签: mysql ...

最新文章

  1. Zabbix2.4安装和问题总结(一)
  2. 注意:C语言结构体里不能赋初始值!
  3. 痛惜,今年已有多名杰青英年早逝!
  4. CentOS7开发环境搭建(2)
  5. Mac OSX 命令行知识
  6. 怎么看b树是几阶_数据库原理基础:设计B树与B+树的目的以及二者的优劣
  7. excel内容少却文件很大_009- EXCEL文件的表格内的数据内容明明不多,但是文件却变得很大...
  8. OpenCV精进之路(十四):图像矫正技术深入探讨
  9. 表单数据序列化,后台如何接收(java)
  10. redis练习-模拟手机验证码的发送
  11. scrapy框架Selector提取数据
  12. paip.最土团购在SHOPEX下安装总结
  13. ffmpeg详细安装教程,亲测有效!
  14. 五菱“神车”再添一员,小型电动车迎来“均值回归”?
  15. 华为鸿蒙系统手机上市,6月2日华为要有大动作,不仅有鸿蒙系统,P50发布时间也将公布...
  16. 前端和Java后端分别需要学哪些技术?
  17. 转行产品经理怎样才能少走弯路?新手必看!
  18. Echarts基本使用(vue实现3D地图)
  19. window如何打开 .h5 格式的文件?
  20. stc89c52c语言开发,STC89C52单片机开发板入门教程——简介(致雅科技)

热门文章

  1. android发送短信的两种方式,发送长短信的两种方式,群发短信
  2. c语言转义字符c,C语言转义字符
  3. 记录——kubeadm集群node节点加入
  4. aar打包依赖 android_打包依赖.aar文件以及坑总结
  5. 洗牌-牛客 第一行一个数T(T ≤ 100),表示数据组数。对于每组数据,第一行两个数n,k(1 ≤ n,k ≤ 100),接下来一行有2n个数a1,a2,...,a2n(1 ≤ ai ≤ 1000
  6. ❤️Hello Programmer❤️
  7. lisp 套料_标志排字及过桥的AutoCAD自动化插件设计
  8. 联想笔记本快捷键控制千千静听 2009年10月2日19:09:44
  9. “铜三铁四“来一套程序员内卷超车赛道-音视频开发
  10. 微信小程序 - - 授权登录退出和缓存