redis主从配置及主从切换

ゝ一世哀愁。 2022-08-03 15:47 297阅读 0赞

http://blog.csdn.net/zfl092005/article/details/17523945

环境描述:
主redis:192.168.10.1 6379
从redis:192.168.10.2 6380

一、主从配置

1、将主从redis配置文件redis.conf中的aemonize no 改为 ``yes

2、修改从redis配置文件redis.conf中的port 6379 改为 6380,添加slaveof 192.168.10.1 6379

3、启动主从服务

  1. redis
  2. \[root@localhost redis-2.8.3\]\# src/redis-server /soft/redis-2.8.3-master/redis-2.8.3/redis.conf
  3. redis
  4. \[root@localhost redis-2.8.3\]\# src/redis-server /soft/redis-2.8.3-slave/redis-2.8.3/redis.conf

4、测试数据同步

  1. redis
  2. \[root@localhost redis-2.8.3\]\# src/redis-cli -p 6379
  3. 127.0.0.1:6379> set name abc
  4. OK
  5. 127.0.0.1:6379> get name
  6. "abc"
  7. 127.0.0.1:6379>
  8. redis
  9. \[root@localhost redis-2.8.3\]\# src/redis-cli -p 6380
  10. 127.0.0.1:6380> get name
  11. "abc"
  12. 127.0.0.1:6380>

5、默认是读写分离的

  1. 在从redis
  2. \[root@localhost redis-2.8.3\]\# src/redis-cli -p 6380
  3. 127.0.0.1:6380> set name 123
  4. (error) READONLY You can't write against a read only slave.

二、主从切换

  1. 1、停止主redis
  2. \[root@localhost redis-2.8.3\]\# src/redis-cli -n 6379 shutdown
  3. \[root@localhost redis-2.8.3\]\# src/redis-cli -p 6379
  4. Could not connect to Redis at 127.0.0.1:6379: Connection refused
  5. not connected>
  6. 2、将从redis设成主redis
  7. \[root@localhost redis-2.8.3\]\# src/redis-cli -p 6380 slaveof NO ONE
  8. OK
  9. 3、测试从redis是否切换从主redis
  10. \[root@localhost redis-2.8.3\]\# src/redis-cli -p 6380
  11. 127.0.0.1:6380> set name 123
  12. OK
  13. 127.0.0.1:6380> get name
  14. "123"
  15. 127.0.0.1:6380>
  16. 4、原来的主redis恢复正常了,要重新切换回去
  17. 1)将现在的主redis的数据进行保存
  18. \[root@localhost redis-2.8.3\]\# src/redis-cli -p 6380
  19. 127.0.0.1:6380> get name
  20. "abc"
  21. 127.0.0.1:6380> set name 123
  22. OK
  23. 127.0.0.1:6380> get name
  24. "123"
  25. 127.0.0.1:6380> save
  26. OK
  27. 127.0.0.1:6380> get name
  28. "123"
  29. 127.0.0.1:6380>
  30. 2)将现在的主redis根目录下dump.rdb文件拷贝覆盖到原来主redis的根目录
  31. 3)启动原来的主redis
  32. \[root@localhost redis-2.8.3\]\# src/redis-server /soft/redis-2.8.3-master/redis-2.8.3/redis.conf
  33. 4)在现在的主redis中切换
  34. \[root@localhost redis-2.8.3\]\# src/redis-cli -p 6380 slaveof 192.168.10.1 6379
  35. OK

发表评论

表情:
评论列表 (有 0 条评论,297人围观)

还没有评论,来说两句吧...

相关阅读