当前位置:   article > 正文

解决Spring Boot中使用Jedis连接Redis异常:JedisDataException: ERR Client sent AUTH, but no password is set_nested exception is redis.clients.jedis.exceptions

nested exception is redis.clients.jedis.exceptions.jedisdataexception: err t

问题描述

在Spring Boot项目中使用Jedis客户端连接Redis时,出现如下异常: 

  1. Exception in thread "main" org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
  2. at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:281)
  3. at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:475)
  4. at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
  5. at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95)
  6. at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82)
  7. at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:215)
  8. at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:188)
  9. at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96)
  10. at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:236)
  11. at cn.zyt.springbootlearning.config.RedisConfiguration.main(RedisConfiguration.java:59)
  12. Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
  13. at redis.clients.jedis.util.Pool.getResource(Pool.java:59)
  14. at redis.clients.jedis.JedisPool.getResource(JedisPool.java:234)
  15. at redis.clients.jedis.JedisPool.getResource(JedisPool.java:15)
  16. at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:271)
  17. ... 9 more
  18. Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
  19. at redis.clients.jedis.Protocol.processError(Protocol.java:132)
  20. at redis.clients.jedis.Protocol.process(Protocol.java:166)
  21. at redis.clients.jedis.Protocol.read(Protocol.java:220)
  22. at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:318)
  23. at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:236)
  24. at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2229)
  25. at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:119)
  26. at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:889)
  27. at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:424)
  28. at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:349)
  29. at redis.clients.jedis.util.Pool.getResource(Pool.java:50)
  30. ... 12 more

解决方法

该问题的出现是由于Redis中本身没有设置密码,但连接时指定了密码则会带Auth,这就造成了如上的异常。最简单的解决方法:将连接时设置的密码删掉后,重新尝试异常即解决。

此外如果需要使用Redis的签名认证,可以使用如下两种方式设置Redis中密码:

1. 在指定配置文件启动时:

yitian@heron01:/usr/local/bin$ redis-server /etc/redis/redis.conf

在配置文件中进行如下设置,既可以指定Redis的密码(将下面foobared改为你需要设置的密码),此时在Jedis连接Redis时需要指定密码。

2. 使用命令行设置Redis密码,此时并不会改变配置文件中的设置,但也可以使密码的设置生效。设置后,客户端重新连接server则需要密码。

config set requirepass 123456

查看密码设置的情况:

config get requirepass

带密码命令行登录Server: 

redis-cli -p 6379 -a 123456

注意,上述指定redis.conf配置文件启动的方式可以使配置文件中的密码生效,而默认redis-server不带配置文件(或配置文件为默认状态时),是没有设置密码的,所以命令行cli连接无需验证,而Jedis登录也不需要带password。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/554936
推荐阅读
相关标签
  

闽ICP备14008679号