Redis之.RDB文件操作

Redis持久化操作

我们直到,Redis是内存数据库,如果不将内存中的数据库状态保存到磁盘中,那么一旦服务器的进程退出,服务器中的数据库状态也会消失,所有redis提供了持久化功能!

RDB(Redis DataBase)

 什么是rdb?

在这里插入图片描述

在指定的时间间隔内将内存中的数据集快照写入磁盘,它恢复时是将快照文件直接读到内存中。
redis的父进程会(fork)一个子进程来进行持久化,会先将一个数据写入临时文件中,等到持久化过程结束了,再用这个临时文件替换掉上一次持久化好的文件。在这个过程中,父进程是不进行任何IO操作的,它只要处理客户端的请求即可,这就包准了极高的性能,如果要进行大规模的数据恢复,且对于数据恢复的完整性不是非常敏感,那么rdb方式要比aof方式更加高效。

 默认的就是.rdb,一般情况下不需要去修改这个配置!

快照持久化

也叫RDB持久化方式。就是通过拍摄快照的方式来实现持久化,将某个时间的内存数据存储在一个rdb文件中。在redis服务重新启动的时候会加载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 ""

save 900 1   #900秒内至少有1个key被更改就执行快照
save 300 10  #300内描述至少有10个key被更改就执行快照
save 60 10000  #60秒内至少有10000个key被更改就执行快照

# 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 ./ #快照文件存储的位置

在这里插入图片描述

  • 进入redis目录,如果发现目录下有dump.rdb文件就进行删除。

在这里插入图片描述

  • 启动我们的redis,进去添加几个值,并退出

在这里插入图片描述

  • 这时候我们再次查看我们的redis目录下,又重新生成了一个dump.rdb文件,这个文件就是我们快照的备份文件,里面存放着我们刚才修改的数据。
    在这里插入图片描述

  • 再次启动redis,会发现数据还存在,这是因为在我们启动redis的时候,redis会自动检测加载自己目录下的dump.rdb文件中的数据。
    在这里插入图片描述

  • 再次关闭并把备份的dump.rdb文件进行删除
    在这里插入图片描述

  • 再次启动,发现我们的数据没有了,这是因为redis重新启动加载不到我们的dump.rdb文件,数据不存在了。
    在这里插入图片描述

会发现有一个dump.rdb文件
在这里插入图片描述

  • 进行清空所有数据库命令(flushall),会发现,redis自动也会帮我们生成一个dump.rdb文件

在这里插入图片描述

快照持久化原理

1.save命令
在redis运行中,我们可以显示的发送一条save命令来拍摄快照。save命令是阻塞命令,也就是当服务器接收了一条save命令之后就会开始拍摄快照,在此期间不会再去处理其他的请求,其他请求会被挂起直到备份结束

在这里插入图片描述
在这里插入图片描述
2.bgsave命令
bgsave命令也是立即拍摄快照,有别于save命令,bgsave并不是一条阻塞命令,而是fork一个子线程,然后这个子线程负责备份操作。而父进程继续处理客户端的请求,这样就不会造成阻塞了。
在这里插入图片描述3.根据配置文件(redis.conf)默认快照

save 900 1   #900秒内至少有1个key被更改就执行快照
save 300 10  #300内描述至少有10个key被更改就执行快照
save 60 10000  #60秒内至少有10000个key被更改就执行快照

4.shutdown命令
当我们只想shutdown命令的时候。服务器会自动发送一条save命令来完成快照操作。并在完成备份操作后关闭服务器。所以我们当我们的操作不满足前面三种情况的时候关闭服务器后,再次打开我们的数据也不会丢失。

rdb文件触发产生规则

  • save的规则满足情况下,会自动触发rdb规则!(conf文件中的bgsave规则)
  • 执行flushall命令,也会自动触发rdb规则!(空的rdb文件)
  • 退出redis后,也会产生一个rdb文件!(数据备份)

如何恢复rdb文件!

  • 只要将dump.rdb文件放在redis启动目录下就可以,redis启动的时候会自动检测dump.rdb恢复其中的数据!
  • 查看我们需要存放的位置,把.rdb文件存放在该目录下即可。
    在这里插入图片描述

.RDB文件的优点和缺点

优点:

  • 适合大规模的数据恢复!
  • 对数据完整性要求不高!
    缺点:
  • 如果我们的redis出现意外宕机了这个最后一次修改的数据就没有了!
  • fork进程的时候,会占用一定的内容空间!
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨某人的快乐学习日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值