io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode

Problem

While trying to connect to Redis running on Linux through my java code getting io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode.

The exception stack trace is as below.

io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode because the 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.
        at io.lettuce.core.protocol.CommandHandler.onProtectedMode(CommandHandler.java:667) ~[lettuce-core-5.0.1.RELEASE.jar!/:na]
        at io.lettuce.core.protocol.CommandHandler.channelInactive(CommandHandler.java:305) ~[lettuce-core-5.0.1.RELEASE.jar!/:na]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.ChannelInboundHandlerAdapter.channelInactive(ChannelInboundHandlerAdapter.java:75) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.lettuce.core.ChannelGroupListener.channelInactive(ChannelGroupListener.java:46) ~[lettuce-core-5.0.1.RELEASE.jar!/:na]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.ChannelInboundHandlerAdapter.channelInactive(ChannelInboundHandlerAdapter.java:75) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.lettuce.core.PlainChannelInitializer$1.channelInactive(PlainChannelInitializer.java:85) ~[lettuce-core-5.0.1.RELEASE.jar!/:na]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelInactive(DefaultChannelPipeline.java:1429) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:947) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.AbstractChannel$AbstractUnsafe$8.run(AbstractChannel.java:822) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-common-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-common-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:466) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]

Solution

After analyzing the logs noticed following lines which describe the root cause and fix for this.

DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no auth

In order to fix this we need to do one of the following:

  1. Disable protected mode from redis CLI
  2. Disable protected mode from redis config
  3. Restart redis it with the ‘–protected-mode no’ option, only for testing purposes.
  4. Setup a bind address or an auth

Since we knew the solution for this, let’s see how we can actually do it.

  1. To disable the protected mode just open the redis-cli and run ‘CONFIG SET protected-mode no’.
  2. To disable the protected mode stop redis server and edit redis.conf file inside conf folder, add ‘protected-mode no’.¬† Restart the redis server .
  3. To restart redis server with –protected-mode no’ option, run redis-server¬† with option –protected-mode. Like redis-server –protected-mode no
  4. To bind an IP address stop redis server, edit redis config and add bind *.*.*.* to bind a specific IP to access the redis server. i.e. bind 127.0.0.1. Restart redis server.

Note: If none of the above options are performed you will not be able to access redis server.

If you know of any other fixes to this issue please let us know through comments.

Read more about Java

What happens when a Java object is created?

Java 8 Stream API with examples

Happy Learning !!