環(huán)境描述:
主Redis:192.168.10.1 6379
從redis:192.168.10.2 6380
主從配置
- 將主從redis配置文件redis.conf中的aemonize no 改為 yes
- 修改從redis配置文件redis.conf中的port 6379 改為 6380猾普,添加slaveof 192.168.10.1 6379
- 啟動主從服務(wù)
主redis:
[root@localhost redis-2.8.3]# src/redis-server/soft/redis-2.8.3-master/redis-2.8.3/redis.conf
從redis:
[root@localhost redis-2.8.3]# src/redis-server /soft/redis-2.8.3-slave/redis-2.8.3/redis.conf
- 測試數(shù)據(jù)同步
主redis:
[root@localhost redis-2.8.3]# src/redis-cli -p 6379 127.0.0.1:6379> set name abc OK 127.0.0.1:6379> get name "abc" 127.0.0.1:6379>
從redis:
[root@localhost redis-2.8.3]# src/redis-cli -p 6380 127.0.0.1:6380> get name "abc" 127.0.0.1:6380>
- 默認是讀寫分離的
在從redis:
[root@localhost redis-2.8.3]# src/redis-cli -p 6380 127.0.0.1:6380> set name 123 (error) READONLY You can't write against a read only slave.
主從切換
- 停止主redis
[root@localhost redis-2.8.3]# src/redis-cli -n 6379 shutdown [root@localhost redis-2.8.3]# src/redis-cli -p 6379 Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected>
- 將從redis設(shè)成主redis
[root@localhost redis-2.8.3]# src/redis-cli -p 6380 slaveof NO ONE OK
- 測試從redis是否切換從主redis
[root@localhost redis-2.8.3]# src/redis-cli -p 6380 127.0.0.1:6380> set name 123 OK 127.0.0.1:6380> get name "123" 127.0.0.1:6380>
- 原來的主redis恢復(fù)正常了宦赠,要重新切換回去
1)將現(xiàn)在的主redis的數(shù)據(jù)進行保存
[root@localhost redis-2.8.3]# src/redis-cli -p 6380 127.0.0.1:6380> get name "abc" 127.0.0.1:6380> set name 123 OK 127.0.0.1:6380> get name "123" 127.0.0.1:6380> save OK 127.0.0.1:6380> get name "123" 127.0.0.1:6380>
2)將現(xiàn)在的主redis根目錄下dump.rdb文件拷貝覆蓋到原來主redis的根目錄
3)啟動原來的主redis
[root@localhost redis-2.8.3]# src/redis-server /soft/redis-2.8.3-master/redis-2.8.3/redis.conf
4)在現(xiàn)在的主redis中切換
[root@localhost redis-2.8.3]# src/redis-cli -p 6379 slaveof 192.168.10.1 6380 OK