RDB数据持久化
目录
原理
-
RDB持久化:将Redis在内存中的数据定时
dump
到磁盘上,实际操作过程是fork
一个子进程,先将数据写入临时文件,写入成功后,再替换之前的文件,用二进制压缩存储

实现步骤
自动数据持久化
redis支持自动数据持久化,(默认开启)。在redis.conf配置文件中有数据自动备份配置,在197行开始是数据快照相关配置。一下为默认配置
################################ 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 ""
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 /var/lib/redis
①、save:这里是用来配置触发 Redis的持久化条件,也就是什么时候将内存中的数据保存到硬盘。默认如下配置:
save 900 1:表示900 秒内如果至少有 1 个 key 的值变化,则保存
save 300 10:表示300 秒内如果至少有 10 个 key 的值变化,则保存
save 60 10000:表示60 秒内如果至少有 10000 个 key 的值变化,则保存
当然如果你只是用Redis的缓存功能,不需要持久化,那么你可以注释掉所有的 save 行来停用保存功能。可以直接一个空字符串来实现停用:save ""
②、stop-writes-on-bgsave-error :默认值为yes。当启用了RDB且最后一次后台保存数据失败,Redis是否停止接收数据。这会让用户意识到数据没有正确持久化到磁盘上,否则没有人会注意到灾难(disaster)发生了。如果Redis重启了,那么又可以重新开始接收数据了
③、rdbcompression ;默认值是yes。对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。如果你不想消耗CPU来进行压缩的话,可以设置为关闭此功能,但是存储在磁盘上的快照会比较大。
④、rdbchecksum :默认值是yes。在存储快照后,我们还可以让redis使用CRC64算法来进行数据校验,但是这样做会增加大约10%的性能消耗,如果希望获取到最大的性能提升,可以关闭此功能。
⑤、dbfilename :设置快照的文件名,默认是 dump.rdb
⑥、dir:设置快照文件的存放路径,这个配置项一定是个目录,而不能是文件名。使用上面的 dbfilename 作为保存的文件名
手动RDB持久化
同样也可以使用命令进行数据持久化,redis提供了SAVE和GBSAVE
127.0.0.1:6379> save
OK
127.0.0.1:6379> bgsave
Background saving started
SAVE
和BGSAVE
命令的区别在于:SAVE
命令是阻塞主进程,save操作完成之后,主进程才开始工作,客户端可以连接;BGSAVE
命令是fork一个专门save的子进程,此操作不会影响主进程
注:SAVE
只是将当前的数据库备份,备份文件名默认为dump.rdb
,可通过配置文件修改备份文件名 dbfilename xxx.rdb
(发现一个问题:如果要对多个数据库进行备份,那么最终只能备份最后一个数据库,因为dump.rdb
文件会相互覆盖)
备份恢复
将备份的RDB文件,放在指定目录,重启Redis即可恢复数据
- 备份的RDB文件: 通过命令
redis 127.0.0.1:6379> CONFIG GET dir
查看执行SAVE
命令之后,redis默认存放备份文件的目录;通过命令redis 127.0.0.1:6379> CONFIG GET dbfilename
查看备份RDB文件的文件名称; - 指定目录: 通过命令
redis 127.0.0.1:6379> CONFIG GET dir
,得出redis从哪个目录读取备份文件(一般只要直接重启Redis就能恢复数据,因为备份的默认目录和启动读取的目录是同一个)
注意:如果备份目录和启动读取目录是同一个时(默认配置是同一个),在使用SAVE或GBSAVE进行数据备份之后,需要将备份文件进行重命名或者移动到其他目录下,因为配置中默认启动自动备份,触发条件是
save 900 1:表示900 秒内如果至少有 1 个 key 的值变化,则保存
只要有一个key发生变化,就会触发备份。这样会覆盖同名备份文件,进行重启之后恢复的不是手动备份时的数据