本文参考自:尚硅谷redis视频
一、RDB是什么1、在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里。 2、Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺点是最后一次持久化后的数据可能丢失。
二、Fork的作用1、fork的作用是复制一个与当前进程一样的进程。新进程的所有数据(变量、环境变量、程序计数器等)数值都和原进程一致,但是是一个全新的进程,并作为原进程的子进程
三、RDB保存的是什么1、rdb 保存的是dump.rdb文件
四、RDB配置位置(SNAPSHOTTING)180 ################################ SNAPSHOTTING ################################
181 #
182 # Save the DB on disk:
183 #
184 # save
185 #
186 # Will save the DB if both the given number of seconds and the given
187 # number of write operations against the DB occurred.
188 #
189 # In the example below the behaviour will be to save:
190 # after 900 sec (15 min) if at least 1 key changed
191 # after 300 sec (5 min) if at least 10 keys changed
192 # after 60 sec if at least 10000 keys changed
193 #
194 # Note: you can disable saving completely by commenting out all "save" lines.
195 #
196 # It is also possible to remove all the previously configured save
197 # points by adding a save directive with a single empty string argument
198 # like in the following example:
199 #
200 # save ""
201
202 save 900 1
203 save 300 10
204 save 60 10000
205
206 # By default Redis will stop accepting writes if RDB snapshots are enabled
207 # (at least one save point) and the latest background save failed.
208 # This will make the user aware (in a hard way) that data is not persisting
209 # on disk properly, otherwise chances are that no one will notice and some
210 # disaster will happen.
211 #
212 # If the background saving process will start working again Redis will
213 # automatically allow writes again.
214 #
215 # However if you have setup your proper monitoring of the Redis server
216 # and persistence, you may want to disable this feature so that Redis will
217 # continue to work as usual even if there are problems with disk,
218 # permissions, and so forth.
219 stop-writes-on-bgsave-error yes
220
221 # Compress string objects using LZF when dump .rdb databases?
222 # For default that's set to 'yes' as it's almost always a win.
223 # If you want to save some CPU in the saving child set it to 'no' but
224 # the dataset will likely be bigger if you have compressible values or keys.
225 rdbcompression yes
226
227 # Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
228 # This makes the format more resistant to corruption but there is a performance
229 # hit to pay (around 10%) when saving and loading RDB files, so you can disable it
230 # for maximum performances.
231 #
232 # RDB files created with checksum disabled have a checksum of zero that will
233 # tell the loading code to skip the check.
234 rdbchecksum yes
235
236 # The filename where to dump the DB
237 dbfilename dump.rdb
238
239 # The working directory.
240 #
241 # The DB will be written inside this directory, with the filename specified
242 # above using the 'dbfilename' configuration directive.
243 #
244 # The Append Only File will also be created inside this directory.
245 #
246 # Note that you must specify a directory here, not a file name.
247 dir ./
248
- 数据保存频率: 1、save 900 1 900秒后保存,至少有1个key被更改时才会触发 2、save 300 10 300秒后保存,至少有10个key被更改时才会触发 3、save 60 10000 60秒后保存,至少有10000个key被更改时才会触发
- stop-writes-on-bgsave-error yes 最近一次save操作失败则停止写操作
- rdbcompression yes 启用压缩
- rdbchecksum yes 启用CRC64校验码,当然这个会影响一部份性能
- dbfilename dump.rdb 指定存储数据的文件名
- dir ./ 指定工作目录,rdb文件和aof文件都会存放在这个目录中,默认为当前目录
1、先修改redis.conf配置文件的快照选项,为了演示方便:设置每2分钟执行10次更改保存,并且设置保存的文件名称dump.rdb备份文件,如下图: 2、执行10次以上的修改操作,2分钟后查看默认备份目录下会自动产生rdb格式的备份文件。如下图:
3、冷拷贝dump.rdb备份文件为dump_bk.rdb,如下图:
4、执行flushall清空数据库命令,在执行shutdown停止命令,在执行exit退出,重新启动redis,查看数据为空。 原因:执行flushall命令,也会产生dump.rdb文件,但里面是空的,无意义。如下图:
5、删除空的dump.rdb备份文件,重新复制dump_bk.rdb为dump.rdb 到redis 安装目录并启动服务即可,如下图:
6、退出redis,再重启redis,重新查看数据,数据被恢复了。如下图:
将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可
七、RDB优势1、适合大规模的数据恢复 2、对数据完整性和一致性要求不高
八、RDB劣势1、在一定间隔时间做一次备份,所以如果redis意外down掉的话,就会丢失最后一次快照后的所有修改 2、fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑
九、RDB如何停止动态所有停止RDB保存规则的方法:redis-cli config set save ""
十、小节