lang80270人评论585人阅读2018-02-27 09:51:47
最近发现一个业务的数据无法插入redis中,尝试在redis中执行命令时报了如下错误提示:
127.0.0.1:6380> SET Key0 Value0 (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
大意为:(错误)misconf redis被配置以保存数据库快照,但misconf redis目前不能在硬盘上持久化。用来修改数据集合的命令不能用,请使用日志的错误详细信息。
这是由于强制停止redis快照,不能持久化引起的,运行info命令查看redis快照的状态,如下:
<span style="color:#333333"><span style="color:black"><code class="language-bash"><span style="color:slategray"># Persistence</span> loading:0 rdb_changes_since_last_save:973 rdb_bgsave_in_progress:0 rdb_last_save_time:1511138863 rdb_last_bgsave_status:err rdb_last_bgsave_time_sec:9 rdb_current_bgsave_time_sec:-1 aof_enabled:0 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:-1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok</code></span></span>解决方案如下:
运行 config set stop-writes-on-bgsave-error no 命令,关闭配置项stop-writes-on-bgsave-error解决该问题,使数据能正常插入redis。
<span style="color:#333333"><span style="color:black"><code class="language-bash">127.0.0.1:6380<span style="color:#9a6e3a">></span> config <span style="color:#0077aa">set</span> stop-writes-on-bgsave-error no OK 127.0.0.1:6380<span style="color:#9a6e3a">></span> SET Key0 Value0 OK</code></span></span>但这样做其实是不好的,这仅仅是让程序忽略了这个异常,使得程序能够继续往下运行,实际上数据还是会存储到硬盘失败!
其实这个问题的根源是服务器内存问题。
如果Redis是daemon模式运行,是没法看到详细的日志的,修改配置文件设置logfile参数为文件(默认是stdout,建议以后安装完毕就修改这个参数为文件,不然会丢掉很多重要信息),重启Redis,查看日志,如果内存不够用时程序启动时就有一行警告提示:
“WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.” (警告:过量使用内存设置为0!在低内存环境下,后台保存可能失败。为了修正这个问题,请在/etc/sysctl.conf 添加一项 'vm.overcommit_memory = 1' ,然后重启(或者运行命令'sysctl vm.overcommit_memory=1' )使其生效。)
<span style="color:#333333"><span style="color:black"><code class="language-bash"><span style="color:#0077aa">echo</span> <span style="color:#669900">"vm.overcommit_memory = 1"</span> <span style="color:#9a6e3a">>></span>/etc/sysctl.conf sysctl vm.overcommit_memory<span style="color:#9a6e3a">=</span>1</code></span></span>如果不做修改,程序保存数据时会继续报“MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk”异常,再查看Redis日志,看到有这样的错误提示“Can’t save in background: fork: Cannot allocate memory”,这个提示很明显"Fork进程时内存不够用了!"
通过谷歌查询“Can’t save in background: fork: Cannot allocate memory”这个提示,找到了解决方法: view plaincopy to clipboardprint?
// 原文: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
修改vm.overcommit_memory=1后问题果然解决了。
为什么系统明明还有内存,Redis会说内存不够呢?
网上查了一下,有人也遇到类似的问题,并且给出了很好的分析(详见:http://www.linuxidc.com/Linux/2012-07/66079.htm)。
简单地说:Redis在保存数据到硬盘时为了避免主进程假死,需要Fork一份主进程,然后在Fork进程内完成数据保存到硬盘的操作,如果主进程使用了4GB的内存,Fork子进程的时候需要额外的4GB,此时内存就不够了,Fork失败,进而数据保存硬盘也失败了。