详解Redis开启远程登录连接
程序员文章站
2022-06-17 18:57:33
今天使用jedis客户端api连接远程连接redis的时候,一直报错,如下:
redis.clients.jedis.exceptions.jedisconnec...
今天使用jedis客户端api连接远程连接redis的时候,一直报错,如下:
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
原来是redis默认只能localhost登录,所以需要开启远程登录。解决方法如下:
在redis的配置文件redis.conf中,找到bind localhost注释掉。
注释掉本机,局域网内的所有计算机都能访问。
band localhost 只能本机访问,局域网内计算机不能访问。
bind 局域网ip 只能局域网内ip的机器访问, 本地localhost都无法访问。
验证方法:
[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 *:6379 中通过"*"就可以看出此时是允许所有的ip连接登录到这台redis服务上。
注意事项:
今天再设置远程访问的时候,在启动redis的时候报错:creating server tcp listening socket *:6379: unable to bind socket(redis一定不能设置成后台运行,否则终端不会有任何错误显示)。
上网搜索说各种原因的都有,有的说是redis版本的bug(我用的版本是3.2.0),我是参考以下几篇文章解决了这个问题:
我没有注释掉bind 127.0.0.1,而是将bind 127.0.0.1 改成了bind 0.0.0.0。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。