把 bind 改成 0.0.0.0 之后远程还连不上,先在本机确认端口到底监听在哪。
ss -lntp | grep 6379
显示 127.0.0.1:6379 说明配置没生效,配置改了没重启或者改错了文件(多实例常有 6379.conf、6380.conf 好几份)。
protected-mode 是第二道门
Redis 3.2 之后默认 protected-mode yes,在没有密码、没有 bind 指定地址的情况下,只接受本机连接,远程直接拒。
protected-mode yes bind 0.0.0.0 requirepass 你的强密码
设了密码,protected-mode 就允许远程连。内网环境图省事把 protected-mode 设 no 也行,但公网机器绝对不能这么干。
第三道门是防火墙
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" port port="6379" protocol="tcp" accept' firewall-cmd --reload
只放应用网段,别 --add-port 全网开放。
连上之后又报 NOAUTH
这是密码没带,不是网络问题。redis-cli -h 10.0.1.20 -a 密码 再试。