官網介紹看這里 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,