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

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

文章目录

zabbix自定义监控mysql状态

配置主从

bash 复制代码
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)

配置自定义监控

bash 复制代码
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延迟

配置自定义监控

bash 复制代码
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

添加监控项




添加触发器





测试



相关推荐
qq_29750461几秒前
【解决】Linux更新系统内核后Nvidia-smi has failed...
linux·运维·服务器
weixin_4373982114 分钟前
Linux扩展——shell编程
linux·运维·服务器·bash
小燚~16 分钟前
ubuntu开机进入initramfs状态
linux·运维·ubuntu
小林熬夜学编程23 分钟前
【Linux网络编程】第十四弹---构建功能丰富的HTTP服务器:从状态码处理到服务函数扩展
linux·运维·服务器·c语言·网络·c++·http
亦世凡华、24 分钟前
MySQL--》如何在MySQL中打造高效优化索引
数据库·经验分享·mysql·索引·性能分析
炫彩@之星27 分钟前
Windows和Linux安全配置和加固
linux·windows·安全·系统安全配置和加固
hhhhhhh_hhhhhh_37 分钟前
ubuntu18.04连接不上网络问题
linux·运维·ubuntu
冷心笑看丽美人44 分钟前
探秘 DNS 服务器:揭开域名解析的神秘面纱
linux·运维·服务器·dns
冬天vs不冷2 小时前
Linux用户与权限管理详解
linux·运维·chrome
凯子坚持 c3 小时前
深入Linux权限体系:守护系统安全的第一道防线
linux·运维·系统安全