报错场景:
使用redis的时候报错
redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
大致意思
redis异常响应错误:MISCONF Redis被配置为保存RDB快照,但当前无法在磁盘上持久化。用来修改数据集合的命令不能用,因为此实例配置为在RDB快照失败时在写入过程中报告错误(stop-writes-on-bgsave-error选项)。请检查Redis日志以获取有关RDB错误的详细信息。
错误原因:
强制把redis快照关闭了导致不能持久化的问题
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes
然而如果配置了redis服务器的监控和持久化,您可能希望禁用此功能,使Redis将继续工作,即使出现了磁盘故障、权限等问题。就是redis出现了错误,但是redis并没有配置持久化,所以就会导致错误信息没办法保存,所以直接报错了
处理方式
通过redis-cli连接到服务器后执行以下命令,关闭配置项stop-writes-on-bgsave-error即可
config set stop-writes-on-bgsave-error no
知识扩展
redis有两种持久化方式,RDB 和 AOF,RDB方式是指在指定的时间间隔内将内存中的数据集快照写入磁盘,实际的操作过程是fork一个子进程。
先将数据集写入临时文件,写入成功后,再替换之前的文件,用二进制压缩存储(binlog),存储的文件为:dum.rdb
常见问题
- Could not connect to Redis at 127.0.0.1:6379: Connection refused
# 查看redis启用状态
ps -ef | grep redis
redis 24124 1 0 2019 ? 06:23:43 /usr/bin/redis-server *:8888
# redis启用,但是端口号被修改过,通过端口号访问即可
redis-cli -p 8888
127.0.0.1:8888>
- 提示操作权限不够 (error) NOAUTH Authentication required.
# root账号登录,并填写正确的密码
127.0.0.1:8888> auth root
(error) ERR invalid password
# 忘记密码,可以通过配置文件搜索requirepass 找到对应位置
127.0.0.1:8888> auth foobared
OK
- 如何查看redis的快照文件
$ 通过PID进程号查看工作区
ls -l /proc/$pid/cwd
lrwxrwxrwx 1 redis redis 0 Apr 26 15:25 /proc/24124/cwd -> /var/lib/redis
➜ redis ls /var/lib/redis
dump.rdb