MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk

发现问题

程序高峰期访问时突然报错,日志显示错误如下:

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.

根据抛错的位置,判断是程序启动的时候某服务读取数据存入缓存时往redis写数据的地方报错了。错误显示根据redis配置需要将数据持久化到RDB快照中,但是当前无法持久化到磁盘上。根据stop-writes-on-bgsave-error配置项的配置,停止修改数据。

MISCONFRedisisconfiguredtosaveRDBsnapshots,butitcurrentlynotablepersistondisk

分析问题

众所周知,redis有两种持久化方式:AOF、RDB。RDB方式是指定时间内数据变动达到指定的次数就持久化到磁盘中。所以Redis配置文件中会配置保存的文件路径、文件名称、保存策略等,而配置项stop-writes-on-bgsave-error表示如果最近一次持久化时出错了,就停止往Redis中写数据,这样可以尽量保持数据一致。所以网上有人说把stop-writes-on-bgsave-error配置项的值改为false或no,但是这样并没有真正解决问题。此时应检查Redis日志、磁盘空间、可用内存等信息,来找到没有持久化成功的原因。


解决问题

按照修改stop-writes-on-bgsave-error配置项的值并没有真正的解决问题,发现程序端读不到前端的HTTPS,又出现connection refused,经排查后前端日志后发现是那服务器盘空间满了,而且有营销蜘蛛一直在爬,前端日志20分钟就占了1000M,就最后robots.txt屏蔽掉营销蜘蛛,再清理完磁盘后再启动就不报错了。


*延伸阅读

redis连接错误3种解决方案System Error MISCONF Redis is configured to save RDB snapshots

情况1解决办法:

由于强制停止redis快照,不能持久化引起的。

在客户端输入

config set stop-writes-on-bgsave-error no

 

情况2解决办法:

Redis在保存数据到硬盘时为了避免主进程假死,需要Fork一份主进程,然后在Fork进程内完成数据保存到硬盘的操作,如果主进程使用了4GB的内存,Fork子进程的时候需要额外的4GB,此时内存就不够了,Fork失败,进而数据保存硬盘也失败了。

修改vm.overcommit_memory=1

// 原文:http://pydelion.com/2013/05/27/redis-cant-save-in-background-fork-cannot-allocate-memory/  
If you get this error  
  
Can't save in background: fork: Cannot allocate memory  
  
it means that your current database is bigger than memory you have. To fix the issue enable vm.overcommit_memory:  
  
sysctl vm.overcommit_memory=1  
  
To have if after reboot add this line to /etc/sysctl.cnf:  
  
vm.overcommit_memory=1

 

情况3解决办法:

 Permission denied持久化文件无权限,改一下文件夹权限就解决了

chmod  777 /xxx/redis_data

当然是那种情况需要先看看redis日志文件。

redis在默认情况下,是不会生成日志文件的,所以需要配置 配置方法:

1、首先找到redis的配置文件

2、打开配置文件,直接搜logfile ""

3、将路径填入logfile后面的引号内,例如:logfile "/plus/data/redis_data/redislog/redis.log" 

4、手动建立了/plus/data/redis_data/redislog文件夹,日志文件不用建,重启redis会自动生成