在写入Redis (SET foo bar)期间,我得到以下错误:

MISCONF Redis被配置为保存RDB快照,但当前为 无法在磁盘上持久保存。可能修改数据集的命令是 禁用。有关错误的详细信息,请查看Redis日志。

基本上我理解的问题是,redis是不能在磁盘上保存数据,但不知道如何摆脱这个问题。

下面的问题也有同样的问题,它在很久以前就被抛弃了,没有答案,很可能没有尝试解决这个问题。


当前回答

此错误是由于BGSAVE失败导致的。在BGSAVE期间,Redis会fork一个子进程来将数据保存到磁盘上。虽然BGSAVE失败的确切原因可以从日志中检查(通常在linux机器上的/var/log/redis/redis-server.log),但很多时候bgive失败是因为fork不能分配内存。很多时候,由于操作系统的优化冲突,fork无法分配内存(尽管机器有足够的可用RAM)。

可以从Redis常见问题中阅读:

Redis background saving schema relies on the copy-on-write semantic of fork in modern operating systems: Redis forks (creates a child process) that is an exact copy of the parent. The child process dumps the DB on disk and finally exits. In theory the child should use as much memory as the parent being a copy, but actually thanks to the copy-on-write semantic implemented by most modern operating systems the parent and child process will share the common memory pages. A page will be duplicated only when it changes in the child or in the parent. Since in theory all the pages may change while the child process is saving, Linux can't tell in advance how much memory the child will take, so if the overcommit_memory setting is set to zero fork will fail unless there is as much free RAM as required to really duplicate all the parent memory pages, with the result that if you have a Redis dataset of 3 GB and just 2 GB of free memory it will fail. Setting overcommit_memory to 1 says Linux to relax and perform the fork in a more optimistic allocation fashion, and this is indeed what you want for Redis.

Redis不需要像操作系统认为的那样多的内存来写入磁盘,所以可能会预先导致fork失败。

要解决这个问题,您可以:

修改/etc/sysctl.conf,增加:

vm.overcommit_memory=1

然后重新启动sysctl:

在FreeBSD上:

sudo /etc/rc.d/sysctl reload

在Linux上:

sudo sysctl -p /etc/sysctl.conf

其他回答

我也面临着同样的问题。两个答案(被点赞最多的和被接受的)都只是暂时的解决办法。

此外,配置set stop-writes-on-bgsave-error no是一种可怕的忽略这个错误的方式,因为这个选项所做的是阻止redis通知写已经停止,并继续在快照中不写数据。这是简单地忽略这个错误。 请参考这个

至于在redis-cli的config中设置dir,当你重新启动redis服务时,这个问题也会被清除,并且会再次弹出相同的错误。在redis.conf中dir的默认值是。/,如果你以root用户启动redis,那么。/是/,写权限不被授予,因此会出现错误。

最好的方法是在redis.conf文件中设置dir参数,并对该目录设置适当的权限。大多数debian发行版都将它放在/etc/redis/redis.conf中

如果你在windows机器上本地运行Redis,试着“以管理员身份运行”,看看它是否有效。对我来说,问题是Redis位于“程序文件”文件夹中,默认情况下会限制权限。这是应该的。

但是,不要以管理员身份自动运行Redis。你不会想要授予它更多的权限。你想按规矩办事。

因此,我们能够通过作为管理员运行它来快速识别问题,但这不是解决方法。一种可能的情况是,你把Redis放在一个没有写权限的文件夹中,结果DB文件被存储在同一个位置。

您可以通过打开redis.windows.conf并搜索以下配置来解决此问题:

    # The working directory.
    #
    # The DB will be written inside this directory, with the filename specified
    # above using the 'dbfilename' configuration directive.
    #
    # The Append Only File will also be created inside this directory.
    #
    # Note that you must specify a directory here, not a file name.
    dir ./

将dir ./更改为具有常规读写权限的路径

你也可以将Redis文件夹整体移动到一个你知道拥有正确权限的文件夹。

对我来说

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

我重新加载我的mac,它工作

在我的情况下,Ubuntu虚拟机的磁盘空间已满,这就是为什么我得到这个错误。从磁盘删除一些文件后,已解决问题。

在我的情况下,它发生了,因为我刚刚安装了redis使用快速的方式。所以redis不是以根用户身份运行。 我可以按照他们快速入门指南中“更正确地安装Redis”部分的说明来解决这个问题。这样做之后,问题得到了解决,redis现在以根用户身份运行。来看看。