设为首页 收藏本站
查看: 1183|回复: 0

[经验分享] Java 连接操作 Redis 出现错误

[复制链接]

尚未签到

发表于 2017-12-21 11:40:00 | 显示全部楼层 |阅读模式
  

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect  
at redis.clients.jedis.Connection.connect(Connection.java:
207)  
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:
93)  
at redis.clients.jedis.Connection.sendCommand(Connection.java:
126)  
at redis.clients.jedis.Connection.sendCommand(Connection.java:
121)  
at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:
106)  
at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:
195)  
at practice.RedisJava.main(RedisJava.java:
13)  
Caused by: java.net.ConnectException: Connection refused: connect
  
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
  
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:
85)  
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:
350)  
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:
206)  
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:
188)  
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:
172)  
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:
392)  
at java.net.Socket.connect(Socket.java:
589)  
at redis.clients.jedis.Connection.connect(Connection.java:
184)  
...
6 more  

  这可能是两个原因造成的,首先可能是redis的6379端口无法访问,请先在cmd中输入命令
  telnet 127.0.0.1 6379
  看看可不可以访问redis-server 机器的6379端口,如果不能访问,需要在远程机器关掉防火墙或者添加允许通过
  1)使用root用户登录,vi /etc/sysconfig/iptables,添加如图所以一行
DSC0000.png

  2)输入命令service iptables restart重启防火墙
  或者可以直接root用户使用命令service iptables stop关闭防火墙。
  防火墙检查完后,如果还是出现上述问题,说明redis还有地方需要配置,redis默认是只有本机可以访问的,想要远程访问需要修改redis.conf配置文件。
  进入redis.conf目录,并使用vi命令打开,找到bind那行修改后,wq保存退出,重启redis-server。
DSC0001.png

  bind 后加的是允许访问的ip
  bind 127.0.0.1代表只有本机可以访问,可以将允许访问的ip加入,也可以直接注释掉这一行,这样所有机器都可以访问。

解决上述问题后出现的新问题:DENIED Redis is running in protected mode
  报错信息如下:
  

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)>
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.Jedis.set(Jedis.java:
121)  
at roy.redis.test.Test.init(Test.java:
13)  
at roy.redis.test.Test.main(Test.java:
8)  

  报错信息很长,但是主要是说redis开启了protected mode,这也是Redis3.2加入的新特性,开启保护模式的redis只允许本机登录,同样设置在配置文件redis.conf中,如图
DSC0002.jpg

  这里原来是yes代表开启了保护模式,后面可以填密码也可以填no代表关闭,我们这里选择关闭保护模式,wq保存退出后再重启redis-server
  转载:
  使用Java远程连接操作Redis - Roy_70的专栏 - 博客频道 - CSDN.NET
  http://blog.csdn.net/roy_70/article/details/54583686

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426398-1-1.html 上篇帖子: Redis单机配置多实例,实现主从同步 下篇帖子: Python操作redis系列之 列表(list) (四)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表