[root@localhost ~]# redis-cli # 启动master上的redis-cli客户端 127.0.0.1:6379> info replication # 查看192.168.81.100机器上Redis的信息 # Replication role:master # 角色为主节点 connected_slaves:1 # 连接一个从节点 slave0:ip=192.168.81.101,port=6379,state=online,offset=141,lag=2 # 从节点的信息 master_repl_offset:141 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:140 127.0.0.1:6379> set hello world # 向主节点写入数据 OK 127.0.0.1:6379> info server # Server redis_version:3.2.10 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:c8b45a0ec7dc67c6 redis_mode:standalone os:Linux 3.10.0-514.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll gcc_version:4.8.5 process_id:2529 run_id:7091f874c7c3eeadae873d3e6704e67637d8772b # 注意这个run_id tcp_port:6379 uptime_in_seconds:488 uptime_in_days:0 hz:10 lru_clock:12784741 executable:/usr/bin/redis-server config_file:/etc/redis.conf
第三步:回到192.168.81.101这台从节点上操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
127.0.0.1:6379> get hello # 获取'hello'的值,可以获取到 "world" 127.0.0.1:6379> set a b # 向192.168.81.101从节点写入数据,失败 (error) READONLY You can't write against a read only slave. 127.0.0.1:6379> slaveof no one # 取消从节点设置 OK 127.0.0.1:6379> info replication # 查看192.168.81.101机器,已经不再是从节点,而变成主节点了 # Replication role:master # 变成主节点了 connected_slaves:0 master_repl_offset:787 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379> dbsize # 查看192.168.81.101上Redis所有数据大小 (integer) 2
第四步:回到192.168.81.100虚拟机
1 2 3 4
127.0.0.1:6379> mset a b c d e f # 向192.168.81.100上的Redis集合中写入数据 OK 127.0.0.1:6379> dbsize # Redis中数据大小为5 (integer) 5
第五步:查看192.168.81.100虚拟机上Redis的日志
1 2 3 4 5 6 7 8 9 10 11
[root@localhost ~]# tail /var/log/redis/redis.log # 查看Redis最后10行日志 2529:M 14 Oct 17:55:09.448 * DB loaded from disk: 0.026 seconds 2529:M 14 Oct 17:55:09.448 * The server is now ready to accept connections on port 6379 2529:M 14 Oct 17:55:10.118 * Slave 192.168.81.101:6379 asks for synchronization 2529:M 14 Oct 17:55:10.118 * Partial resynchronization not accepted: Runid mismatch (Client asked for runid '9f93f85bce758b9c48e72d96a182a2966940cf52', my runid is '7091f874c7c3eeadae873d3e6704e67637d8772b') # 与192.168.81.100设备上通过info命令查看到的run_id相同 2529:M 14 Oct 17:55:10.118 * Starting BGSAVE for SYNC with target: disk # 执行BGSAVE命令成功 2529:M 14 Oct 17:55:10.119 * Background saving started by pid 2532 2532:C 14 Oct 17:55:10.158 * DB saved on disk 2532:C 14 Oct 17:55:10.159 * RDB: 12 MB of memory used by copy-on-write 2529:M 14 Oct 17:55:10.254 * Background saving terminated with success 2529:M 14 Oct 17:55:10.256 * Synchronization with slave 192.168.81.101:6379 succeeded # 向192.168.81.101同步数据成功
第六步:回到192.168.81.101虚拟机
1 2 3 4 5 6
127.0.0.1:6379> slaveof 192.168.81.100 6379 # 把192.168.81.101重新设置为192.168.81.100的从节点 OK 127.0.0.1:6379> dbsize (integer) 5 127.0.0.1:6379> mget a 1) "b"
第七步:查看192.168.81.101虚拟机上Redis的日志
1 2 3 4 5 6 7 8 9 10 11
[root@mysql ~]# tail /var/log/redis/redis.log # 查看Redis最后10行日志 2319:S 14 Oct 09:55:17.625 * MASTER <-> SLAVE sync started 2319:S 14 Oct 09:55:17.625 * Non blocking connect for SYNC fired the event. 2319:S 14 Oct 09:55:17.626 * Master replied to PING, replication can continue... 2319:S 14 Oct 09:55:17.626 * Trying a partial resynchronization (request 9f93f85bce758b9c48e72d96a182a2966940cf52:16). 2319:S 14 Oct 09:55:17.628 * Full resync from master: 7091f874c7c3eeadae873d3e6704e67637d8772b:1 # 从master节点全量复制数据 2319:S 14 Oct 09:55:17.629 * Discarding previously cached master state. 2319:S 14 Oct 09:55:17.763 * MASTER <-> SLAVE sync: receiving 366035 bytes from master # 显示从master同步的数据大小 2319:S 14 Oct 09:55:17.765 * MASTER <-> SLAVE sync: Flushing old data # slave清空原来的数据 2319:S 14 Oct 09:55:17.779 * MASTER <-> SLAVE sync: Loading DB in memory # 加载同步过来的RDB文件 2319:S 14 Oct 09:55:17.804 * MASTER <-> SLAVE sync: Finished with success
[root@localhost ~]# redis-cli info server |grep run_id run_id:7e366f6029d3525177392e98604ceb5195980518 [root@localhost ~]# redis-cli info |grep master_repl_offset master_repl_offset:0
查看192.168.91.100虚拟机上的run_id和偏移量
1 2 3 4
[root@mysql ~]# redis-cli info server | grep run_id run_id:7091f874c7c3eeadae873d3e6704e67637d8772b [root@mysql ~]# redis-cli info | grep master_repl_offset master_repl_offset:4483