DENIED Redis is running in protected mode错误

项目场景:

docker 部署jar包应用,连接宿主机的Redis


问题描述:

使用docker容器连接宿主机的Redis出现以下错误

at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:74)

at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41)

at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)

at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)

at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:268)

at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.convertLettuceAccessException(LettuceStringCommands.java:799)

at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.set(LettuceStringCommands.java:148)

at org.springframework.data.redis.connection.DefaultedRedisConnection.set(DefaultedRedisConnection.java:274)

at org.springframework.data.redis.core.DefaultValueOperations$3.inRedis(DefaultValueOperations.java:240)

at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:60)

at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:225)

at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:185)

at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96)

at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:236)

at com.dataocean.common.core.redis.RedisCache.setCacheObject(RedisCache.java:34)

at com.dataocean.system.service.impl.SysConfigServiceImpl.init(SysConfigServiceImpl.java:44)

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.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)

at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)

at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)

... 39 common frames omitted

Caused by: io.lettuce.core.RedisException: io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.


原因分析:

于redis的保护模式开启了,并且没有绑定ip地址,没有密码认证


解决方案:

方案一:

1) 使用 设置CONFIG SET protected-mode no

步骤:

1. 在redis本机上打开redis-cli工具,并连接

2. 在其中输入CONFIG SET protected-mode no即可

缺点:

该种方式只是短暂性有效,如果redisServer重新启动后还是不能正常链接

方案二:

1)编辑redis配置文件,将保护模式关闭

步骤:

由于我是直接使用redis-server 命令启动的,所以说其服务是按照其默认设置进行启动,修改/usr/local/etc/redis.conf

如果没有请从下载的redis中复制一个redis.conf到这里,

将bind 后面绑定的ip后面加入你本机外网的ip

1

bind 127.0.0.1 192.168.21.4

  

然后保存,再使用redis-server /usr/local/etc/redis.conf启动

问题:

  1.redis-cli不能直接使用,必须输入绑定的ip+d端口号才能正常使用

  2.不安全

2). 将redis.conf中安全模式关闭

将该保护模式改为no

再试

缺点:1.保护模式关闭后不安全

3)设置密码访问模式

在redis.conf中加入一行密码设置

1

require 你的密码

 保存,再重新启动redis客户端

以上四种方式解决该错误都可以,但是根据上面的优缺点,

猜你喜欢

转载自blog.csdn.net/poxiao58/article/details/115457975