自定义监控(mysql主从和mysql延迟)

自定义监控(MysqL主从和MysqL延迟)

准备工作

#在server主机上搭建zabbix服务
#在localhost主机上搭建zabbix客户端
#在master主机和localhost主机上安装MysqL
 
    
//关闭主从端的防火墙
[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; v>
   Active: inactive (dead)
     Docs: man:firewalld(1)

[root@master ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; v>
   Active: inactive (dead)
     Docs: man:firewalld(1)

搭建MysqL主从

主库

//在主库上创建用户repl授权给从库使用
MysqL> grant replication slave on *.* to repl@'192.168.78.140' identified by 'repl123';
Query OK, 0 rows affected, 1 warning (0.01 sec)

MysqL> flush privileges;
Query OK, 0 rows affected (0.00 sec)

配置主库文件

[root@master ~]# vim /etc/my.cnf 
[root@master ~]# cat /etc/my.cnf 
[MysqLd]
basedir = /usr/local/MysqL
datadir = /opt/data
socket = /tmp/MysqL.sock
port = 3306
pid-file = /opt/data/MysqL.pid
user = MysqL
skip-name-resolve

server-id=10 
log-bin=MysqL_bin
[root@master ~]# systemctl restart MysqLd.service 
[root@master ~]# MysqL -uroot -pruntime123!
MysqL: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MysqL monitor.  Commands end with ; or \g.
Your MysqL connection id is 2
Server version: 5.7.37-log MysqL Community Server (GPL)

copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered Trademark of Oracle Corporation and/or its
affiliates. Other names may be Trademarks of their respective
owners.
MysqL> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | binlog_Do_DB | binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| MysqL_bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

配置从库

[root@localhost ~]# vim /etc/my.cnf 
[root@localhost ~]# cat /etc/my.cnf 
[MysqLd]
basedir = /usr/local/MysqL
datadir = /opt/data
socket = /tmp/MysqL.sock
port = 3306
pid-file = /opt/data/MysqL.pid
user = MysqL
skip-name-resolve

server-id=20
relay-log=MysqL-relay-bin
[root@localhost ~]# systemctl restart MysqLd.service 
//连接数据库主从配置
MysqL> change master to
    -> master_host='192.168.78.142',
    -> master_user='repl',
    -> master_password='repl123',
    -> master_log_file='MysqL_bin.000001',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

MysqL> start slave;  //启动
MysqL> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.78.142
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: MysqL_bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: MysqL-relay-bin.000003
                Relay_Log_Pos: 0
        Relay_Master_Log_File: MysqL_bin.000001
             Slave_IO_Running: Yes       //两个yse表示开启成功
            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: 154
              Relay_Log_Space: 693
              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
                  Master_UUID: 32748ca6-00f6-11ed-a5ea-000c29972037
             Master_Info_File: /opt/data/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: 
1 row in set (0.00 sec) 

测试

//主库添加123
[root@master ~]# MysqL -uroot -p'runtime123!' -e  'create database 123;'
MysqL: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# MysqL -uroot -p'runtime123!' -e  'show databases;'
MysqL: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| 123                |
| MysqL              |
| performance_schema |
| sys                |
| test               |
+--------------------+

//从库查看
[root@localhost ~]# MysqL -uroot -p'runtime123!' -e 'show databases;'
MysqL: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| 123                |
| MysqL              |
| performance_schema |
| sys                |
| test               |
+--------------------+

编写脚本

[root@localhost scropts]# cat MysqLms.sh 
#!/bin/bash
count=$( MysqL -uroot -pruntime123! -e "show slave status\G" 2>/dev/null | grep -v grep | grep -c 'Yes')
if [ $count -ne 2 ];then   
    echo '1'
else
    echo '0'
fi
    
[root@localhost scropts]# chmod +x MysqLms.sh   //赋予执行权限
[root@localhost scropts]# ll
总用量 12
-rwxr-xr-x. 1 root root 1554 7月   9 11:15 log.py
-rwxr-xr-x. 1 root root  176 7月  11 13:40 MysqLms.sh
[root@localhost scropts]# bash MysqLms.sh    //结果为0,脚本没有问题
0
    
配置文件zabbix_agentd.conf
[root@localhost ~]# vim zabbix_agentd.conf
UserParameter=check_MysqLms,/bin/bash /scropts/MysqLms.sh   //添加自定义监控
[root@localhost ~]# pkill zabbix_agentd  
[root@localhost ~]# zabbix_agentd   

配置zabbix网页

添加监控项(右上角)

添加触发器(右上角)


测试

//打开防火墙
[root@master ~]# systemctl start firewalld.service 
[root@master ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2022-07-11 06:10:40 EDT; 32s ago
     Docs: man:firewalld(1)
 Main PID: 258109 (firewalld)
    Tasks: 2 (limit: 11160)
   Memory: 28.8M
   CGroup: /system.slice/firewalld.service
           └─258109 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

查看zabbix

自定义监控MysqL主从延迟

编写脚本

[root@localhost scropts]# cat MysqL_delay.sh 
#!/bin/bash

delay=$( MysqL -uroot -pruntime123! -e "show slave status\G" 2>/dev/null | grep 'Seconds_Behind_Master'| awk '{print $2}')

echo $delay

//给执行权限
[root@localhost scropts]# chmod +x MysqL_delay.sh 
[root@localhost scropts]# ll
总用量 16
-rwxr-xr-x. 1 root root 1854 7月   9 16:15 log.py
-rwxr-xr-x. 1 root root  151 7月  11 18:30 MysqL_delay.sh
-rwxr-xr-x. 1 root root  178 7月  11 17:20 MysqLms.sh
  
//修改文件
[root@localhost etc]# cd /usr/local/etc/
[root@localhost etc]# cat zabbix_agentd.conf
 //添加
UserParameter=MysqL_delay,/bin/bash /scropts/MysqL_delay.sh
[root@localhost etc]# pkill zabbix_agentd  
[root@localhost etc]# zabbix_agentd 
    
//测试
[root@localhost scropts]# bash MysqL_delay.sh   
0
[root@zabbix.server ~]# zabbix_get -s 192.168.78.141 -k MysqL_delay    //服务端测试
0  

配置zabbix网页

添加监控项(右上角)


1

添加触发器(右上角)


1

回到首页查看告警

相关文章

优化MySQL数据库发布系统存储的方法有:1.mysql库主从读写分...
使用mysql的方法:在“我的电脑”→右键→“管理”→“服务”...
在mysql中查看root用户权限的方法:1.命令行启动mysql服务;...
MySQL主从复制是用来备份一个与主数据库一样环境的从数据库,...
运行mysql的方法1.启动mysql服务,在“我的电脑”→右键→“...
开启mysql的方法1.可以通过快捷键win+r,输入cmd,打开窗口,...