官网介绍看这里 http://redis.io/topics/replication
主从复制:就是主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主
Redis replication is a very simple to use and configure master-slave replication that allows slave Redis servers to be exact copies of master servers.
用处:读写分离,性能扩展;容灾快速回复
特点:
Redis uses asynchronous replication 异步复制A master can have multiple slaves 一主可以多从 并联关系Slaves are able to accept connections from other slaves 串联关系Redis replication is non-blocking on the master side 复制是非阻塞模式Replication is also non-blocking on the slave sideavoid the cost of having the master write the full dataset to disk 防止数据溢出写入硬盘
一、配从(服务器)不配主(服务器)
配置三个服务器6379,6380,6381,
需要复制三份配置文件,并分别更改端口号,pid文件名字,dump.rdb名字,
Appendonly关掉;
info replication查询主从复制的信息,初始时都是master
salveof <ip> <port>设置主仆关系,
①并联关系:80和81都是79的slave
②串联关系: 79是80的master,80是81的master
先来并联关系:
①并联关系:80和81都是79的slave
1 切入点问题?slave1、slave2是从头开始复制还是从切入点开始复制?比如从k4进来,那之前的123是否也可以复制 .
2 从机是否可以写?set可否?
127.0.0.1:6380> set k8 v8
(error) READONLY You can't write against a read only slave.
Read-only slave
Since Redis 2.6, slaves support a read-only mode that is enabled by default. This behavior is controlled by theslave-read-only
option in the redis.conf file, and can be enabled and disabled at runtime usingCONFIG SET.
Read-only slaves will reject all write commands, so that it is not possible to write to a slave because of a mistake. This does not mean that the feature is intended to expose a slave instance to the internet or more generally to a network where untrusted clients exist, because administrative commands likeDEBUG
orCONFIG
are still enabled. However, security of read-only instances can be improved by disabling commands in redis.conf using therename-command
directive.
You may wonder why it is possible to revert the read-only setting and have slave instances that can be target of write operations. While those writes will be discarded if the slave and the master resynchronize or if the slave is restarted, there are a few legitimate use case for storing ephemeral data in writable slaves. However in the future it is possible that this feature will be dropped.
3 主机shutdown后情况如何?从机是上位还是原地待命
4 主机又回来了后,主机新增记录,从机还能否顺利复制?
5 其中一台从机down后情况如何?依照原有它能跟上大部队吗?
配置文件
②串联关系: 79是80的master,80是81的master
薪火相传上一个slave可以是下一个slave的Master,slave同样可以接收其他slaves的连接和同步请求,那么该slave作为了链条中下一个的master, 可以有效减轻master的写压力,去中心化降低风险。用 slaveof <ip> <port>中途变更转向:会清除之前的数据,重新建立拷贝最新的风险是一旦某个slave宕机,后面的slave都没法备份反客为主当一个master宕机后,后面的slave可以立刻升为master,其后面的slave不用做任何修改。用 slaveof no one 将从机变为主机。
主从复制的原理:
每次从机联通后,都会给主机发送sync指令,主机立刻进行存盘操作,发送RDB文件给从机 ,
从机收到RDB文件后,进行全盘加载,之后每次主机的写操作,都会立刻发送给从机,从机执行相同的命令
How Redis replication works
If you set up a slave, upon connection it sends a PSYNC command.
If this is a reconnection and the master has enoughbacklog, only the difference (what the slave missed) is sent. Otherwise what is called afull resynchronizationis triggered.触发再同步
When a full resynchronization is triggered, the master starts a background saving process in order to produce an RDB file. At the same time it starts to buffer all new write commands received from the clients. When the background saving is complete, the master transfers the database file to the slave, which saves it on disk, and then loads it into memory. The master will then send all buffered commands to the slave. This is done as a stream of commands and is in the same format of the Redis protocol itself.
You can try it yourself via telnet. Connect to the Redis port while the server is doing some work and issue theSYNCcommand. You'll see a bulk transfer and then every command received by the master will be re-issued in the telnet session.
Slaves are able to automatically reconnect when the master-slave link goes down for some reason. If the master receives multiple concurrent slave synchronization requests, it performs a single background save in order to serve all of them.
二、哨兵模式sentinal
反客为主的自动版,能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库.
(1)、新建哨兵配置文件
自定义的/myredis目录下新建sentinel.conf文件,名字绝不能错在配置文件中填写内容:
sentinel monitor mymaster 127.0.0.1 6379 1
其中mymaster为监控对象起的服务器名称, 1 为 至少有多少个哨兵同意迁移的数量。执行redis-sentinel /myredis/sentinel.conf 启动哨兵模式可以看到Running in sentinel mode ,和显示79master 80,81slave的信息
故障恢复
主机79shutdown
可以看到new epoch-选举leader—elect leader -----switch master 成81,
如果觉得《Redis主从复制-Replication》对你有帮助,请点赞、收藏,并留下你的观点哦!