Redis ”MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on di

来源:互联网 发布:mac怎么下网页视频 编辑:程序博客网 时间:2024/05/16 09:20

今天第二次遇到Redis “MISCONF Redis is configured to save RDB snapshots, but iscurrently not able to persist on disk”的问题。这个错误信息是Redis客户端工具在保存数据时候抛出的异常信息。

网上查了一下很多人都是建议“config set stop-writes-on-bgsave-error no”。这样做其实是不好的这仅仅是让程序忽略了这个异常使得程序能够继续往下运行但实际上数据还是会存储到硬盘失败!

上一次遇到这个问题是因为一个程序的Bug造成系统内存被耗尽了后来修复了那个Bug问题就解决了。今天出现问题时查看系统内存还有2GB左右“感觉好像不是内存的缘故”(后面发现还是因为内存的缘故)。

由于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' )使其生效。)

当时没明白意思就忽略了。再启动Redis客户端程序保存数据时继续报“MISCONFRedis 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”这个提示找到了解决方法:

// 原文:http://pydelion.com/2013/05/27/<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: black; background: rgb(255, 153, 153);">redis</span>-cant-<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: white; background: rgb(136, 104, 0);">save</span>-in-background-fork-cannot-allocate-memory/If you get this <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: black; background: rgb(255, 255, 102);">error</span>Can't <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: white; background: rgb(136, 104, 0);">save</span> in background: fork: Cannot allocate memoryit means that your current database <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: black; background: rgb(255, 102, 255);">is</span> bigger than memory you have. <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: white; background: rgb(0, 170, 0);">To</span> fix the issue enable vm.overcommit_memory:sysctl vm.overcommit_memory=1<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: white; background: rgb(0, 170, 0);">To</span> have if after reboot add this line <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: white; background: rgb(0, 170, 0);">to</span> /etc/sysctl.cnf:vm.overcommit_memory=1

修改vm.overcommit_memory=1后问题果然解决了。

为什么系统明明还剩2GB的内存,Redis会说内存不够呢?

网上查了一下有人也遇到类似的问题并且给出了很好的分析(详见:http://www.linuxidc.com/Linux/2012-07/66079.htm)简单地说:Redis在保存数据到硬盘时为了避免主进程假死需要Fork一份主进程然后在Fork进程内完成数据保存到硬盘的操作如果主进程使用了4GB的内存Fork子进程的时候需要额外的4GB此时内存就不够了Fork失败进而数据保存硬盘也失败了。

0 0
原创粉丝点击