mysql主从配置和从库zabbix监控

2024-01-29 01:18

本文主要是介绍mysql主从配置和从库zabbix监控,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Mysql的主从架构模式,是很多企业广泛使用,并且大家所广为熟知的一种架构模式

mysql主从复制主要用途

  • 实时灾备,用于故障切换
  • 读写分离,提供查询服务
  • 备份,避免影响业务

mysql主从复制存在的问题

  • 主库宕机后, 数据可能丢失
  • 主库写压力大, 复制可能会延时
主从复制原理

在这里插入图片描述
主从复制步骤:

  • 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程

  • 从库生成两个线程,一个I/O线程,一个SQL线程

    • I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
    • SQL线程会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
主从复制配置

主从复制配置步骤:

确保从数据库与主数据库里的数据一样
在主数据库里创建一个同步账号授权给从数据库使用
配置主数据库(修改配置文件)
配置从数据库(修改配置文件)

需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作

环境说明:

数据库角色IP应用与系统版本有无数据
主数据库192.168.230.16centos7/redhat7
mysql-5.7
有数据
从数据库192.168.230.15centos7/redhat7
mysql-5.7
无数据

这里mysql的安装都是源码编译的,详情见前面几篇博客

主库

[root@master ~]# mysql -uroot -p
mysql> create database cs;
mysql> use cs;
mysql> create table aa (id int,name varchar(30));
mysql> insert into aa values (1,'apple'),(2,'banana');
mysql> select * from aa;
+------+--------+
| id   | name   |
+------+--------+
|    1 | apple  |
|    2 | banana |
+------+--------+
做全备
[root@master ~]# mysqldump -uroot -p'cs123.com' --all-databases > $(date +%F-%H-%M)-all.sql
拷贝到从主机
[root@master ~]# scp 2019-02-27-14-36-all.sql 192.168.230.15:/root/

从库

[root@slave ~]# mysql -uroot -p'cs123.com' < 2019-02-27-14-36-all.sql 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cs                 |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
mysql> use cs;
mysql> show tables;
+--------------+
| Tables_in_cs |
+--------------+
| aa           |
+--------------+
mysql> select * from aa;
+------+--------+
| id   | name   |
+------+--------+
|    1 | apple  |
|    2 | banana |
+------+--------+

主库

mysql> grant replication slave on *.* to 'rep'@'192.168.230.15' identified by 'rep123.com';
mysql> flush privileges;

从库

[root@slave ~]# mysql -urep -p'rep123.com' -h 192.168.230.16
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+

主库

[root@master ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=1
[root@master ~]# service mysqld restartmysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |   804259 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

从库

[root@slave ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=2
[root@slave ~]# service mysqld restart
mysql> change master to master_host='192.168.230.16',master_user='rep',master_password='rep123.com',master_log_file='mysql-bin.000001',master_log_pos=804259;
mysql> start slave;
mysql> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.230.16Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 841636Relay_Log_File: slave-relay-bin.000002Relay_Log_Pos: 37697Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 841636Relay_Log_Space: 37904Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: b27045f6-36bb-11e9-b404-000c29c87115Master_Info_File: /opt/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 

看到这个就说明成功了

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

测试

主库插入数据
mysql> use cs;
mysql> show tables;
+--------------+
| Tables_in_cs |
+--------------+
| aa           |
+--------------+
mysql> insert into aa values (3,'cici'),(4,'dog');
mysql> select * from aa;
+------+--------+
| id   | name   |
+------+--------+
|    1 | apple  |
|    2 | banana |
|    3 | cici   |
|    4 | dog    |
+------+--------+从库查看
mysql> use cs;
mysql> select * from aa;
+------+--------+
| id   | name   |
+------+--------+
|    1 | apple  |
|    2 | banana |
|    3 | cici   |
|    4 | dog    |
+------+--------+
从库zabbix监控

先在zabbix客户端添加监控脚本和修改配置

[root@slave scripts]# vim Slave_IO_Running.sh
#!/bin/bash
Slave_IO_Running=$(mysql -uroot -pcs123.com -e "show slave status \G" 2> /dev/null |grep "Slave_IO_Running:" |awk -F: '{pri    nt $2}')
if [ $Slave_IO_Running == Yes ];thenecho 0
elseecho 1
fi[root@slave scripts]# vim Slave_SQL_Running.sh
#!/bin/bash
Slave_SQL_Running=$(mysql -uroot -pcs123.com -e "show slave status \G" 2> /dev/null |grep "Slave_SQL_Running:" |awk -F: '{p    rint $2}')
if [ $Slave_SQL_Running == Yes ];thenecho 0
elseecho 1
fi[root@slave scripts]# vim Read_Exec.sh
#!/bin/bash
Read_Master_Log_Pos=$(mysql -uroot -pcs123.com -e "show slave status \G" 2> /dev/null | grep "Read_Master_Log_Pos:" |awk -F: '{print $2}')
Exec_Master_Log_Pos=$(mysql -uroot -pcs123.com -e "show slave status \G" 2> /dev/null | grep "Exec_Master_Log_Pos:" |awk -F: '{print $2}')
a=$[$Read_Master_Log_Pos - $Exec_Master_Log_Pos]
if [ $a -eq 0 ];thenecho "0"
elseecho "$a"
fi[root@slave ~]# chmod -R 755 /scripts/
[root@slave ~]# chown -R zabbix.zabbix /scripts/[root@slave ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_io,/scripts/Slave_IO_Running.sh
UserParameter=check_mysql_sql,/scripts/Slave_SQL_Running.sh
UserParameter=check_mysql_wait,/scripts/Read_Exec.sh[root@slave scripts]# pkill zabbix
[root@slave scripts]# zabbix_agentd 

然后就是配置zabbix的web界面,添加监控项和触发器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

添加完了触发器后再添加告警媒介,因为之前已经添加过了,这里就不添加直接用了,怎么添加见上个博客

然后去客户端停掉slave角色,等待报警结果

mysql> stop slave;

过一会就告警了,邮件也发过来了
在这里插入图片描述

另外两个照着上面做就行了~~

这篇关于mysql主从配置和从库zabbix监控的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/655353

相关文章

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

MySQL 衍生表(Derived Tables)的使用

《MySQL衍生表(DerivedTables)的使用》本文主要介绍了MySQL衍生表(DerivedTables)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学... 目录一、衍生表简介1.1 衍生表基本用法1.2 自定义列名1.3 衍生表的局限在SQL的查询语句select

MySQL 横向衍生表(Lateral Derived Tables)的实现

《MySQL横向衍生表(LateralDerivedTables)的实现》横向衍生表适用于在需要通过子查询获取中间结果集的场景,相对于普通衍生表,横向衍生表可以引用在其之前出现过的表名,本文就来... 目录一、横向衍生表用法示例1.1 用法示例1.2 使用建议前面我们介绍过mysql中的衍生表(From子句

六个案例搞懂mysql间隙锁

《六个案例搞懂mysql间隙锁》MySQL中的间隙是指索引中两个索引键之间的空间,间隙锁用于防止范围查询期间的幻读,本文主要介绍了六个案例搞懂mysql间隙锁,具有一定的参考价值,感兴趣的可以了解一下... 目录概念解释间隙锁详解间隙锁触发条件间隙锁加锁规则案例演示案例一:唯一索引等值锁定存在的数据案例二: