一. 异常现象
壹哥最近在带学生做项目的过程中,有学生在使用Redis的时候,产生了如下这样的异常信息:
com.yyg.demo.Demo01Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: 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. at redis.clients.jedis.Protocol.processError(Protocol.java:127) at redis.clients.jedis.Protocol.process(Protocol.java:161) at redis.clients.jedis.Protocol.read(Protocol.java:215) at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239) at redis.clients.jedis.Jedis.set(Jedis.java:121) at com.yyg.demo.Demo01.main(Demo01.java:18)Process finished with exit code 1
上面这个异常现象,壹哥觉得具有一定的普遍性,所以特地给大家讲解一下该问题的解决过程。
二. 异常原因
其实之所以产生该问题,主要是因为你的项目所在的服务器,和Redis服务器不是在同一台机器上,产生了跨进程的远程访问。而Redis服务器在默认情况下开启了“保护模式”,也不允许非本机的进程来访问。所以,如果我们想解决该问题,只需要修改Redis的配置文件即可。
另外,如果你的Redis客户端软件,比如RedisDesktopManager私活连接不上你的Redis服务器,大概率也是这个原因。只要你按照壹哥接下来的步骤去修改,基本上就可以解决这两个问题了!
三. 解决办法
首先我们要找到自己的redis.conf配置文件,然后利用vi打开:
vi redis.conf
然后修改该文件中的绑定地址,可以将默认的127.0.0.0注释掉,然后bind 0.0.0.0,修改如下,:
接着关闭保护模式,如下图所示:
最后我们只需要重启redis服务即可。
#加载制定的配置文件
redis-server ./redis.conf &
以上就是解决该问题的办法和步骤,你Get到了吗?关注壹哥,干货多多!如果你想学习更多优质内容,就来找我哦。