加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

一则Redis主从同步失败案例

(2015-05-19 22:22:05)
分类: Redis
今天在配置Redis主从的时候,info信息的 # Replication 项,总是显示同步不成功:
# Replication
role:slave
master_host:XX.XX.XX.XX
master_port:6349
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:1

master_link_status的状态一直是down,master_sync_in_progress的值一直显示为1.
值为1表示从服务器一直在与主服务器进行同步.

一开始怀疑是主服务器数据量太大了,所以同步的时间比较长。但是等了好久,从服务器的同步状态还是显示
master_link_status:down
master_sync_in_progress:1

这就奇怪了,难道主服务器真的有那么大的数据量吗 ?

带着这个疑问,我登陆上主服务器查看数据量大小:
KEYS *
.........
631019) "BINDTASK14174499"

主服务器总共有631019个key,这也不是很大数据量啊 !!!

info查看Redis使用的内存:
used_memory:1673093192
used_memory_human:1.56G
used_memory_rss:1739427840
used_memory_peak:4113859368
used_memory_peak_human:3.83G

主服务器的Redis占用的内存才1个多G,内存使用的高峰是3.83G。于是怀疑是否从服务器的内存设置过小了 ??

登陆从服务器查看设置的内存:
127.0.0.1:6380> config get maxmemory
1) "maxmemory"
2) "4000000000"

从服务器设置的内存值为4G,这样子的内存,肯定是足够从服务器将数据载入到内存中的。。。

在百思不得其解的时候,想到可以查看redis的日志:
[2351] 19 May 22:07:01.936 * The server is now ready to accept connections on port 6380
[2351] 19 May 22:07:01.937 * Connecting to MASTER 183.61.143.107:6349
[2351] 19 May 22:07:01.937 * MASTER <-> SLAVE sync started
[2351] 19 May 22:07:01.939 * Non blocking connect for SYNC fired the event.
[2351] 19 May 22:07:01.941 * Master replied to PING, replication can continue...
[2351] 19 May 22:07:01.943 * (Non critical) Master does not understand REPLCONF listening-port: -ERR unknown command 'REPLCONF'
[2351] 19 May 22:07:01.943 * Partial resynchronization not possible (no cached master)
[2351] 19 May 22:07:01.944 * Master does not support PSYNC or is in error state (reply: -ERR unknown command 'PSYNC')
[2351] 19 May 22:07:01.944 * Retrying with SYNC...
[2351] 19 May 22:07:14.084 * MASTER <-> SLAVE sync: receiving 510459928 bytes from master
[2351] 19 May 22:07:30.751 * MASTER <-> SLAVE sync: Flushing old data
[2351] 19 May 22:07:30.751 * MASTER <-> SLAVE sync: Loading DB in memory
[2351] 19 May 22:07:46.572 * MASTER <-> SLAVE sync: Finished with success
[2351] 19 May 22:07:48.712 * Connecting to MASTER 183.61.143.107:6349
[2351] 19 May 22:07:48.712 * MASTER <-> SLAVE sync started
[2351] 19 May 22:07:48.713 * Non blocking connect for SYNC fired the event.

看日志,其中有一个比较重要的线索:
[2351] 19 May 22:07:01.943 * (Non critical) Master does not understand REPLCONF listening-port: -ERR unknown command 'REPLCONF'

[2351] 19 May 22:07:01.944 * Master does not support PSYNC or is in error state (reply: -ERR unknown command 'PSYNC')

第一条日志内容提示:Mater不支持REPLCONF listening-port 指令。为啥会不支持呢?难道是主从之间版本不一致??
第二条日志内容提示:Master不支持半同步PSYNC功能.


于是马上登陆到Master,info查看redis版本:
redis 127.0.0.1:6349> info
redis_version:2.4.4

对比从服务器的redis版本:
127.0.0.1:6380> info
# Server
redis_version:2.8.3

主从之间果然是版本不一致。Mater的版本是2.4,Slave的版本是2.8 .主库的版本低于从库版本!!!

是否真的不支持REPLCONF指令呢?我们模拟一个主从同步的过程,来测试一下:
telnet 183.61.143.107 6349
Trying 183.61.143.107...
Connected to 183.61.143.107.
Escape character is '^]'.
ping
+PONG
REPLCONF listening-port 6380
-ERR unknown command 'REPLCONF'

错误提示,不支持REPLCONF指令  !!!  如果主从版本一致,并且都是2.8,这个REPLCONF指令一定是支持的,是经过测试的。

由此就可以解释,为啥主从之间同步一直不成功了http://www/uc/myshow/blog/misc/gif/E___7394ZH00SIGG.gif

至于这个模拟主从同步的过程,请见我的一下解释:
===========================================================

由于Redis使用TCP协议通信,所以我们可以使用telnet工具伪装成一个从数据库来了解同步的具体过程。

root@ubuntu:~# telnet 127.0.0.1 6379

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

 

然后作为从数据库,我们先要发送PING命令确认主数据库是否可以连接:

ping

+PONG

 主数据库会回复+PONG。如果没有收到主数据库的回复,则会向用户提示错误。如果主数据库需要密码才能连接,我们还得发送AUTH命令进行验证。然后向主数据库发送replconf命令说明自己的端口号:

REPLCONF listening-port 6380

+OK

 此时可以开始同步的过程了:从数据库向主数据库发送SYNC命令开始进行同步,此时主数据库会发送会快照文件和缓存的命令。

SYNC

$760

REDIS0006myzseylistmylistalpha  ACBdecanumbersring

scoreboard!Nicholas

=====================================================

总结:如果要配置redis主从同步,最好是要确保主从之间的redis版本是一致的,否则很可能会出现类似于上述所说的错误http://www/uc/myshow/blog/misc/gif/E___6719EN00SIGG.gif






0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有