当前位置:网站首页>Redis 主从机制
Redis 主从机制
2022-07-24 06:02:00 【熙攘。】
一、集群结构
单节点Redis的并发能力是有上限的,要进一步提高Redis的并发能力,就需要搭建主从集群,实现读写分离。
主从结构如下:

共包含三个节点,一个主节点,两个从节点。在同一台虚拟机中开启3个redis实例,模拟主从集群:

二、搭建步骤
1、创建目录:
创建三个文件夹,名字分别叫7001、7002、7003:
# 进入/tmp目录
cd /tmp
# 创建目录
mkdir 7001 7002 70032、编写redis.conf配置文件:
将redis-6.2.6/redis.conf文件复制到三个目录中,持久化模式为默认的RDB模式。并修改每个文件夹内的配置文件,将端口分别修改为7001、7002、7003,将rdb文件保存位置都修改为自己所在目录:
# 开启RDB
# save ""
save 3600 1
save 300 100
save 60 10000
# 关闭AOF
appendonly nosed -i -e 's/6379/7001/g' -e 's/dir .\//dir \/tmp\/7001\//g' 7001/redis.conf
sed -i -e 's/6379/7002/g' -e 's/dir .\//dir \/tmp\/7002\//g' 7002/redis.conf
sed -i -e 's/6379/7003/g' -e 's/dir .\//dir \/tmp\/7003\//g' 7003/redis.conf3、在配置文件中修改声明IP:
虚拟机本身有多个IP,为了避免将来混乱,我们需要在redis.conf文件中指定每一个实例的绑定ip信息,格式如下:
# redis实例的声明 IP
replica-announce-ip 192.168.1.103# 逐一执行
sed -i '1a replica-announce-ip 192.168.150.101' 7001/redis.conf
sed -i '1a replica-announce-ip 192.168.150.101' 7002/redis.conf
sed -i '1a replica-announce-ip 192.168.150.101' 7003/redis.conf
# 或者一键修改
printf '%s\n' 7001 7002 7003 | xargs -I{} -t sed -i '1a replica-announce-ip 192.168.150.101' {}/redis.conf三、启动
为了方便查看日志,在mobaxterm中打开3个redis窗口,分别在tmp目录下启动命令:
# 第1个
redis-server 7001/redis.conf
# 第2个
redis-server 7002/redis.conf
# 第3个
redis-server 7003/redis.conf启动后:

四、开启主从关系
配置主从关系可以使用replicaof 或者slaveof(redis5.0以前)命令:
# slaveof <masterip> <masterport>
slaveof 192.168.1.103 7001临时:使用redis-cli客户端连接到redis服务,执行slaveof命令(重启后失效);
永久:将该命令添加在在redis.conf中。
为了便于演示我们使用临时方式。
通过redis-cli命令连接7002,执行下面命令:
# 在tmp目录下连接 7002
redis-cli -p 7002
# 执行slaveof
slaveof 192.168.1.103 7001通过redis-cli命令连接7003,执行下面命令:
# 在tmp目录下连接 7003
redis-cli -p 7003
# 执行slaveof
slaveof 192.168.1.103 7001连接 7001,查看集群状态 :
# 连接 7001
redis-cli -p 7001
# 查看状态
info replication结果:

五、测试

六、原理
1、全量同步

master如何判断slave是不是第一次来同步数据?这里会用到两个很重要的概念:
Replication Id:
简称replid,是数据集的标记,id一致则说明是同一数据集。每一个master都有唯一的replid,slave则会继承master节点的replid;
offset:
偏移量,随着记录在repl_baklog中的数据增多而逐渐增大。slave完成同步时也会记录当前同步的offset。如果slave的offset小于master的offset,说明slave数据落后于master,需要更新。因此slave做数据同步,必须向master声明自己的replication id 和offset,master才可以判断到底需要同步哪些数据。
全量同步流程:

2、增量同步

其中repl_baklog大小有上限,写满后会覆盖最早的数据。如果slave断开时间过久,导致尚未备份的数据被覆盖,则无法基于log做增量同步,只能再次全量同步。
3、优化
可以从以下几个方面来优化Redis主从集群:

4、总结

边栏推荐
- Special effects - click with the mouse and the fireworks will burst
- mysql获取自增行标(区别mysql版本)
- [learning notes] possible reasons and optimization methods for white screen on Web pages
- (note sorting is not completed) [graph theory: find the shortest path of single source]
- STM32外部中断(寄存器版本)
- tensorflow scatter_nd函数
- Redis special data type Geo
- XXL execute node error log swiping
- mysql自动生成创建时间和更新时间
- Camera Hal OEM模块 ---- cmr_grab.c
猜你喜欢

MySQL gets the self incrementing line mark (different from MySQL version)

Introduction to pyqt5 - student management system

STM32 MP3 music player based on FatFs r0.14b & SD card (also a simple application of FatFs)

Account and authority management

Redis特殊数据类型-HyperLogLog

kubernetes简介和架构及其原理

创建WPF项目

Machine learning case: smoking in pregnant women and fetal health

Sealos packages and deploys kubesphere container platform

Requirements already satisfied: and read timed out. problem solving methods appear during the installation of snownlp package
随机推荐
MapReduce (I)
【学习笔记】从汇编看 a+++a与 a+a++的区别
Redis基本类型-结合Set
Nodejs enables multi process and inter process communication
Detailed explanation of class loader and parental delegation mechanism
Kubernetes' deployment, service concept, dynamic capacity expansion
ADB interaction - kill the ugly default shell interface
HashSet to array
反射
Special effects - when the mouse moves, there will be a custom expression trail
kubernetes简介(kubernetes优点)
使用root用户为创建新用户并设置密码
歹徒逃亡3
Redis basic type - combined with set
Redis入门
Special effects - return to the top (kitten effects)
【C语言】操作符详解(深入理解+整理归类)
Getting started with redis
tensorflow boolean_mask函数
Mac can't connect to local MySQL server through socket '/tmp/mysql Sock '(2) problem