赞
踩
Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到 一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。 整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能 如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺点是最后一次持久化后的数据可能丢失
# 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 ./
格式 save 秒数 写操作的次数
################################ 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 "" # 15分钟内改了1次 save 900 1 # 5分钟内改了10次 save 300 10 # 1分钟内改了10000次 save 60 10000
其实有分自动触发和手动触发:
手动触发:save/bgsave, 不建议
自动触发:满足redis配置文件中的自动触发条件(比如我们设置的:每5分钟有10次更新就会触发RDB文件生成)
自动触发:客户端关闭、关闭Redis服务时也会自动触发RDB
自动触发:执行flushall命令也会自动触发RDB
(个人感觉和word自动保存功能有相似的地方)
手动触发的区别:
stop-writes-on-bgsave-error
# 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的写操作。推荐yes
rdbcompression
# 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
rdbchecksum
# 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
在存储快照后,还可以让redis使用CRC64算法来进行数据校验,
但是这样做会增加大约10%的性能消耗,如果希望获取到最大的性能提升,可以关闭此功能
开始备份操作:rdb会生成dump.rdb文件这个我们前面已经介绍了,
第一步,查看dump所在目录:在客户端执行该命令 config get dir
第二步,复制dump文件到我们的定义的文件夹,这里就相当于保留快照文件, 假设在根目录下创建了一个文件夹/snapshot/rdb
//我们先进入dump所在文件夹, 使用cp命令复制dump到我们定义好的文件夹
[root@iZbp1gy0nsh5vadm0h4sjjZ data]# ls
appendonly.aof dump.rdb
[root@iZbp1gy0nsh5vadm0h4sjjZ data]# cp dump.rdb 2022_5_26.rdb /snapshot/rdb/2022_5_26.rdb
上面就备份成功了,假设突然数据库突然大量的key丢失,或者被删除,我们还原之前快照点,那就开始还原:
第一步,我们需要停止redis服务 , 并删除dump.rdb文件(也不一定删除,可能mv到其他地方也行,但是这个文件夹只能有一个dump.rdb文件)
第二步,我们把之前备份的rdb文件,cp到当前文件夹下,并命名为dump.rdb
这里讲一下cp和mv,cp是复制的意思,mv是移动,移动的话相当于把文件移动过来,原来文件不在那个地方了, 我是感觉用cp合适一点
//用mv的话就这样, .代表移动到当前文件夹
[root@iZbp1gy0nsh5vadm0h4sjjZ data]# mv /snapshot/rdb/2022_5_26.rdb .
//重命名, mv有移动和重命名2个功能
[root@iZbp1gy0nsh5vadm0h4sjjZ data]# mv 2022_5_26.rdb dump.rdb
第三步,重启redis服务
[root@iZbp1gy0nsh5vadm0h4sjjZ data]# docker stop xxx
//我这里是直接删除了当前的dump.rdb文件了
[root@iZbp1gy0nsh5vadm0h4sjjZ data]# rm -rf dump.rdb
//把备份文件复制过来,并命名为dump.rdb
[root@iZbp1gy0nsh5vadm0h4sjjZ data]# cp /snapshot/rdb/2022_5_26.rdb dump.rdb
//重启服务
[root@iZbp1gy0nsh5vadm0h4sjjZ data]# docker start xxx
RDB优缺点:
Redis
的高性能;而且RDB
文件存储的是压缩的二进制文件,适用于备份、全量复制,可用于灾难备份,同时RDB
文件的加载速度远超于AOF
文件。RDB
是间隔一段时间进行持久化,如果持久化之间的时间内发生故障,会出现数据丢失。所以这种方式更适合数据要求不严谨的时候,因为RDB
无法做到实时持久化,而且每次都要创建子进程,频繁创建成本过高
Redis
在备份时会独立创建一个子进程,将数据写入到一个临时文件(需要的内存是原本的两倍);RDB
文件保存的二进制文件存在新老版本不兼容的问题Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。