Redis主从复制简单配置教程详解

作者:袖梨 2022-06-29

主从复制

192.168.1.106 master机器
mkdir -p /data/redis
cp /etc/redis/redis.conf /etc/redis/redis-6379.conf
vim /etc/redis/redis-6379.conf


daemonize yes
pidfile /var/run/redis-6379.pid
port 6379
logfile "6379.log"
dbfilename "dump-6379.rdb"
dir "/data/redis"
appendonly yes
appendfilename "appendonly-6379.aof"
 
192.168.1.107 slave机器
mkdir -p /data/redis
cp /etc/redis/redis.conf /etc/redis/redis-6381.conf
vim /etc/redis/redis-6381.conf


daemonize yes
pidfile /var/run/redis-6381.pid
port 6381
logfile "6381.log"
dbfilename "dump-6381.rdb"
dir "/data/redis"
appendonly yes
appendfilename "appendonly-6381.aof"
 
连从服务器


[root@Slave redis]# redis-cli -p 6381
127.0.0.1:6381>
127.0.0.1:6381> dbsize
(integer) 0
127.0.0.1:6381> slaveof 192.168.1.106 6379
127.0.0.1:6381> get hello
"world"
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:192.168.1.106
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:773
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
 
[root@Slave redis]# redis-cli -p 6381
127.0.0.1:6381> slaveof 192.168.1.106 6379
127.0.0.1:6381> config rewrite

再配置一个从服务器


[root@Slave redis]# cd /etc/redis/
[root@Slave redis]# ls
redis-6379.conf  redis-6381.conf
[root@Slave redis]# sed 's/6381/6382/g' redis-6381.conf > redis-6382.conf
[root@Slave redis]# redis-server /etc/redis/redis-6382.conf
[root@Slave redis]# redis-cli -p 6382
127.0.0.1:6382> get hello
"world"
127.0.0.1:6382> info replication
# Replication
role:slave
master_host:192.168.1.106
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:3026
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
127.0.0.1:6382>
 

相关文章

精彩推荐