共计 4661 个字符,预计需要花费 12 分钟才能阅读完成。
官网介绍看这里 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 side
- avoid 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 the slave-read-only
option in the redis.conf file, and can be enabled and disabled at runtime using CONFIG 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 like DEBUG
or CONFIG
are still enabled. However, security of read-only instances can be improved by disabling commands in redis.conf using the rename-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 enough backlog, only the difference (what the slave missed) is sent. Otherwise what is called a full resynchronization is 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 the SYNCcommand. 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 的文章您也可能喜欢,不妨参考下:
Ubuntu 14.04 下 Redis 安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm
Redis 主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm
CentOS 7 下 Redis 的安装与配置 http://www.linuxidc.com/Linux/2017-02/140363.htm
Ubuntu 14.04 安装 Redis 与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm
Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm
CentOS 7.0 安装 Redis 3.2.1 详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm
Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm
Ubuntu 15.10 下 Redis 集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm
Redis 实战 中文 PDF http://www.linuxidc.com/Linux/2016-04/129932.htm
Redis 热迁移实战总结 http://www.linuxidc.com/Linux/2017-02/141083.htm
Redis3.0 配置文件详解 http://www.linuxidc.com/Linux/2017-03/141369.htm
Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141375.htm