解决Redis报错: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.
如果你想查看redis的log文件,一般存放在/var/log/redis/redis-server.log
里:
tail /var/log/redis/redis-server.log -n 100
发现报错:
25848:M 07 Apr 18:43:24.098 # Can't save in background: fork: Cannot allocate memory
25848:M 07 Apr 18:43:30.015 * 1 changes in 900 seconds. Saving...
25848:M 07 Apr 18:43:30.015 # Can't save in background: fork: Cannot allocate memory
25848:M 07 Apr 18:43:36.028 * 1 changes in 900 seconds. Saving...
25848:M 07 Apr 18:43:36.029 # Can't save in background: fork: Cannot allocate memory
25848:M 07 Apr 18:43:42.046 * 1 changes in 900 seconds. Saving...
25848:M 07 Apr 18:43:42.047 * Background saving started by pid 10605
10605:C 07 Apr 18:43:42.615 * DB saved on disk
10605:C 07 Apr 18:43:42.620 * RDB: 0 MB of memory used by copy-on-write
网上的解决方法大都是关闭报错:
127.0.0.1:6379> config set stop-writes-on-bgsave-error no
这个方案只是治标不治本的,毕竟如果关闭持久化,服务器关机后,redis的数据会被全部清空。
运行命令:
sudo echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
sudo sysctl -p
造成这个问题的另一个可能原因是Redis没有权限保存文件。
Redis保存的文件名默认为dump.rdb,查看文件的位置:
-> # locate dump.rdb
/var/lib/redis/dump.rdb
查看是否拥有文件权限:
-> # ls -al /var/lib/redis/
total 2376
drwxr-x--- 2 redis redis 4096 Mar 30 08:54 .
drwxr-xr-x 50 root root 4096 Nov 22 16:33 ..
-rw-rw---- 1 redis redis 2418060 Mar 30 08:54 dump.rdb
根据他人的建议,目录的权限最好为755,文件的权限为644
注意:我直接使用777权限,请不要在生产环境中使用
chown 777 /var/lib/redis
chown 777 /var/lib/redis/dump.rdb
然后在Redis中运行命令:
SAVE
如果没有报错就问题解决了。

本文解决Redis在尝试进行RDB快照时出现的内存分配错误问题,包括调整系统配置、设置正确的文件权限以及执行SAVE命令来验证解决方案。
1万+

被折叠的 条评论
为什么被折叠?



