redis-2,redis持久化

发布时间 2023-04-07 17:28:14作者: niko5960

持久化

rdb:snapshot快照,持久化快照
aof:append only file 写命令操作全部记录下来

RDB

rdb持久性以指定的时间间隔,执行数据集的时间点快照,全量快照
rbd保存到磁盘的文件就是dump.rdb

案例

配置文件redis.conf
在配置文件中找到snapshotting

################################ 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.
快照默认触发方式有以下三种
900秒之内修改过一次
300秒之内修改过10次
60秒之内修改过10000次
这些情况下触发保存快照
#   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 ""
默认的快照触发
save 900 1
save 300 10
save 60 10000

# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# 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

# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes

# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes

# The filename where to dump the DB
这里修改保存的名字
dbfilename dump.rdb

# 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 /www/server/redis/

rdb的操作分为自动触发和手动触发。
当写一个提交命令的时候,比如flushdb,flushall,redis也会自动保存一份快照
shutdown命令模拟宕机的时候,也会保存最后一份快照
修改dump文件的保存路径
修改dump文件的名称

如何恢复

将备份文件dump.rdb移动到redis的安装目录并启动服务即可

AOF手动触发