环境描述:
主Redis:192.168.10.1 6379从redis:192.168.10.2 6380一、主从配置
1、将主从redis配置文件redis.conf中的daemonize no 改为 yes2、 修改从redis配置文件redis.conf中的port 6379 改为 6380,配置如下:
slaveof 192.168.10.1 6380 #指定master的ip和端口masterauth 123456 #主服务器有密码需开启此项配置
从redis:
redis 127.0.0.1:6380> get name"redis-6379"redis 127.0.0.1:6380>5、默认是读写分离的(从2.6开始,从redis默认为只读)
在从redis:redis-server redis-6380.confredis 127.0.0.1:6380> set name 123(error) READONLY You can't write against a read only slave.二、主从切换
1、停止主redis[root@localhost redis-2.8.3]# redis-cli -n 6379 shutdown[root@localhost redis-2.8.3]# redis-cli -p 6379Could not connect to Redis at 127.0.0.1:6379: Connection refusednot connected>2、将从redis设成主redis
[root@localhost redis-2.8.3]# redis-cli -p 6380 slaveof NO ONEOK3、测试从redis是否切换从主redis
[root@localhost redis-2.8.3]# redis-cli -p 6380127.0.0.1:6380> set name 123OK127.0.0.1:6380> get name"123"127.0.0.1:6380>4、查询redis状态
127.0.0.1:6380>info...vm_enabled:0role:masterdb0:keys=1446,expires=2245、原来的主redis恢复正常了,要重新切换回去
a、将现在的主redis的数据进行保存127.0.0.1:6380> get name"abc"127.0.0.1:6380> set name 123OK127.0.0.1:6380> get name"123"127.0.0.1:6380> saveOK127.0.0.1:6380> get name"123"127.0.0.1:6380>b、将现在的主redis根目录下dump.rdb文件拷贝覆盖到原来主redis的根目录
c、启动原来的主redis主redis# redis-server redis-6379.confd、在现在的主redis中切换主redis# redis-cli -p 6380 slaveof 192.168.10.1 6379e、查询redis状态
redis 127.0.0.1:6379> infovm_enabled:0role:masterdb0:keys=1341,expires=110 redis 127.0.0.1:6380> infovm_enabled:0role:slavemaster_host:192.168.10.1