Redis主从同步
主从同步原理
1. 从服务器向主服务器发送 SYNC 命令。
2. 接到 SYNC 命令的主服务器会调用BGSAVE 命令,创建一个 RDB 文件,并使用缓冲区记录接下来执行的所有写命令。
3. 当主服务器执行完 BGSAVE 命令时,它会向从服务器发送 RDB 文件,而从服务器则会接收并载入这个文件。
4. 主服务器将缓冲区储存的所有写命令发送给从服务器执行。
修改配置文件
vim redis-6379.conf port 6379 daemonize yes pidfile /data/6379/redis.pid loglevel notice logfile "/data/6379/redis.log" protected-mode yes # RDB配置 dir /data/6379 dbfilename dbmp.rdb save 900 1 save 300 10 save 60 10000 # AOF配置 #appendonly yes #appendfsync everysec
vim redis-6380.conf port 6380 daemonize yes pidfile /data/6380/redis.pid loglevel notice logfile "/data/6380/redis.log" protected-mode yes # RDB配置 dir /data/6380 dbfilename dbmp.rdb save 900 1 save 300 10 save 60 10000 # 主从同步 slaveof 127.0.0.1 6379 # AOF配置 #appendonly yes #appendfsync everysec
vim redis-6381.conf port 6381 daemonize yes pidfile /data/6381/redis.pid loglevel notice logfile "/data/6381/redis.log" protected-mode yes # RDB配置 dir /data/6381 dbfilename dbmp.rdb save 900 1 save 300 10 save 60 10000 # 主从同步 slaveof 127.0.0.1 6379 # AOF配置 #appendonly yes #appendfsync everysec
启动三个实例
[root@localhost redis-4.0.12]# redis-server redis-6379.conf [root@localhost redis-4.0.12]# redis-server redis-6380.conf [root@localhost redis-4.0.12]# redis-server redis-6381.conf
查看主从状态
# 6379 [root@localhost ~]# redis-cli -p 6379 info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=196,lag=1 slave1:ip=127.0.0.1,port=6381,state=online,offset=196,lag=0 master_replid:0f3b54ebd09f5cc585c33c9872f005f1437d25e1 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:196 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:196
# 6380 [root@localhost ~]# redis-cli -p 6380 info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:210 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:0f3b54ebd09f5cc585c33c9872f005f1437d25e1 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:210 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:210
# 6381 [root@localhost ~]# redis-cli -p 6381 info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:224 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:0f3b54ebd09f5cc585c33c9872f005f1437d25e1 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:224 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:224
方式二(不修改配置文件,重启服务后失效):
操作
127.0.0.1:6381> slaveof 127.0.0.1 6379 127.0.0.1:6380> slaveof 127.0.0.1 6379
查看状态
[root@localhost ~]# redis-cli -p 6379 127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_replid:d0fff3d6587307c4d3072cc92a6e16bdd91e291d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
[root@localhost redis-4.0.12]# redis-cli -p 6380 127.0.0.1:6380> info replication # Replication role:master connected_slaves:0 master_replid:a9960a82a571793eae37e322143a3ef1caee75e5 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
127.0.0.1:6381> info replication # Replication role:master connected_slaves:0 master_replid:fd7b3f7a0c98a0dcd2fb3d4a7a8b7fde6e09238b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0