Redis opens the remote login connection

  • 2020-05-24 06:26:57
  • OfStack

Today, when using jedis client api to connect the remote connection redis, 1 directly reported an error, as follows:


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.Connection.sendCommand(Connection.java:91)
  at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:551)
  at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2047)
  at sy.test.TestJedis.setUp(TestJedis.java:18)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
  at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
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:339)
  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  at java.net.Socket.connect(Socket.java:579)
  at redis.clients.jedis.Connection.connect(Connection.java:158)
  ... 30 more 

By default, redis can only log in localhost, so you need to enable remote login. The solution is as follows:

In redis's configuration file redis.conf, find bind localhost and comment it out.

Comment out the native so that all computers on the LAN can access it.

band localhost can only be accessed natively, not by computers on the LAN.

bind LAN IP can only be accessed by IP machines within the LAN, local localhost cannot be accessed.

Verification method:


  [root@mch ~]# ps -ef | grep redis
  root   2175   1 0 08:15 ?    00:00:05 /usr/local/bin/redis-server *:6379 

/ usr local/bin/redis - server * : in 6379 through the "*" can be seen at this point is to allow all ip connection login to the machine redis service.

Matters needing attention:

When setting up remote access again today, when starting Redis, error: Creating Server TCP listening socket *:6379: unable to bind socket (Redis1 must not be set to run in the background, otherwise the terminal will not have any error display).

Some said bug version of Redis (I used version 3.2.0). I solved this problem by referring to the following articles:

https://github.com/antirez/redis/issues/3241

http://stackoverflow.com/questions/8537254/redis-connect-to-remote-server

Instead of commenting out bind 127.0.0.1, I changed bind 127.0.0.1 to bind 0.0.0.0.


Related articles: