问题描述:搭建redis主从复制集群环境时,当从库执行slaveof命令时报错“Error condition on socket for SYNC: No route to host”,如下所示:
操作系统:rhel 7.9 64位
数据库:redis 6.2.6
主机名:主库leo-redis626-a,从库 leo-redis626-b.
1、异常重现
[root@leo-redis626-b redis-6.2.6]# pwd
/usr/local/src/redis-6.2.6
[root@leo-redis626-b redis-6.2.6]# redis-server redis6380.conf
[root@leo-redis626-b redis-6.2.6]# redis-cli -p 6380
127.0.0.1:6380> slaveof 192.168.133.100 6379
OK
告警日志如下:
3155:S 03 Apr 2023 09:14:08.923 * Before turning into a replica, using my own master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
3155:S 03 Apr 2023 09:14:08.923 * Connecting to MASTER 192.168.133.100:6379
3155:S 03 Apr 2023 09:14:08.924 * MASTER <-> REPLICA sync started
3155:S 03 Apr 2023 09:14:08.924 * REPLICAOF 192.168.133.100:6379 enabled (user request from 'id=3 addr=127.0.0.1:54926 laddr=127.0.0.1:6380 fd=8 name= age=56 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=49 qbuf-free=40905 argv-mem=26 obl=0 oll=0 omem=0 tot-mem=61490 events=r cmd=slaveof user=default redir=-1')
3155:S 03 Apr 2023 09:14:08.924 # Error condition on socket for SYNC: No route to host
3155:S 03 Apr 2023 09:14:09.177 * Connecting to MASTER 192.168.133.100:6379
3155:S 03 Apr 2023 09:14:09.178 * MASTER <-> REPLICA sync started
3155:S 03 Apr 2023 09:14:09.181 # Error condition on socket for SYNC: No route to host
3155:S 03 Apr 2023 09:14:10.254 * Connecting to MASTER 192.168.133.100:6379
3155:S 03 Apr 2023 09:14:10.255 * MASTER <-> REPLICA sync started
3155:S 03 Apr 2023 09:14:10.255 # Error condition on socket for SYNC: No route to host
3155:S 03 Apr 2023 09:14:11.360 * Connecting to MASTER 192.168.133.100:6379
3155:S 03 Apr 2023 09:14:11.360 * MASTER <-> REPLICA sync started
3155:S 03 Apr 2023 09:14:11.361 # Error condition on socket for SYNC: No route to host
3155:S 03 Apr 2023 09:14:12.450 * Connecting to MASTER 192.168.133.100:6379
3155:S 03 Apr 2023 09:14:12.450 * MASTER <-> REPLICA sync started
3155:S 03 Apr 2023 09:14:12.451 # Error condition on socket for SYNC: No route to host
2、解决过程
2.1、关闭防火墙
--关闭主库防火墙。
[root@leo-redis626-a redis-6.2.6]# systemctl stop firewalld
[root@leo-redis626-a redis-6.2.6]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
--关闭从库防火墙
[root@leo-redis626-b redis-6.2.6]# systemctl stop firewalld
[root@leo-redis626-b redis-6.2.6]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
2.2、修改主库配置文件
[root@leo-redis626-a redis-6.2.6]# pwd
/usr/local/src/redis-6.2.6
[root@leo-redis626-a redis-6.2.6]# vi redis6379.conf
include /usr/local/src/redis-6.2.6/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb
daemonize yes
bind 0.0.0.0
说明:向redis6379.conf添加“bind 0.0.0.0”。
2.3、重启redis服务
--重启主库redis进程。
[root@leo-redis626-a redis-6.2.6]# ps -ef|grep -i redis
avahi 831 1 0 08:25 ? 00:00:00 avahi-daemon: running [leo-redis626-a.local]
root 2790 1 0 08:46 ? 00:00:04 redis-server 127.0.0.1:6379
root 4876 2308 0 09:20 pts/0 00:00:00 grep --color=auto -i redis
[root@leo-redis626-a redis-6.2.6]# kill -9 2790
[root@leo-redis626-a redis-6.2.6]# redis-server redis6379.conf
--重启从库redis进程。
[root@leo-redis626-b redis-6.2.6]# redis-server redis6380.conf
[root@leo-redis626-b redis-6.2.6]# ps -ef|grep redis
avahi 829 1 0 09:32 ? 00:00:00 avahi-daemon: running [leo-redis626-b.local]
root 2304 2263 0 09:34 pts/1 00:00:00 tail -5000f redis.log
root 2390 1 0 09:41 ? 00:00:00 redis-server 127.0.0.1:6380
root 2396 2194 0 09:41 pts/0 00:00:00 grep --color=auto redis
3、结果验证
--从库开启主从复制
[root@leo-redis626-b redis-6.2.6]# redis-cli -p 6380
127.0.0.1:6380> slaveof 192.168.133.100 6379
OK
--主库验证
[root@leo-redis626-a redis-6.2.6]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.133.101,port=6380,state=online,offset=56,lag=0
master_failover_state:no-failover
master_replid:42036cfd230f20054ec04042c0f001c3b083c3ce
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56
--主库添加键值
127.0.0.1:6379> set key1 val1
OK
--从库验证键值
127.0.0.1:6380> keys *
1) "key1"
127.0.0.1:6380> get key1
"val1"
说明:如上所示,从库告警日志中不在报错,且主库info replication命令显示正常.
标签:主从复制,slaveof,No,09,redis,00,leo,6.2,root
From: https://blog.51cto.com/u_12991611/6167480