1. 程式人生 > >java.net.ConnectException: Connection refused: connect(無法連線redis伺服器)

java.net.ConnectException: Connection refused: connect(無法連線redis伺服器)

提示:出現問題版本為redis-3.2.1.tar.gz

使用其他版本不需要做如下配置,idea、eclipse均連線正常,如:

  • redis-3.0.0.tar.gz
  • redis-4.0.11.tar.gz
redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
	at redis.clients.jedis.Connection.connect(Connection.java:164)
	at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
	at redis.clients.jedis.Connection.sendCommand(Connection.java:100)
	at redis.clients.jedis.BinaryClient.set(BinaryClient.java:97)
	at redis.clients.jedis.Client.set(Client.java:32)
	at redis.clients.jedis.Jedis.set(Jedis.java:68)
	at com.the.one.jedis.MyJedis.test1(MyJedis.java:14)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
	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:538)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
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:158)
	... 29 more

這個問題是由於Redis是在守護狀態下執行的,如果不是本地連線,就會報錯,解決方法在問題描述中已經給出:

1、檢查在linux的redis是否開啟:ps -ef | grep redis
錯誤的狀態 root      28465      1  0 06:36 ?        00:00:00 ./redis-server 127.0.0.1:6379    
正確的狀態 root      28465      1  0 06:36 ?        00:00:00 ./redis-server *:6379 

2、在linux上連線jedis的連結是否成功  ./redis-cli -h 192.168.66.101 -p 6379

3、修改配置檔案redis.conf,並重新啟動redis服務

  1. 註釋 bind 127.0.0.1
  2. protected-mode yes 改為 no
  3. 重啟:先shutdown,後exit,然後重新執行./redis.server -redis.conf

4.以上都沒問題,還是連線不成功,檢視liunx的防火牆是否關閉
輸入:service iptables status
關閉防火牆:service iptables stop