首页 > 数据库 >自定义监控MySQL主从状态

自定义监控MySQL主从状态

时间:2022-09-08 01:33:25浏览次数:108  
标签: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,主从
From: https://www.cnblogs.com/nie123/p/16667889.html

相关文章

  • zabbix自定义监控mysql主从状态和延迟
    zabbix自定义监控mysql主从状态和延迟目录zabbix自定义监控mysql主从状态和延迟zabbix自定义监控mysql主从状态zabbix自定义监控mysql主从延迟zabbix自定义监控mysql主......
  • 【PHP】5版本 过程式操作MySQL
     建立连接和释放连接:#连接参数$sever='localhost:3309';$username='root';$password='root';#调用连接方法,如果失败结束并打印错误信息$connection=my......
  • MySQL的基础
    MySQL的基础函数字符串函数数值函数日期函数流程函数字符串函数函数功能CONCAT(S1,S2...Sn)将字符串拼接起来LOWER(str)将字符串str转换成小写U......
  • Ubuntu实现电商网站+Mysql主从复制+NFS
    Ubuntu实现电商网站+Mysql主从复制+NFS1、环境准备提前准备:Mysql8.0.30安装包、Mysql安装脚本、shopxo2.3.0安装包、DNS脚本服务器IP地址作用系统版本Mysql......
  • 【django学习-11】模板3:自定义标签与过滤器
    前言:Django虽然内置了二十多种标签和六十多种过滤器,但是为了给Web开发者提供更好使用体验,Django也提供了自定义标签与过滤器的功能。当内置标签与过滤器满足不了实际......
  • zabbix自定义监控主从状态延迟与创建用户和组及权限管理
    1:zabbix监控主从部署mysql主从环境用到两台centos8的操作系统一台为master一台为slave[root@localhost~]#hostnamectlset-hostnamemaster[root@localhost~]#ba......
  • MySQL第五天
    2022-09-071、Mysql中的清屏:systemclear一般的清屏命令:clear聚合函数2、查询某个表中某个字段的值的个数(使用count)以“students”表(字段有id,name,age,gender,heig......
  • MySQL备份与日志
    MySQL日志管理事务日志innodb_log##事务日志执行逻辑:1.想要修改表,update操作,磁盘内先加载到内存中要执行2.在内存中执行完update3.结果先写入到事务日志,这一条操作确......
  • mysql数据迁移-换主机,换版本升级
    换主机:--数据量小在线MDP(mysqldump),XBK备份出来,scp到目标主机追加所有备份后的日志申请停机5分钟剩余部分的binlog继续恢复(搭建主从的方式来代替)校验数据进行业务......
  • [安装配置] Linux docker mysql 安装
    一.docker安装MySQLLinux命令:dockerpullmysql:5.7dockerimages查看所有镜像二.创建实例并启动suroot切换到Linuxroot用户dockerrun-p3306:3306--name......