安装完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的密码,此参数是当与主连接时的密码验证)
这样就解决了主从连接不成功问题