save 900 1 每900秒保存一次
save 300 10 300秒内如果更新了10次就保存一次
save 60 10000 60秒内更新了10000次就保存一次
- RDB
rdb的持久化保存的文件一般为dump.rdb redis 默认为rdb持久化
关于rdb的配置文件内容(自动触发)
################################ SNAPSHOTTING ################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
#
# Note: you can disable saving completely by commenting out all "save" lines.
#
# It is also possible to remove all the previously configured save
# points by adding a save directive with a single empty string argument
# like in the following example:
#
# save ""
执行FLUSHALL会执行rdb操作
退出redis也会执行rdb操作
stop-writes-on-bgsave-error :默认值为yes。当启用了RDB且最后一次后台保存数据失败,Redis是否停止接收数据。这会让用户意识到数据没有正确持久化到磁盘上,否则没有人会注意到灾难(disaster)发生了。如果Redis重启了,那么又可以重新开始接收数据了
rdbcompression ;默认值是yes。对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。如果你不想消耗CPU来进行压缩的话,可以设置为关闭此功能,但是存储在磁盘上的快照会比较大。
rdbchecksum :默认值是yes。在存储快照后,我们还可以让redis使用CRC64算法来进行数据校验,但是这样做会增加大约10%的性能消耗,如果希望获取到最大的性能提升,可以关闭此功能。
dbfilename :设置快照的文件名,默认是 dump.rdb
dir:设置快照文件的存放路径,这个配置项一定是个目录,而不能是文件名。默认是和当前配置文件保存在同一目录。
启动redis的时候会自动扫描配置文件dir配置的目录下的rdb文件,自动恢复数据
- AOF
aof 默认不开启 需要在配置文件将 appendonly no 改为 yes 即可
appendonly yes
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"
appendfsync always 每次修改都会同步
appendfsync everysec 每秒同步一次,可能会丢失一秒的数据
appendfsync no 不会自动同步,需要人为手动同步,性能最好
如果aof文件有错误,开启将会redis将会失败
当aof文件出错,需要使用aof文件检测工具 redis-check-aof
使用命令 ./redis-check-aof --fix appendonly.aof
检测过后就可以重新连接使用redis