前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jedis连接失败 Could not get a resource from the pool] with root cause

Jedis连接失败 Could not get a resource from the pool] with root cause

作者头像
traffic
发布2020-04-09 15:19:10
1.9K0
发布2020-04-09 15:19:10
举报
文章被收录于专栏:大龄码农大龄码农

网上差了很多例子,都是说连接池个数问题,或者是超时问题,但是始终并没有解决问题。

后来看了一片文章,提到有可能是linux防火墙问题造成的,果断测试一下

所以,先查看一下防火墙的位置,如图所示:

以前都是用iptables,但是centos7.0之后就换成了firewalled了,那么我查看了firewalled的状态为active(running),很明显是在运行中的了,我尝试关闭一下防火墙,看看是不是防火墙影响的我无法访问redis,如图:

关闭之后,测试一下程序,如图:

Exception in thread "main" 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.
    at redis.clients.jedis.Protocol.processError(Protocol.java:127)
    at redis.clients.jedis.Protocol.process(Protocol.java:161)
    at redis.clients.jedis.Protocol.read(Protocol.java:215)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:196)
    at com.example.redis.JedisTest.main(JedisTest.java:24)

结果又报错,我的天啊,不过仔细看看发现它提供了4个解决方案,所以至少可以得出结论,就是linux的防火墙影响的redis的连接,所以,我要重新开启防火墙,在开启之后,增加这个端口号可以外网访问,首先,先进行开启防火墙的操作,如图:

再次检查一下防火墙的状态,如图:

这样说明已经正常开启了防火墙,下面需要开启6379端口,如图:

success说明添加成功了,下面需要重新启动防火墙,如图:

防火墙的问题解决了,之后就是看看在这4个解决方案中去哪一种方案了。

1.修改redis.conf配置文件,将绑定的ip地址端口号给注释见下图

这里写图片描述
这里写图片描述

  2.由于Linux上的redis处于安全保护模式,这就让你无法从虚拟机外部去轻松建立连接,这里就有两种解决方法,一种是在redis.conf中设置保护模式为no,见下图

这里写图片描述
这里写图片描述

  3.另外一种方式是加上安全认证,即redis默认是没有密码的可以直接登录,这里加上密码,配置见下图

这里写图片描述
这里写图片描述

  4.好吧,此时你以为轻松解决问题了,于是再次在Linux启动redis服务器后,在外部连接还是报错,真是too young too native,起初我以为是配置没有生效,因为明明配置都写好了,于是杀掉redis的相关进程重启服务,结果发现还是没卵用。最后研究发现问题所在:之前启动redis-server并没有指定配置文件,而Linux上的redis比较操蛋的一点就是如果你不指定配置文件去启动,那么你做的修改就没有用,会读取默认配置(PS:至于这个默认配置在哪我也不清楚),于是用下面这种方式启动就可以使修改的配置文件生效,至于config rewrite命令我测试了并没有什么卵用。

这里写图片描述
这里写图片描述

  5.再次测试,在windows上进行jedis连接测试,出现下面pong(),恼人的错误终于消失了,比起windows版本的redis还真是让人龟毛啊,如果配置了密码认证,调用Jedis的auth方法输入密码即可

总结一下:

1.关闭LINUX上的防火墙

2.修改redis配置文件中的本地绑定(注释掉bind 127.0.0.1)

3.提供验证密码

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档