ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

自定义监控MySQL主从状态

2022-09-08 01:30:13  阅读:222  来源: 互联网

标签:none mariadb 自定义 MySQL nie mysql MariaDB root 主从


自定义监控MySQL主从状态

zabbix自定义监控Mysql主从状态和延迟

目录

监控Mysql主从状态

主从同步

监控mysql主从状态监控的是从库,所以只用在从库是安装并启动zabbix_aength

以下启动安装可参考

zabbix介绍与部署

这里实验使用的yum安装的mariadb,实战中请2进制安装mysql

mysql主从同步可参考

mysql主从介绍与配置

主库配置

#给主库改一个名字
[root@localhost ~]# hostnamectl  set-hostname nie
[root@localhost ~]# bash

#这里实验使用yum安装mariadb,并设置开机自启立即启动
[root@nie ~]# dnf -y install mariadb*
[root@nie ~]# systemctl enable --now 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.

#更改mariadb数据库密码
[root@nie ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (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)]> set password = password('123456');
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> quit
Bye
[root@nie ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (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)]> 

#在主库上创建同步账号
MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.118.136' identified by '123456';
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> quit
Bye

#编写主库配置文件
[root@nie ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
...
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid

log-bin = mysql_bin   //启用binlog日志
server-id=10          //数据库服务器唯一标识符,此处需要小于从库
...
[root@nie ~]# systemctl restart mariadb

#查看主库状态
[root@nie ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (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)]> show nie status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000001 |      328 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

从库配置

#从库中安装并启动zabbix_agent
[root@136 ~]# 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       128            0.0.0.0:10050          0.0.0.0:*   

#这里实验使用yum安装mariadb,并设置开机自启立即启动
[root@136 ~]# dnf -y install mariadb*
[root@136 ~]# systemctl enable --now 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.

#更改mariadb数据库密码
[root@136 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (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)]> set password = password('123456');
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> quit
Bye
[root@136 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (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)]> 

#编写从库配置文件
[root@136 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
...
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid

relay-log = myrelay
server-id = 20
...
[root@136 ~]# systemctl restart mariadb

#配置主从
[root@136 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (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)]> change nie to
    -> nie_host='192.168.48.141',
    -> nie_user='repl',
    -> nie_password='123456',
    -> nie_log_file='mysql_bin.000001',
    -> nie_log_pos=328;
Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for nie to send event
                   nie_Host: 192.168.118.130
                   nie_User: repl
                   nie_Port: 3306
                 Connect_Retry: 60
               nie_Log_File: mysql_bin.000001
           Read_nie_Log_Pos: 328
                Relay_Log_File: myrelay.000002
                 Relay_Log_Pos: 555
         Relay_nie_Log_File: mysql_bin.000001
              Slave_IO_Running: Yes         //此处必须为yes  
             Slave_SQL_Running: Yes         //此处必须为yes
               Replicate_Do_DB: 
...

测试验证

在主数据库中创建一个runtime数据库

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)

MariaDB [(none)]> create database runtime;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| runtime            |
+--------------------+
4 rows in set (0.000 sec)

在从库中查看,这里也多了一个runtime数据库,验证成功。

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| runtime            |
+--------------------+
4 rows in set (0.000 sec)

配置监控脚本

从库配置文件与编写脚本

#编写脚本
[root@136 ~]# vim /etc/scripts/mysql_status.sh
#!/bin/bash
yes=`mysql -uroot -p123456 -e " show slave status\G" 2> /dev/null |grep "Running:"|grep -c "Yes"`
if [ $yes == 2  ]; then
        echo "0"
else    
        echo "1"
fi 
[root@136 ~]# chmod +x /etc/scripts/mysql_status.sh

#在从库修改配置文件
[root@136 ~]# vim /usr/local/etc/zabbix_agentd.conf
...
UserParameter=check_mysql_status,/bin/bash /etc/scripts/mysql_status.sh

#重启服务生效配置
[root@136 ~]# pkill zabbix_agentd 
[root@136 ~]# zabbix_agentd 

服务端检查key是否可以使用

[root@zabbix ~]# zabbix_get -s 192.168.118.136 -k check_mysql_status
0

添加监控项

1

2

3

5

6

7

手动触发报警,查看报警信息

自定义监控MySQL延迟

配置监控脚本

监控延迟,我们需要关注 Seconds_Behind_Master这一项

//在被监控端,也就是从库,首先修改配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysqlyc,/bin/bash /scripts/mysqlyc.sh
 
//重启服务生效配置
[root@localhost ~]# pkill zabbix_agentd 
[root@localhost ~]# zabbix_agentd 
 
//再编写脚本
[root@localhost ~]# vim /scripts/mysqlyc.sh
 
#!/bin/bash
yc=`mysql -uroot -p123456 -e "show slave status\G" 2> /dev/null |awk '/Seconds_Behind_Master/ {print $2}' `
echo $yc
 
[root@localhost ~]# chmod +x /scripts/mysqlyc.sh 
 
//去服务端检查key是否可用
[root@zabbix ~]# zabbix_get -s 192.168.48.151 -k check_mysqlyc
0

用户管理用户组

1

2

3

用户角色

4

5

用户

8

11

13

14

模板管理

16

18

19

20

21

23

标签:none,mariadb,自定义,MySQL,nie,mysql,MariaDB,root,主从
来源: https://www.cnblogs.com/nie123/p/16667889.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有