Redis之 Unable to connect to localhost:6379

Posted 笛卡尔的三重梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis之 Unable to connect to localhost:6379相关的知识,希望对你有一定的参考价值。

Redis之Unable to connect to localhost:6379

1、无法连接的原因

我已经配置好密码了,但是Java程序运行仍然报无法连接本地端口,经过我一顿猛查找,终于发现为什么无法连接redis了。【不要密码是可以正常登录的】

windows 系统开启redis服务有两种,一种是双击redis里面的redis-server.exe文件;另一种是用cmd命令开启redis服务。我选择的是第一种,修改了密码忘记了开启redis服务加载配置文件了!

.\\redis-server.exe .\\redis.windows.conf

2、制作自启动批处理

这样每次开启都要加载配置文件过于麻烦,所以我做了一个批处理自启动。文件后缀是.bat结尾的。

图1:

图2:

**温馨提示:**因为每个人的路径不同,所以要自己替换一下自己本地redis的路径,不要把cd 这个系统自带命令的命令替换了,替换\\software\\Redis-x64-3.2.100 这部分即可。

d:
cd \\software\\Redis-x64-3.2.100
.\\redis-server.exe .\\redis.windows.conf
图3:

win+R 调出窗口 输入:shell:startup

图4:

把批处理命令放进去,就可以每次开机自启动了

图5:

3、redis注册成windows服务

批处理有点美中不足就是有cmd窗口不能关闭,这个时候我们可以选择注册成windows服务解决这个问题。在redis的目录里面打开cmd,输入命令出现下方图片就注册成功了

redis-server --service-install redis.windows.conf

这个时候我们可以去服务里面找到它,可以手动开启服务!

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis xxx

项目开发的过程中,应业务需求将Redis作为缓存服务器整合进了Spring Boot的项目中,在初测试的时候,遇到连接超时的情况,错误信息如下:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.43.129:6379
    at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:198)
    at com.leyou.test.RedisTest.testRedis(RedisTest.java:25)
    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: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.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.43.129:6379
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
    at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:233)
    at io.lettuce.core.RedisClient.connectStandalone(RedisClient.java:253)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:202)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:56)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:959)
    ... 41 more
Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.43.129:6379

网上很多,你应该已经查了一溜够了!全是要 protected-mode 修改为 no  并且 将 bind属性注释掉 (老版本只注释掉bind),不想说,治标不治本

config 配置文件!

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes
......

如果链接不上,我看很多都让把 protected-mode 修改为 no  并且 将 bind属性注释掉 (老版本只注释掉bind),但是,这样,不就谁都可以链接redis数据库了?那就不安全了啊

所以protected-mode属性根本不用改,bind属性不要注释,只需要在bind 后添加你想要链接此数据库的地址就好了 如:

bind 192.168.1.100 10.0.0.1 就代表192.168.1.100和10.0.0.1是可以链接到数据库的,人家写的很清楚了!而且这也是安全并被推荐使用的!

像这种情况,出现的原因可能有两种:

  • 其一,在redis的配置文件中,没有配置局域网的访问权限
  • 其二,被访问方的服务器的6379端口不允许访问

如果是云服务器,记得开放对应的6379端口号,我的是第二种情况

针对第一种情况,具体解决方案如下

打开配置文件redis.conf,搜索 “ bind ”,你会看到这一句:

这个bind配置的是,允许以这些地址来访问我们的reids,配置之前是只有127.0.0.1的,那只需要写上我们服务器的地址就好了,,注意,写的是服务器自己的地址,那么在这个配置之后,与其同局域网的电脑也就可以访问这个redis了

记得在改完配置文件后,重启redis,使得配置生效。

 

针对第二种情况,具体解决方案

首先运行: firewall-cmd --query-port=6379/tcp 以检测我们的端口是不是被允许访问的

这里很明显:

 

6379端口不允许外界访问。

所以我们需要打开端口:

firewall-cmd --zone=public --add-port=6379/tcp --permanent

然后重启防火墙即可!

开放端口:

firewall-cmd --zone=public --add-port=6379/tcp --permanent

关闭端口:

firewall-cmd --permanent --remove-port=6379/tcp

查询端口是否开放:

firewall-cmd --query-port=6379/tcp

重启:

firewall-cmd --reload

 

以上是关于Redis之 Unable to connect to localhost:6379的主要内容,如果未能解决你的问题,请参考以下文章

Unable to connect to Redis; nested exception is org.springframework.data.redis.connection.PoolExcept

Redis - Redis health check failed:Unable to connect to localhost:6379

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis xxx

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis xxx

项目启动报错Redis health check failed:Unable to connect to localhost:6379

Unable to connect to 192.168.xx.xxx:6379 连接虚拟机(Linux)上的redis失败解决方法