本次搭建一个一主2从的模式
1.进入redis目录中,创建3个conf文件,分别是redis6379.conf 、 redis6380.conf 、redis6381.conf。
redis6379.conf配置如下:
include redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb
redis6380.conf配置如下:
include redis.conf
pidfile /var/run/redis_6380.pid
port 6380
dbfilename dump6380.rdb
redis6381.conf配置如下:
include redis.conf
pidfile /var/run/redis_6381.pid
port 6381
dbfilename dump6381.rdb
2.分别启动 6379、6380、6381这3个redis服务
3.进入6380和6381的客户端,输入命令slaveof ip:端口 把他们变更成从服务。然后输入命令info replication查看当前服务节点的状态。
[root@localhost redis-6.2.1]# redis-cli -h 192.168.80.140 -p 6380
127.0.0.1:6380> slaveof 192.168.80.140 6379
OK
127.0.0.1:6380> info replication
# Replication
role:slave
[root@localhost redis-6.2.1]# redis-cli -h 192.168.80.140 -p 6381
127.0.0.1:6381> slaveof 192.168.80.140 6379
OK
127.0.0.1:6381> info replication
# Replication
role:slave
进入 6379节点中查看6380和6381已变成了从节点。
192.168.80.140:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.80.140,port=6380,state=online,offset=42,lag=0
slave1:ip=192.168.80.140,port=6381,state=online,offset=42,lag=0
4.在主节点6379中添加一个key,然后在6380中能看到这个key,说明节点数据已同步。