这里假设在主从服务器上单实例Redis已经安装配置好。如果没有安装,可参考文章《Redis及PHP Redis扩展安装笔记》
一、双机主从演示
环境
主:10.235.25.242 【CentOS release 6.5】redis_version:2.4.10
从:10.235.25.241 【CentOS release 6.9】redis_version:3.2.11
0、主服务器上启动并连接Redis
/usr/sbin/redis-server /etc/redis.conf /usr/bin/redis-cli -h 10.235.25.242 -p 6379 redis 10.235.25.242:6379> redis 10.235.25.242:6379>
1、从服务器上启动并连接Redis
/usr/bin/redis-server /etc/redis.conf /usr/bin/redis-cli -h 10.235.25.241 -p 6379 10.235.25.241:6379> 10.235.25.241:6379>
2、查看主从库Redis 角色
通过info命令,低版本Redis info命令不能传参,高版本可以用info replication
# 主库role为master redis 10.235.25.242:6379> info redis_version:2.4.10 role:master # 从库role为master 10.235.25.241:6379> info replication # Replication role:master connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
3、设置从库角色
# 从库上执行slaveof 10.235.25.241:6379> slaveof 10.235.25.242 6379 OK # 查看从库角色role为slave 10.235.25.241:6379> info replication # Replication role:slave master_host:10.235.25.242 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:16889 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 # 查看主库角色role为master redis 10.235.25.242:6379> info role:master slave0:10.235.25.241,34518,online db0:keys=232,expires=0
4、同步测试
# 主库上写入 redis 10.235.25.242:6379> set salmonl niliu OK redis 10.235.25.242:6379> get salmonl "niliu" # 从库查看 10.235.25.241:6379> get salmonl "niliu"
5、通过配置设置从库
通过命令slaveof设置,Redis重启后就会失效。最好还是设置在配置文件中。
在/etc/redis.conf(这里需要修改自己的配置文件)中增加如下命令
slaveof 10.235.25.242 6379
二、单机主从演示
环境:10.235.25.241 【CentOS release 6.9】redis_version:3.2.11
0、在原有redis配置的基础上增加两个配置
cp /etc/redis.conf /etc/redis_6380.conf cp /etc/redis.conf /etc/redis_6381.conf
1、调整新增配置
vim /etc/redis_6380.conf port 6380 pidfile /var/run/redis/redis_6380.pid slaveof 127.0.0.1 6379 vim /etc/redis_6381.conf port 6381 pidfile /var/run/redis/redis_6381.pid slaveof 127.0.0.1 6379
2、启动1一个master和2个slave
/usr/bin/redis-server /etc/redis.conf /usr/bin/redis-server /etc/redis_6380.conf /usr/bin/redis-server /etc/redis_6381.conf
3、验证
打开3个终端窗口, 通过info replication来观察
# master /usr/bin/redis-cli -h 10.235.25.241 -p 6379 10.235.25.241:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=1153,lag=0 slave1:ip=127.0.0.1,port=6381,state=online,offset=1153,lag=1 master_repl_offset:1153 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:1152 # slave1 /usr/bin/redis-cli -h 10.235.25.241 -p 6380 10.235.25.241:6380> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:7 master_sync_in_progress:0 slave_repl_offset:1279 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 # slave2 /usr/bin/redis-cli -h 10.235.25.241 -p 6381 10.235.25.241:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:1279 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
4、测试
# master 10.235.25.241:6379> set salmonl niliu OK 10.235.25.241:6379> get salmonl "niliu" # slave1 10.235.25.241:6380> get salmonl "niliu" # slave2 10.235.25.241:6381> get salmonl "niliu"