Redis 主从安装部署

配置模版:
master:6379;slave:6380;slave:6381

# redis-6379.conf
daemonize yes
port 6379
dir ./
logfile "redis-6379.log"
dbfilename "dump-6379.rdb"
pidfile "/var/run/redis-6379.pid"

# redis-6380.conf
daemonize yes
port 6380
dir ./
logfile "redis-6380.log"
dbfilename "dump-6380.rdb"
pidfile "/var/run/redis-6380.pid"
slaveof 127.0.0.1 6379

# redis-6381.conf
daemonize yes
port 6381
dir ./
logfile "redis-6381.log"
dbfilename "dump-6381.rdb"
pidfile "/var/run/redis-6381.pid"
slaveof 127.0.0.1 6379

启动3个Redis服务:

[root@yqLinux2 conf]# redis-server redis-6379.conf 
[root@yqLinux2 conf]# redis-server redis-6380.conf 
[root@yqLinux2 conf]# redis-server redis-6381.conf 
[root@yqLinux2 conf]# ps -ef|grep redis-server | grep -v 'grep'
root      4814     1  0 23:55 ?        00:00:00 redis-server *:6379         
root      4818     1  0 23:55 ?        00:00:00 redis-server *:6380         
root      4822     1  0 23:55 ?        00:00:00 redis-server *:6381         
[root@yqLinux2 conf]# 

确认主从关系:

# 主节点视角
$ redis-cli -h 127.0.0.1 -p 6379 info replication
# Replication
role:master
490connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=281,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=281,lag=0
.................

# 从节点视角
$ redis-cli -h 127.0.0.1 -p 6380 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
.................

使用 slaveof 建立复制关系:

[root@yqLinux2 conf]# redis-cli -p 6380
127.0.0.1:6380> slaveof 127.0.0.1 6379
OK
127.0.0.1:6380> 
[root@yqLinux2 conf]# redis-cli -p 6381
127.0.0.1:6381> slaveof 127.0.0.1 6379
OK
127.0.0.1:6381> 
[root@yqLinux2 conf]# redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=43,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=43,lag=0
master_repl_offset:43
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:42
[root@yqLinux2 conf]# 

使用 config rewrite 目录重写配置文件:

[root@yqLinux2 conf]# redis-cli -p 6380 config rewrite
OK
[root@yqLinux2 conf]# redis-cli -p 6381 config rewrite
OK
[root@yqLinux2 conf]# cat redis-6380.conf 
daemonize yes
port 6380
dir "/root/conf"
logfile "redis-6380.log"
# Generated by CONFIG REWRITE
slaveof 127.0.0.1 6379
[root@yqLinux2 conf]# cat redis-6381.conf 
daemonize yes
port 6381
dir "/root/conf"
logfile "redis-6381.log"
# Generated by CONFIG REWRITE
slaveof 127.0.0.1 6379
[root@yqLinux2 conf]#

断开复制关系:

# 执行slaveof no one来断开与主节点复制关系。
[root@yqLinux2 conf]# redis-cli -p 6380 slaveof no one
OK

你可能感兴趣的:(Redis)