<p>今天使用jedis客户端api连接远程连接redis的时候,一直报错,如下: </p><p>redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect</p><p> at redis.clients.jedis.Connection.connect(Connection.java:164)</p><p> at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)</p><p> at redis.clients.jedis.Connection.sendCommand(Connection.java:100)</p><p> at redis.clients.jedis.Connection.sendCommand(Connection.java:91)</p><p> at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:551)</p><p> at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2047)</p><p> at sy.test.TestJedis.setUp(TestJedis.java:18)</p><p> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)</p><p> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)</p><p> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)</p><p> at java.lang.reflect.Method.invoke(Method.java:606)</p><p> at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)</p><p> at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)</p><p> at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)</p><p> at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)</p><p> at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)</p><p> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)</p><p> at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)</p><p> at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)</p><p> at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)</p><p> at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)</p><p> at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)</p><p> at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)</p><p> at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)</p><p> at org.junit.runners.ParentRunner.run(ParentRunner.java:309)</p><p> at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)</p><p> at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)</p><p> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)</p><p> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)</p><p> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)</p><p> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)</p><p>Caused by: java.net.ConnectException: Connection refused: connect</p><p> at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)</p><p> at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)</p><p> at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)</p><p> at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)</p><p> at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)</p><p> at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)</p><p> at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)</p><p> at java.net.Socket.connect(Socket.java:579)</p><p> at redis.clients.jedis.Connection.connect(Connection.java:158)</p><p> ... 30 more</p><p>  原来是redis默认只能localhost登录,所以需要开启远程登录。解决方法如下:</p><p>  在redis的配置文件redis.conf中,找到bind localhost注释掉。</p><p>    注释掉本机,局域网内的所有计算机都能访问。</p><p>    band localhost 只能本机访问,局域网内计算机不能访问。</p><p>    bind 局域网IP 只能局域网内IP的机器访问, 本地localhost都无法访问。</p><p>  验证方法:</p><p>  [root@mch ~]# ps -ef | grep redis</p><p>  root 2175 1 0 08:15 ? 00:00:05 /usr/local/bin/redis-server *:6379</p><p>  /usr/local/bin/redis-server *:6379 中通过&quot;*&quot;就可以看出此时是允许所有的ip连接登录到这台redis服务上。</p><p>  注意事项:</p><p>  今天再设置远程访问的时候,在启动Redis的时候报错:Creating Server TCP listening socket *:6379: unable to bind socket(Redis一定不能设置成后台运行,否则终端不会有任何错误显示)。</p><p>  上网搜索说各种原因的都有,有的说是Redis版本的bug(我用的版本是3.2.0),我是参考以下几篇文章解决了这个问题:</p><p>  https://github.com/antirez/redis/issues/3241</p><p>  http://blog.csdn.net/qq_25797077/article/details/51986455</p><p>  http://stackoverflow.com/questions/8537254/redis-connect-to-remote-server</p><p>  我没有注释掉bind 127.0.0.1,而是将bind 127.0.0.1 改成了bind 0.0.0.0。</p><p></p>
T:0.007218s,M:252.03 KB
返回顶部 留言