2018-12-10 · Develop

Java 连接 Redis 常见错误汇总

本文记录下使用 Redis 的过程中经常遇到的一些问题。


如果你是远程连接,第一个需要考虑的就是防火墙问题,下面是配置 Firewalld 的方式。

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

sudo firewall-cmd --reload

bind 地址设置

默认的 bind 接口是,也就是本地回环地址。这样的话,访问 redis 服务只能通过本机的客户端连接,而无法通过远程连接。
需要注意的是: bind 不是绑定外部服务器的 IP,而是绑定本机可以接受访问的 IP

# 配置多个地址

# 配置所有地址

# 注释掉 bind ,功能同上
# bind 


(error) 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 lookback 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 --portected-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.

报错信息说 处于保护模式,只能本地连接。关闭保护模式:

protected-mode no


requirepass your_password

daemonize 是用来指定 redis 是否要用守护线程的方式启动。

daemonize yes

keyspace notifications

Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]:
Invocation of init method failed; nested exception is java.lang.IllegalStateException: Unable to configure Redis to keyspace notifications.
See http://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository-sessiondestroyedevent
Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR unknown command config

如果报上面的错误的话是因为 Spring Session 框架是需要Redis开启Keyspace Notifications功能的,默认是关闭的。Spring 的详细介绍 开启方式如下:

notify-keyspace-events Egx


redis-cli config set notify-keyspace-events Egx