redis主从连接不成功错误
安装完redis后,设置主从,在从服务器上运行
127.0.0.1:6379> slaveof 192.168.159.131 6379
slaveof <主服务器的ip> <主服务器redis的端口>
然后回到主服务器上,运行info replication
显示如下:
127.0.0.1:6379> info replication # replication role:master 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
问题出现了
从没有连接上来。
查看主redis的日志文件
[1922] 22 mar 22:04:24.971 * rdb: 0 mb of memory used by copy-on-write
[1544] 22 mar 22:04:25.008 * background saving terminated with success
[1544] 22 mar 22:27:23.567 * background saving started by pid 2079
[2079] 22 mar 22:27:23.574 * db saved on disk
[2079] 22 mar 22:27:23.575 * rdb: 0 mb of memory used by copy-on-write
[1544] 22 mar 22:27:23.604 * background saving terminated with success
查看从服务器日志
有如下错误:
error condition on socket for sync: no route to host
然后telnet 主的端口竟然不通,发现主服务器的iptables开着,关掉后就可以和主服务器的端口通了(生产环境添加iptables规则)。
但是发现还是从不能连上主,再看从的日志时
变成如下内容:
[4399] 22 mar 14:41:56.311 # master aborted replication with an error: noauth authentication required.
[4399] 22 mar 14:41:57.313 * connecting to master 192.168.159.131:6379
[4399] 22 mar 14:41:57.313 * master <-> slave sync started
[4399] 22 mar 14:41:57.313 * non blocking connect for sync fired the event.
[4399] 22 mar 14:41:57.314 * master replied to ping, replication can continue...
[4399] 22 mar 14:41:57.315 * (non critical) master does not understand replconf listening-port: -noauth authentication required.
[4399] 22 mar 14:41:57.315 * partial resynchronization not possible (no cached master)
[4399] 22 mar 14:41:57.316 # unexpected reply to psync from master: -noauth authentication required.
[4399] 22 mar 14:41:57.317 * retrying with sync...
[4399] 22 mar 14:41:57.319 # master aborted replication with an error: noauth authentication required.
网上查找有说是版本不一致造成的,但是我的版本都是一样的。
最后找到了解决方法
原来是主从都开启了密码requirepass,导致不能验证成功
在redis从服务器的配置文件里面添加如下内容:
masterauth 123456
(123456是我的主redis的密码,此参数是当与主连接时的密码验证)
总结
这样就解决了主从连接不成功问题~
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论