遇到连接redis问题

Caused by: io.lettuce.core.RedisException: io.lettuce.core.RedisConnectionException:
	DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface.
	If you want to connect from external computers to Redis you may adopt one of the following solutions: 
1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 
3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 
4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside
  • 首先检查redis配置文件【redis.conf】

    1.redis.conf取消保护模式
    将 protected-mode yes 改为:protected-mode no (把yes改成no,是允许外网访问)

    protected-mode no 
    

    2.注释掉 #bin 127.0.0.1 (原因:bind 127.0.0.1生效,只能本机访问redis)
    3.【扩展】daemonize yes 改为 daemonize no;该错误跟个属性基本没有关系,只是为了让redis在后台运行

  • 检查防火墙是否关闭

systemctl stop firewalld             # 关闭防火墙
yum -y install iptables-services     # 安装 iptables 服务
systemctl enable iptables            # 设置 iptables 服务开机启动
systemctl start iptables             # 启动 iptables 服务
service iptables save                # 保存 iptables 配置
service iptables restart             # 重启 iptables 服务

要想远程访问,还得开放6379端口,查看6379防火墙状态

firewall-cmd --zone=public --query-port=6379/tcp
  • redis.conf配置文件和防火墙状态修改好,重启服务,注意,redis-server后面需要指定你的配置文件,不然不会生效的。如
[root@localhost trf]# redis-server /myredis/redis.conf
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐