Jedis 連線 Redis報JedisConnectionException: java.net.ConnectException: Connection refused

宜春發表於2020-09-29

總結一下Jedis 連線 Redis 常見錯誤

1、SocketTimeoutException 連線超時

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
    at redis.clients.jedis.Connection.connect(Connection.java:154)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:83)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:93)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:88)
    at redis.clients.jedis.Jedis.get(Jedis.java:97) 

這種情況先排除是否開啟了redis,或者ip,埠是否正確。如果都沒問題,那八成就是防火牆了。
可能是redis的6379埠無法訪問,請先在cmd中輸入命令
telnet 127.0.0.1 6379
如果不能訪問,需要在遠端機器關掉防火牆或者新增允許通過

CentOS,root使用者登陸,輸入如下命令開啟埠:

/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT

重新整理自動啟動儲存

/etc/rc.d/init.d/iptables save

2、Connection refused 拒絕訪問

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.conf配置檔案,使用vim命令開啟,找到bind那行修改後,wq儲存退出,重啟redis-server。

Jedis 連線 Redis報JedisConnectionException: java.net.ConnectException: Connection refused
redis.conf編輯後

編輯後如上圖,用#號註釋。

3、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.
    at redis.clients.jedis.Protocol.processError(Protocol.java:117)
    at redis.clients.jedis.Protocol.process(Protocol.java:142)
    at redis.clients.jedis.Protocol.read(Protocol.java:196)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
    at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:207)
    at redis.clients.jedis.Connection.getBulkReply(Connection.java:196)
    at redis.clients.jedis.Jedis.get(Jedis.java:98)
    at com.zyao89.jedis.JedisTest.test1(JedisTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

報錯資訊很長,但是主要是說redis開啟了protected mode,這也是Redis3.2加入的新特性,開啟保護模式的redis只允許本機登入,同樣設定在配置檔案redis.conf中,如圖

Jedis 連線 Redis報JedisConnectionException: java.net.ConnectException: Connection refused
redis.conf編輯後

這裡原來是yes代表開啟了保護模式,後面可以填密碼也可以填no代表關閉,我們這裡選擇關閉保護模式,wq儲存退出後再重啟redis-server。


作者:Zyao89;轉載請保留此行,謝謝;

個人部落格:https://zyao89.cn

相關文章