Hi!请登陆

redis主从同步

2020-10-27 63 10/27

首先在新服务器上直接进入redis-cli,执行从库配置slaveof 192.168.1.100 6379,这里假设要将192.168.1.100的6379端口的redis服务转移过来.这样就已经开始同步了.通过info可以查看当前服务器是slave.
然后通过info命令查看master_link_status,如果为up,表示同步完成.(在同步过程中,执行查询的时候还是会提示"Redis is loading the dataset in memory",这属于正常情况.把数据从磁盘文件加载到内存中可能会消耗很长的一段时间.)
最后断开主从关系,在redis-cli命令行下执行slaveof no one提示OK,再通过info查看,该新服务器已经自己变成master了.
确保旧的服务器的服务已经停止服务.
上面没有配合说明业务代码对服务器的请求切换,但是我们在切换完服务之后肯定需要转移业务代码的请求吧.那么如何确定服务已经完全转移走了呢?
在旧服务器上通过netstat命令查看是否还有请求过来.

netstat -an|grep "100:6379"|wc -l

有时候netstat的结果也不一定准,因为有些请求已经不在,但是socket状态还在,比如CLOSE_WAIT状态最长可持续2小时.同时socket请求太快,也会出现netstat没数据,但是实际网卡有流量的情况.
我们可以通过tcdump监控网卡在该端口上确实已经没有流量.

tcpdump  -i em2 -vv -nn host  192.168.1.100 and  port 6379

注意排除监控系统对该redis实例的请求.
如何判断 redis 已经同步完毕呢?
在命令行查看:

info master_link_status:up

则表示同步完成了.
日志文件也可以看:

[48864] 12 Jun 11:24:03.549 * The server is now ready to accept connections on port 6310
[48864] 12 Jun 11:31:52.936 * SLAVE OF 192.168.50.17:8004 enabled (user request)
[48864] 12 Jun 11:31:53.467 * Connecting to MASTER 192.168.50.17:8004
[48864] 12 Jun 11:31:53.467 * MASTER <-> SLAVE sync started
[48864] 12 Jun 11:31:53.470 * Non blocking connect for SYNC fired the event.
[48864] 12 Jun 11:31:53.470 * Master replied to PING, replication can continue...
[48864] 12 Jun 11:31:53.470 * Partial resynchronization not possible (no cached master)
[48864] 12 Jun 11:31:53.470 * Master does not support PSYNC or is in error state (reply: -ERR unknown command 'PSYNC')
[48864] 12 Jun 11:31:53.470 * Retrying with SYNC...
[48864] 12 Jun 11:32:13.085 * MASTER <-> SLAVE sync: receiving 484322714 bytes from master
[48864] 12 Jun 11:32:18.498 * MASTER <-> SLAVE sync: Flushing old data
[48864] 12 Jun 11:32:18.498 * MASTER <-> SLAVE sync: Loading DB in memory
[48864] 12 Jun 11:32:32.349 * MASTER <-> SLAVE sync: Finished with succes
Tag:

相关推荐