Java 串連操作 Redis 出現錯誤

來源:互聯網
上載者:User

標籤:資訊   testing   requested   --   使用   登入   south   alter   abstract   

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,添加所以一行 

 

2)輸入命令service iptables restart重啟防火牆

或者可以直接root使用者使用命令service iptables stop關閉防火牆。

防火牆檢查完後,如果還是出現上述問題,說明redis還有地方需要配置,redis預設是只有本機可以訪問的,想要遠端存取需要修改redis.conf設定檔。 
進入redis.conf目錄,並使用vi命令開啟,找到bind那行修改後,wq儲存退出,重啟redis-server。 

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) 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.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中, 
 
這裡原來是yes代表開啟了保護模式,後面可以填密碼也可以填no代表關閉,我們這裡選擇關閉保護模式,wq儲存退出後再重啟redis-server

 

 

 轉載:

使用Java遠端連線操作Redis - Roy_70的專欄 - 部落格頻道 - CSDN.NET
http://blog.csdn.net/roy_70/article/details/54583686

 

Java 串連操作 Redis 出現錯誤

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.