今天遇到Redis “MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk”的問題。這個錯誤資訊是Redis用戶端工具在儲存資料時候抛出的異常資訊。
很多人都是建議“config set stop-writes-on-bgsave-error no”。這樣做其實是不好的,這僅僅是讓程式忽略了這個異常,使得程式能夠繼續往下運作,但實際上資料還是會存儲到硬碟失敗!本人并不推薦這種方式。如果是在自己的電腦做一些練手項目,直接重新開機一下虛拟機就可以了
當然,如果先徹底解決這個問題還有以下的解決方案
由于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用戶端,程式儲存資料時繼續報“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