解决Redis报错Redis is configured to save RDB snapshots, but it is currently not able to persist on disk.
Redis报错信息
在Spring Boot中集成Redis客户之后,进行数据操作,发现报如下错误:
org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error. at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:54) at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:52) 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:270) 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:281) 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:228) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:188) at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96) at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:236)
报错原因
从Redis层面来分析错误的直接原因是:
Redis被配置为保存数据库快照,但它目前不能持久化到硬盘。用来修改集合数据的命令不能用。请查看Redis日志的详细错误信息。
也就是说,Redis无法将缓存中的数据写入本地磁盘。
针对Redis报错无法写入磁盘,往往有以下原因:
- 磁盘满了
- Redis配置问题
- 操作权限问题
如果磁盘满了,直接清理磁盘或进行扩充即可。
解决方案
问题一
如果是权限问题,查看日志会发现如下日志:
11875:M 06 Mar 2020 09:00:12.034 * 1 changes in 3600 seconds. Saving... 11875:M 06 Mar 2020 09:00:12.035 * Background saving started by pid 12625 12625:C 06 Mar 2020 09:00:12.036 # Failed opening the RDB file dump.rdb (in server root dir /usr/local/redis-5.0.7) for saving: Permission denied
如果是权限问题,则服务对应目录的权限,或在配置文件中修改目标目录到有权限操作的目录。
默认目录配置如下:
# The filename where to dump the DB dbfilename dump.rdb # The working directory. # # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. dir ./
修改下面的dir路径:
dir /Users/zzs/tools/dbs
然后重启即可。在重启的过程中可能依然由于无法保存数据而死循环,此时就需要杀手锏了。
问题二
如果是配置问题导致,则可进行如下修改。
使用redis-cli连接上redis,然后执行如下命令:
config set stop-writes-on-bgsave-error no
通过将stop-writes-on-bgsave-error设置为no来进行解决。但这种方案治标不治本,从根本上来讲,还是需要大家查看后台异常原因,进行有针对性的解决。
关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台
除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接