主从切换:
1.环境描述。
master:192.168.1.93 6379
127.0.0.1:6379> set result success
OK
127.0.0.1:6379> get result
"success"
127.0.0.1:6379>
slave:192.168.1.111 6380
127.0.0.1:6380> get result
"success"
127.0.0.1:6380> set result 123
(error) READONLY You can't write against a read only slave.
127.0.0.1:6380>
2.停止master。
127.0.0.1:6379> shutdown
not connected>
[root@localhost src]# ps -ef|grep redis
root 8353 8263 0 09:35 pts/0 00:00:00 grep redis
[root@localhost src]#
3.将slave设为master。
127.0.0.1:6380> slaveof NO ONE
OK
127.0.0.1:6380> set result 123
OK
127.0.0.1:6380> get result
"123"
127.0.0.1:6380>
4.将原来的master设为slave。
127.0.0.1:6379> slaveof 192.168.1.111 6380
OK
127.0.0.1:6379> set result slave
(error) READONLY You can't write against a read only slave.
127.0.0.1:6379>
5.继续第3步,将某一个slave设为master,若此时原master恢复正常,要重新切换回去。
1)将现在的数据进行保存,并将自己设为salve。
127.0.0.1:6380> set result master
OK
127.0.0.1:6380> save
OK
127.0.0.1:6380> slaveof 192.168.1.93 6379
OK
127.0.0.1:6380>
2)将现在的master根目录下的dump.rdb文件拷贝覆盖到原来主redis的根目录。
3)启动原来的master。
127.0.0.1:6379> get result
"master"
127.0.0.1:6379> set result normal
127.0.0.1:6379> get result
"normal"
127.0.0.1:6379>
127.0.0.1:6380> get result
"normal"
127.0.0.1:6380> set result slave
(error) READONLY You can't write against a read only slave.
127.0.0.1:6380>