redis_6378.conf
、redis_6377.conf
。他們將在6378
、6377
兩個端口啓動分別修改兩個配置:redis
# redis_6378.conf ... port 6378 slaveof 127.0.0.1 6379 ...
# redis_6377.conf ... port 6377 slaveof 127.0.0.1 6378 ...
說明:這裏使用監聽6379的節點做爲主節點,6378
做爲6379
的子節點,6377
做爲6378
的子節點。shell
啓動6379
主節點socket
$ redis-server 68931:C 29 May 10:53:54.234 # oO0OoO0OoO0Oo Redis is starting # 省略部分過程 _.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 68931 # 省略部分過程 68931:M 29 May 10:53:54.237 * Ready to accept connections
啓動6378
子節點code
$ redis-server ~/Desktop/redis_6378.conf # 省略部份內容 _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6378 | `-._ `._ / _.-' | PID: 69033 # 省略部份內容 # 這裏說明啓動成功了 69033:S 29 May 10:56:24.581 * Ready to accept connections # 開始鏈接主節點 69033:S 29 May 10:56:24.581 * Connecting to MASTER 127.0.0.1:6379 # 開始同步 69033:S 29 May 10:56:24.581 * MASTER <-> SLAVE sync started 69033:S 29 May 10:56:24.581 * Non blocking connect for SYNC fired the event. 69033:S 29 May 10:56:24.581 * Master replied to PING, replication can continue... # 嘗試增量同步 69033:S 29 May 10:56:24.581 * Trying a partial resynchronization (request 87953c7dc97cf1192a243fe5d7708c11acbd0e0f:1). # 全量同步 69033:S 29 May 10:56:24.582 * Full resync from master: 98a56521a0a958295e499853621029d19ef6b357:0 69033:S 29 May 10:56:24.582 * Discarding previously cached master state. 69033:S 29 May 10:56:24.684 * MASTER <-> SLAVE sync: receiving 826 bytes from master 69033:S 29 May 10:56:24.685 * MASTER <-> SLAVE sync: Flushing old data 69033:S 29 May 10:56:24.685 * MASTER <-> SLAVE sync: Loading DB in memory 69033:S 29 May 10:56:24.685 * MASTER <-> SLAVE sync: Finished with success
此時的6379
主節點server
# 省略部份內容 68931:M 29 May 10:53:54.237 * Ready to accept connections # 上面是以前的狀態,下面是`6378`鏈接進來之後的狀態 68931:M 29 May 10:56:24.581 * Slave 127.0.0.1:6378 asks for synchronization 68931:M 29 May 10:56:24.581 * Partial resynchronization not accepted: Replication ID mismatch (Slave asked for '87953c7dc97cf1192a243fe5d7708c11acbd0e0f', my replication IDs are 'dbf6c826156fa5140d891996afcf8cc4f21b61fd' and '0000000000000000000000000000000000000000') 68931:M 29 May 10:56:24.581 * Starting BGSAVE for SYNC with target: disk 68931:M 29 May 10:56:24.582 * Background saving started by pid 69034 69034:C 29 May 10:56:24.584 * DB saved on disk 68931:M 29 May 10:56:24.684 * Background saving terminated with success # 同步成功 68931:M 29 May 10:56:24.684 * Synchronization with slave 127.0.0.1:6378 succeeded
啓動6377
子節點get
redis-server ~/Desktop/redis_6377.conf # 省略部份內容 _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6377 | `-._ `._ / _.-' | PID: 69207 # 啓動完成 69207:S 29 May 11:02:22.569 * Ready to accept connections # 鏈接上一級節點 69207:S 29 May 11:02:22.569 * Connecting to MASTER 127.0.0.1:6378 69207:S 29 May 11:02:22.570 * MASTER <-> SLAVE sync started 69207:S 29 May 11:02:22.570 * Non blocking connect for SYNC fired the event. 69207:S 29 May 11:02:22.570 * Master replied to PING, replication can continue... 69207:S 29 May 11:02:22.570 * Trying a partial resynchronization (request 98a56521a0a958295e499853621029d19ef6b357:1). 69207:S 29 May 11:02:22.570 * Successful partial resynchronization with master. 69207:S 29 May 11:02:22.570 * MASTER <-> SLAVE sync: Master accepted a Partial Resynchronization.
使用redis-cli
向6379
主節點寫入數據同步
$ redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> set name redis OK 127.0.0.1:6379> get name "redis"
使用redis-cli
鏈接6378
子節點並讀取在主節點寫入的數據it
$ redis-cli -h 127.0.0.1 -p 6378 127.0.0.1:6378> get name "redis"
使用redis-cli
鏈接637 7
子節點並讀去在主節點寫入的數據io
$ redis-cli -h 127.0.0.1 -p 6377 127.0.0.1:6377> get name "redis"
使用redis-cli
鏈接6378
子節點並嘗試寫入數據event
127.0.0.1:6378> set name redis-cli (error) READONLY You can't write against a read only slave.
說明:子節點能夠讀取到主節點寫入的數據,說明同步成功了,子節點沒法寫入數據,由於子節點是默認只讀的,修改配置能夠作到子節點可讀寫
關閉6378子節點
$ redis-cli -h 127.0.0.1 -p 6378 127.0.0.1:6378> shutdown not connected>
此時的6378
# 上面是以前的狀態,下面是`6378`關機時候的狀態 9033:S 29 May 11:09:54.587 # User requested shutdown... 69033:S 29 May 11:09:54.587 * Saving the final RDB snapshot before exiting. 69033:S 29 May 11:09:54.588 * DB saved on disk 69033:S 29 May 11:09:54.588 * Removing the pid file. 69033:S 29 May 11:09:54.589 # Redis is now ready to exit, bye bye...
6379
# 上面是以前的狀態,下面是`6378`關機以後的狀態 68931:M 29 May 11:09:54.589 # Connection with slave 127.0.0.1:6378 lost.
此時的6377
... # 6377 會一直嘗試從新鏈接`6378` 69219:S 29 May 11:12:07.132 * Connecting to MASTER 127.0.0.1:6378 69219:S 29 May 11:12:07.132 * MASTER <-> SLAVE sync started 69219:S 29 May 11:12:07.132 # Error condition on socket for SYNC: Connection refused 69219:S 29 May 11:12:07.132 * Connecting to MASTER 127.0.0.1:6378 69219:S 29 May 11:12:07.132 * MASTER <-> SLAVE sync started 69219:S 29 May 11:12:07.132 # Error condition on socket for SYNC: Connection refused ...
修改配置
# redis_6378.conf slave-read-only no
從新啓動6378
,並向6378
寫入數據
$ redis-cli -h 127.0.0.1 -p 6378 # 此時配置子節點不是隻讀的,因此能夠寫入 127.0.0.1:6378> set name redis-cli OK 127.0.0.1:6378> get name "redis-cli"
鏈接6377
並讀取6378
寫入的key
$ redis-cli -h 127.0.0.1 -p 6377 127.0.0.1:6377> get name "redis"
鏈接6379
並讀取6378
寫入的key
$ redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> GET name "redis"
說明:雖然此時的6378
子節點是可讀的,可是並不會傳播到其餘子節點或者主節點,而且在下次同步的時候,將會被刪除。