当前位置:网站首页>Redis入门完整教程:复制配置
Redis入门完整教程:复制配置
2022-07-06 19:11:00 【谷哥学术】
6.1.1 建立复制
参与复制的Redis实例划分为主节点(master)和从节点(slave)。默认
情况下,Redis都是主节点。每个从节点只能有一个主节点,而主节点可以
同时具有多个从节点。复制的数据流是单向的,只能由主节点复制到从节
点。配置复制的方式有以下三种:
1)在配置文件中加入slaveof{masterHost}{masterPort}随Redis启动生
效。
2)在redis-server启动命令后加入--slaveof{masterHost}{masterPort}生
效。
3)直接使用命令:slaveof{masterHost}{masterPort}生效。
综上所述,slaveof命令在使用时,可以运行期动态配置,也可以提前写
到配置文件中。例如本地启动两个端口为6379和6380的Redis节点,在
127.0.0.1:6380执行如下命令:
127.0.0.1:6380>slaveof 127.0.0.1 6379
slaveof配置都是在从节点发起,这时6379作为主节点,6380作为从节
点。复制关系建立后执行如下命令测试:
127.0.0.1:6379>set hello redis
OK
127.0.0.1:6379>get hello
"redis"
127.0.0.1:6380>get hello
"redis"
从运行结果中看到复制已经工作了,针对主节点6379的任何修改都可以
同步到从节点6380中,复制过程如图6-1所示。

slaveof本身是异步命令,执行slaveof命令时,节点只保存主节点信息后
返回,后续复制流程在节点内部异步执行,具体细节见之后6-3复制原理小
节。主从节点复制成功建立后,可以使用info replication命令查看复制相关
状态,如下所示。
1)主节点6379复制状态信息:
127.0.0.1:6379>info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6379,state=online,offset=43,lag=0
....
2)从节点6380复制状态信息:
127.0.0.1:6380>info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
...
6.1.2 断开复制
slaveof命令不但可以建立复制,还可以在从节点执行slaveof no one来断
开与主节点复制关系。例如在6380节点上执行slaveof no one来断开复制,如
图6-2所示。

断开复制主要流程:
1)断开与主节点复制关系。
2)从节点晋升为主节点。
从节点断开复制后并不会抛弃原有数据,只是无法再获取主节点上的数
据变化。
通过slaveof命令还可以实现切主操作,所谓切主是指把当前从节点对主
节点的复制切换到另一个主节点。执行slaveof{newMasterIp}
{newMasterPort}命令即可,例如把6380节点从原来的复制6379节点变为复
制6381节点,如图6-3所示。
切主操作流程如下:
1)断开与旧主节点复制关系。
2)与新主节点建立复制关系。
3)删除从节点当前所有数据。
4)对新主节点进行复制操作。
运维提示
切主后从节点会清空之前所有的数据,线上人工操作时小心slaveof在错
误的节点上执行或者指向错误的主节点。
6.1.3 安全性
对于数据比较重要的节点,主节点会通过设置requirepass参数进行密码
验证,这时所有的客户端访问必须使用auth命令实行校验。从节点与主节点
的复制连接是通过一个特殊标识的客户端来完成,因此需要配置从节点的
masterauth参数与主节点密码保持一致,这样从节点才可以正确地连接到主
节点并发起复制流程。
6.1.4 只读
默认情况下,从节点使用slave-read-only=yes配置为只读模式。由于复
制只能从主节点到从节点,对于从节点的任何修改主节点都无法感知,修改
从节点会造成主从数据不一致。因此建议线上不要修改从节点的只读模式。
6.1.5 传输延迟
主从节点一般部署在不同机器上,复制时的网络延迟就成为需要考虑的
问题,Redis为我们提供了repl-disable-tcp-nodelay参数用于控制是否关闭
TCP_NODELAY,默认关闭,说明如下:
·当关闭时,主节点产生的命令数据无论大小都会及时地发送给从节
点,这样主从之间延迟会变小,但增加了网络带宽的消耗。适用于主从之间
的网络环境良好的场景,如同机架或同机房部署。
·当开启时,主节点会合并较小的TCP数据包从而节省带宽。默认发送
时间间隔取决于Linux的内核,一般默认为40毫秒。这种配置节省了带宽但
增大主从之间的延迟。适用于主从网络环境复杂或带宽紧张的场景,如跨机
房部署。
运维提示
部署主从节点时需要考虑网络延迟、带宽使用率、防灾级别等因素,如
要求低延迟时,建议同机架或同机房部署并关闭repl-disable-tcp-nodelay;如
果考虑高容灾性,可以同城跨机房部署并开启repl-disable-tcp-nodelay。
边栏推荐
- The cities research center of New York University recruits master of science and postdoctoral students
- MFC Windows 程序设计[147]之ODBC数据库连接(附源码)
- CSDN summer camp course project analysis
- Derivative, partial derivative, directional derivative
- [software test] the most complete interview questions and answers. I'm familiar with the full text. If I don't win the offer, I'll lose
- wzoi 1~200
- 如何设计好接口测试用例?教你几个小技巧,轻松稿定
- Unity custom webgl packaging template
- The panel floating with the mouse in unity can adapt to the size of text content
- Hash table and full comments
猜你喜欢

Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?

Detailed explanation of line segment tree (including tested code implementation)

Douban average 9 x. Five God books in the distributed field!

What management points should be paid attention to when implementing MES management system

如何设计好接口测试用例?教你几个小技巧,轻松稿定

Unity custom webgl packaging template

用全连接+softmax对图片的feature进行分类

This week's hot open source project!

Web3's need for law

3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
随机推荐
软件测试——Jmeter接口测试之常用断言
Summary of basic debugging steps of S120 driver
KYSL 海康摄像头 8247 h9 isapi测试
[leetcode]Search for a Range
Pioneer of Web3: virtual human
Common fitting models and application methods of PCL
Planning and design of double click hot standby layer 2 network based on ENSP firewall
Detailed explanation of line segment tree (including tested code implementation)
3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
Use of pgpool II and pgpooladmin
你不可不知道的Selenium 8种元素定位方法,简单且实用
Huitong programming introductory course - 2A breakthrough
MetaForce原力元宇宙佛萨奇2.0智能合约系统开发(源码部署)
The so-called consumer Internet only matches and connects industry information, and does not change the industry itself
[C # notes] reading and writing of the contents of text files
Django数据库(SQlite)基本入门使用教程
fiddler的使用
C语言练习题_1
wireshark安装
Douban average 9 x. Five God books in the distributed field!