使用Jedis 连接 Redis 时可能出现的常见错误

本人作为一个初学者学习Redis的使用,遇到了下面一些异常,总结了一些解决方案。

1、SocketTimeoutException 连接超时

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out

这种情况先排除是否开启了redis,或者ip地址和端口是否书写正确。如果没有以上问题,那必定是防火墙未允许Redis的默认端口 6379。

编辑防火墙配置文件 :vim /etc/sysconfig/iptables

添加红框标注语句,保存并退出。重启防火墙:service iptables restart


2、Connection refused 拒绝访问

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect

修改redis.conf配置文件,使用vim命令打开。


找到bind那行用#号注释,保存并退出,重启redis-server。


3、JedisDataException:DENIED Redis is running in protected mode 保护模式

redis.clients.jedis.exceptions.JedisDataException: 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开启了protected mode,这也是Redis3.2加入的新特性,开启保护模式的redis只允许本机登录,同样在配置文件redis.conf中,如下图


这里原来默认是yes,代表开启了保护模式,后面可以填密码也可以填no代表关闭,我们这里选择关闭保护模式,保存退出后再重启redis-server。

以上都是本人亲测解决方案,借鉴了https://www.jianshu.com/p/2fa4622fc1d3,欢迎指点。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值