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

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

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

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


当前回答

现在,Redis的写访问问题在官方的Redis docker容器中重新出现,给客户端这个错误消息。

来自官方Redis映像的Redis试图将.rdb文件写入containers /data文件夹中,这是相当不幸的,因为它是一个根目录拥有的文件夹,而且它也是一个非持久化的位置(如果容器/pod崩溃,写入那里的数据将消失)。

所以在一个小时的不活动后,如果你以非根用户运行你的redis容器(例如docker run -u 1007而不是默认的docker run -u 0),你会在你的服务器日志中得到一个非常详细的错误信息(参见docker logs redis):

1:M 29 Jun 2019 21:11:22.014 * 1 changes in 3600 seconds. Saving...
1:M 29 Jun 2019 21:11:22.015 * Background saving started by pid 499
499:C 29 Jun 2019 21:11:22.015 # Failed opening the RDB file dump.rdb (in server root dir /data) for saving: Permission denied
1:M 29 Jun 2019 21:11:22.115 # Background saving error

所以你需要做的是将容器的/data文件夹映射到一个外部位置(非根用户,这里:1007,有写权限,比如主机上的/tmp),例如:

docker run --rm -d --name redis -p 6379:6379 -u 1007 -v /tmp:/data redis

所以这是官方docker镜像的错误配置(应该写入/tmp而不是/data),产生了这个“定时炸弹”,你很可能只会在生产中遇到…在某个特别安静的假日周末过夜:/

其他回答

遇到此错误,并且能够从日志中找出该错误是由于磁盘空间不足造成的。所有插入到我箱子里的数据都不再需要了。所以我试着冲洗马桶。由于redis-rdb-bgsave进程正在运行,因此也不允许FLUSH数据。我按照下面的步骤,并能够继续。

登录redis客户端 执行config set stop-writes-on-bgsave-error no 执行FLUSHALL(不需要存储的数据) 执行config set stop-writes-on-bgsave-error yes

进程redis-rdb-bgsave在执行以上步骤后不再运行。

此错误是由于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

如果你遇到错误,一些重要的数据不能被丢弃在运行的redis实例上(rdb文件或其目录的权限不正确,或运行出磁盘空间),你总是可以重定向rdb文件写入其他地方。

使用redis-cli,你可以这样做:

CONFIG SET dir /tmp/some/directory/other/than/var
CONFIG SET dbfilename temp.rdb

在此之后,您可能想要执行BGSAVE命令,以确保数据将被写入rdb文件。确保当你执行INFO持久化时,bgsave_in_progress已经是0,rdb_last_bgsave_status是ok。在此之后,您现在可以开始在安全的地方备份生成的rdb文件。

正如@Chris指出的,问题可能是低内存。当我们分配太多内存给MySQL (innodb_buffer_pool_size)时,我们就开始遇到这种情况。

为了确保有足够的内存用于Redis和其他服务,我们减少了MySQL上的innodb_buffer_pool_size。

由于内存不足,bgsave过程中可能会出现错误。试试这个(从redis后台保存FAQ)

echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1