zabbix自定义监控mysql状态和延迟

zabbix自定义监控mysql状态和延迟

zabbix自定义监控mysql状态

配置主从

1.安装mysql-server
//主
[root@master ~]# yum -y install mysql-server
Complete!

//从
[root@slave ~]# yum -y install mysql-server
Complete!

2.开启服务
//主
[root@master ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@master ~]# ss -antl
State              Recv-Q             Send-Q                          Local Address:Port                            Peer Address:Port             Process             
LISTEN             0                  128                                   0.0.0.0:22                                   0.0.0.0:*                                    
LISTEN             0                  4096                                  0.0.0.0:10050                                0.0.0.0:*                                    
LISTEN             0                  70                                          *:33060                                      *:*                                    
LISTEN             0                  151                                         *:3306                                       *:*                                    
LISTEN             0                  128                                      [::]:22                                      [::]:*                                    

//从
[root@slave ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@slave ~]# ss -antl
State              Recv-Q             Send-Q                          Local Address:Port                            Peer Address:Port             Process             
LISTEN             0                  128                                   0.0.0.0:22                                   0.0.0.0:*                                    
LISTEN             0                  70                                          *:33060                                      *:*                                    
LISTEN             0                  128                                      [::]:22                                      [::]:*                                    
LISTEN             0                  151                                         *:3306                                       *:*                                    

3.修改数据库密码
//主
[root@master ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user root@localhost identified with mysql_native_password by 'Passw0rd@_~'
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@master ~]# mysql -uroot -pPassw0rd@_~
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 9
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye

//从
[root@slave ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user root@localhost identified with mysql_native_password by 'Passw0rd@_~';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@slave ~]# mysql -uroot -pPassw0rd@_~
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 9
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye

4.创建用户授权并修改配置文件
//主
[root@master ~]# mysql -uroot -pPassw0rd@_~
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 10
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create user repl@192.168.116.139 identified with mysql_native_password by 'Passw0rd@_~';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to repl@192.168.116.139;
Query OK, 0 rows affected (0.00 sec)

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

mysql> quit
Bye

//从
[root@slave ~]# mysql -urepl -p'Passw0rd@_~' -h192.168.116.143
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 11
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

//主
[root@master ~]# vim /etc/my.cnf.d    //进去之后看见一个mysql-server.cnf,把光标移到这回车一下,直接在后面加内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
log-bin = mysql_bin
server-id = 10
[root@master ~]# systemctl restart mysqld
[root@master ~]# ss -antl
State              Recv-Q             Send-Q                          Local Address:Port                            Peer Address:Port             Process             
LISTEN             0                  128                                   0.0.0.0:22                                   0.0.0.0:*                                    
LISTEN             0                  4096                                  0.0.0.0:10050                                0.0.0.0:*                                    
LISTEN             0                  70                                          *:33060                                      *:*                                    
LISTEN             0                  151                                         *:3306                                       *:*                                    
LISTEN             0                  128                                      [::]:22                                      [::]:*                                    

//从
[root@slave ~]# vim /etc/my.cnf.d
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
relay-log = mysql_relay_bin
server-id = 20
[root@slave ~]# systemctl restart mysqld
[root@slave ~]# ss -antl
State              Recv-Q             Send-Q                          Local Address:Port                            Peer Address:Port             Process             
LISTEN             0                  128                                   0.0.0.0:22                                   0.0.0.0:*                                    
LISTEN             0                  70                                          *:33060                                      *:*                                    
LISTEN             0                  128                                      [::]:22                                      [::]:*                                    
LISTEN             0                  151                                         *:3306                                       *:*                                    

5.实现主从的同步
//主
[root@master ~]# mysql -uroot -pPassw0rd@_~
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 8
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 |      157 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

//从
[root@slave ~]# mysql -uroot -pPassw0rd@_~
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 8
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> change master to 
    -> master_host='192.168.116.143',
    -> master_user='repl',
    -> master_password='Passw0rd@_~',
    -> master_log_file='mysql_bin.000001',
    -> master_log_pos=157;
Query OK, 0 rows affected, 8 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.116.143
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 157
               Relay_Log_File: mysql_relay_bin.000002
                Relay_Log_Pos: 326
        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: 157
              Relay_Log_Space: 536
              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: 77f22ade-aeb5-11ee-b197-000c297b5e5c
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica 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: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

//主
[root@master ~]# mysql -uroot -pPassw0rd@_~
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 10
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database hl;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| hl                 |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

//从
[root@slave ~]# mysql -uroot -pPassw0rd@_~
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 15
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| hl                 |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

配置自定义监控

1.编写脚本
//从
[root@slave ~]# mysql -uroot -pPassw0rd@_~ -e 'show slave status\G' 2> /dev/null | grep Running: | awk '{print $2}'|grep -c 'Yes'
2
[root@slave ~]# mkdir /scripts
[root@slave ~]# touch /scripts/mysql_msstatus.sh
[root@slave ~]# cd /scripts
[root@slave scripts]# ls
mysql_msstatus.sh
[root@slave scripts]# chmod +x mysql_msstatus.sh 
[root@slave scripts]# ls
mysql_msstatus.sh
[root@slave scripts]# vim mysql_msstatus.sh 
[root@slave scripts]# cat mysql_msstatus.sh 
#!/bin/bash

count=$(mysql -uroot -pPassw0rd@_~ -e 'show slave status\G' 2> /dev/null | grep Running: | awk '{print $2}'|grep -c 'Yes')

echo $count
[root@slave scripts]# ./mysql_msstatus.sh 
2

2.安装zabbix_agent
//安装依赖包
[root@slave ~]# yum -y install gcc gcc-c++ pcre-devel
Complete!

//下载软件包
[root@slave ~]# ls
alter  anaconda-ks.cfg  quit  zabbix-6.4.10.tar.gz

//解压
[root@slave ~]# tar xf zabbix-6.4.10.tar.gz 
[root@slave ~]# ls
alter  anaconda-ks.cfg  quit  zabbix-6.4.10  zabbix-6.4.10.tar.gz

//编译
[root@slave ~]# cd zabbix-6.4.10
[root@slave zabbix-6.4.10]# ./configure --help| grep agent
  --enable-agent          Turn on build of Zabbix agent and client utilities
  --enable-agent2         Turn on build of Zabbix agent 2
[root@slave zabbix-6.4.10]# ./configure --enable-agent
***********************************************************
*            Now run 'make install'                       *
*                                                         *
*            Thank you for using Zabbix!                  *
*              <http://www.zabbix.com>                    *
***********************************************************
[root@slave zabbix-6.4.10]# make && make install
[root@slave zabbix-6.4.10]# echo $?
0

//设置开机自启
[root@slave ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/zabbix.service
[root@slave ~]# vim /usr/lib/systemd/system/zabbix.service 
[root@slave ~]# cat /usr/lib/systemd/system/zabbix.service 
[Unit]
Description=zabbix server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/sbin/zabbix_agentd
ExecStop=pkill zabbix
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@slave ~]# systemctl daemon-reload

//创建系统用户
[root@slave ~]# useradd -r -M -s /sbin/nologin zabbix

//启动服务
[root@slave ~]# systemctl start zabbix
[root@slave ~]# systemctl enable zabbix
[root@slave ~]# systemctl start zabbix
[root@slave ~]# systemctl enable zabbix
[root@slave ~]# ss -antl
State              Recv-Q             Send-Q                          Local Address:Port                            Peer Address:Port             Process             
LISTEN             0                  128                                   0.0.0.0:22                                   0.0.0.0:*                                    
LISTEN             0                  4096                                  0.0.0.0:10050                                0.0.0.0:*                                    
LISTEN             0                  70                                          *:33060                                      *:*                                    
LISTEN             0                  128                                      [::]:22                                      [::]:*                                    
LISTEN             0                  151                                         *:3306                                       *:*                                    

3.编辑zabbix_agentd.conf配置文件
[root@slave ~]# cd /usr/local/etc
[root@slave etc]# ls
zabbix_agentd.conf  zabbix_agentd.conf.d
[root@slave etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1          //取消注释并开启
UserParameter=check_mysqlms,/scripts/mysql_msstatus.sh    //到最后面加
[root@slave ~]# systemctl restart zabbix
[root@slave ~]# ss -antl
State              Recv-Q             Send-Q                          Local Address:Port                            Peer Address:Port             Process             
LISTEN             0                  128                                   0.0.0.0:22                                   0.0.0.0:*                                    
LISTEN             0                  4096                                  0.0.0.0:10050                                0.0.0.0:*                                    
LISTEN             0                  70                                          *:33060                                      *:*                                    
LISTEN             0                  128                                      [::]:22                                      [::]:*                                    
LISTEN             0                  151                                         *:3306                                       *:*                                    

4.到zabbix_server主机中获取数据
[root@zabbixserver ~]# zabbix_get -s 192.168.116.139 -k check_mysqlms
2

5.加密脚本文件让其他用户不可查看
[root@slave ~]# chown -R zabbix.zabbix /scripts
[root@slave ~]# ll /scripts
total 8
-rwxr-xr-x 1 zabbix zabbix 148 Jan  9 16:19 mysql_msrelay.sh
-rwxr-xr-x 1 zabbix zabbix 150 Jan  9 14:59 mysql_msstatus.sh
[root@slave ~]# chmod 700 /scripts
[root@slave ~]# ll /
drwx------    2 zabbix zabbix   55 Jan  9 16:19 scripts

//测试
[root@slave ~]# su - tom
[tom@slave ~]$ ll /
drwx------    2 zabbix zabbix   55 Jan  9 16:19 scripts
[tom@slave ~]$ cd /scripts
-bash: cd: /scripts: Permission denied
[tom@slave ~]$ cat /scripts/mysql_msstatus.sh
cat: /scripts/mysql_msstatus.sh: Permission denied

添加监控项

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

添加触发器

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

模拟测试异常

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

zabbix自定义监控mysql延迟

配置自定义监控

1.编写脚本
mysql> show slave status\G;
Seconds_Behind_Master: 0
[root@slave ~]# mysql -uroot -pPassw0rd@_~ -e 'show slave status\G' 2> /dev/null | grep Seconds_Behind_Master | awk '{print $2}'
0
[root@slave ~]# cd /scripts
[root@slave scripts]# ls
mysql_msstatus.sh
[root@slave scripts]# touch mysql_msrelay.sh
[root@slave scripts]# chmod +x mysql_msrelay.sh 
[root@slave scripts]# vim mysql_msrelay.sh 
[root@slave scripts]# cat mysql_msrelay.sh 
#!/bin/bash

relay=$(mysql -uroot -pPassw0rd@_~ -e 'show slave status\G' 2> /dev/null | grep Seconds_Behind_Master | awk '{print $2}')

echo $relay
[root@slave scripts]# ./mysql_msrelay.sh 
0

2.写入配置文件
[root@slave scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_msrelay,/scripts/mysql_msrelay.sh
[root@slave scripts]# systemctl restart zabbix
[root@slave scripts]# ss -antl
State              Recv-Q             Send-Q                          Local Address:Port                            Peer Address:Port             Process             
LISTEN             0                  128                                   0.0.0.0:22                                   0.0.0.0:*                                    
LISTEN             0                  4096                                  0.0.0.0:10050                                0.0.0.0:*                                    
LISTEN             0                  70                                          *:33060                                      *:*                                    
LISTEN             0                  128                                      [::]:22                                      [::]:*                                    
LISTEN             0                  151                                         *:3306                                       *:*                                    

3.在zabbixserver主机中获取数据
[root@zabbixserver ~]# zabbix_get -s 192.168.116.139 -k check_msrelay
0

添加监控项

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

添加触发器

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

测试

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

  • 8
    点赞
  • 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、付费专栏及课程。

余额充值