測試環境:
Linux ubuntu 3.11.0-12-generic
2GB Mem
1 core of Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz
Redis 2.6.13
如下 R1 表示master, R2表示slave
配置採用運維組標準配置,默認主從配置同樣,在從節點啓動後設置爲slaveredis
R1: 127.0.0.1 6378 R2: 127.0.0.1 6379
MASTER -> 'connected_slaves': 1, #可判斷slave存活情況 SLAVE -> 'master_host': '127.0.0.1', 'master_last_io_seconds_ago': 5, 'master_link_status': 'up', #可判斷master與slave存活情況 'master_port': 6378
In [32]: r1.set('key', 'value') Out[32]: True In [34]: r2.get('key') Out[34]: 'value'
向slave寫入後發現報錯ubuntu
In [35]: r2.set('key2', 'value') ReadOnlyError: You can't write against a read only slave
Crash掉主節點:運維
'master_host': '127.0.0.1', 'master_last_io_seconds_ago': -1, 'master_link_down_since_seconds': 4, 'master_link_status': 'down', 'master_port': 6378
重啓主節點後,slave自動從重新創建鏈接測試
'master_host': '127.0.0.1', 'master_last_io_seconds_ago': 9, 'master_link_status': 'up', 'master_port': 6378,
即便master掛掉,slave寫入依然報錯code
SLAVE -> 'master_host': '127.0.0.1', 'master_last_io_seconds_ago': -1, 'master_link_down_since_seconds': 6, 'master_link_status': 'down', 'master_port': 6378 In [37]: r2.set('key', 'value') ReadOnlyError: You can't write against a read only slave.
設置slave爲readonly,發現報錯get
zb@ubuntu:~/codes/test$ redis-cli -h 127.0.0.1 -p 6379 set slave-read-only no (error) READONLY You can't write against a read only slave.
須要關閉slave的slave特性, 將其升級爲主節點同步
zb@ubuntu:~/codes/test$ redis-cli -h 127.0.0.1 -p 6379 slaveof NO ONE OK zb@ubuntu:~/codes/test$ redis-cli -h 127.0.0.1 -p 6379 set slave-read-only no OK
升級爲主節點後,繼續向slave寫入:it
In [38]: r2.set('key', 'value') Out[38]: True
In [41]: r1.get('key') In [42]:
In [43]: r1.get('key') Out[43]: 'value'
向slave寫入:io
In [45]: r2.set('key2', 'value') ReadOnlyError: You can't write against a read only slave.
能夠向slave寫入數據,可是不會同步到masterast
In [50]: r2.set('key2', 'value') Out[50]: True
SLAVE -> 'master_link_status': 'down' In [75]: r2.set('key2', 'value') Out[75]: True
重啓Master(此時的master無數據):
In [85]: r2.get('key2') In [86]: r2.get('key3')
能夠看到,重啓了Master後,slave同步了master狀態,因爲master沒有數據,slave的數據同時被刪除
總結(部分測試未寫出來):