Redis学习——Redis持久化之RDB备份方式保存数据

Redis学习——Redis持久化之RDB备份方式保存数据

从这一个介绍里面知道,redis比memcache作为缓存数据库强大的地方,一个是支持的数据类型比较多,另一个就是redis持久化功能。
下面就介绍Redis的持久化之RDB!

一:什么是redis的持久化
官网介绍:
英文:https://redis.io/topics/persistence
中文:http://www.redis.cn/topics/persistence.html

二:Redis的RDB是什么?

在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里,Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到。

一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能。如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺点是最后一次持久化后的数据可能丢失。

备注解释:

--fork的作用是复制一个与当前进程一样的进程。新进程的所有数据(变量、环境变量、程序计数器等)数值都和原进程一致,但是是一个全新的进程,并作为原进程的子进程

--AOF方式,点击这里查看[待补充]!

三:Redis配置文件redis.conf中关于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
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 ./

如果上面的配置文件有看不懂,请点击这里进行想关的查看了解:Redis配置文件-redis.conf详解

1:如何触发RDB快照
– 配置文件中默认的快照配置

save 900 1
save 300 10
save 60 10000

    命令save或者是bgsave
    SAVE:save时只管保存,其它不管,全部阻塞
    BGSAVE:Redis会在后台异步进行快照操作,快照同时还可以响应客户端请求。可以通过lastsave
    命令获取最后一次成功执行快照的时间

注:执行flushall命令,也会产生dump.rdb文件,但里面是空的,无意义

2:默认RDB方式保存的是dump.rdb文件,恢复也是识别的是dump.rdb

3:配置位置,以及快照恢复
查看目录

CONFIG GET dir获取目录
###将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可  或者就在当前目录启动

举例:
我的redis启动服务的目录是 /usr/local/bin 下面
我启动redis的目录是/root 下面,然后生成的的dump.rdb 文件也是在/root 目录下,假如redis服务器出现问题,挂掉了。那么想要根据rdb恢复数据
(1)将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务
(2)当前目录启动

如果我的dump.rdb 在/root下面,而我到/usr/local/bin这个目录下去启动了redis,那么数据是无法恢复的。只能从 /root 下面启动才能看到之前保存的数据。
如下操作:

127.0.0.1:6379> CONFIG GET dir  #获取当前操作的目录
1) "dir"
2) "/root"
127.0.0.1:6379> KEYS *  #redis中存在的key
 1) "myhash"
 2) "k3"
 3) "mylist"
 4) "b1"
 5) "du1"
 6) "k1"
 7) "b4"
 8) "key1"
 9) "d"
10) "myset"
11) "du11"
12) "list"
13) "b3"
14) "du"
15) "b2"
16) "skey"
17) "k2"
127.0.0.1:6379> 

下面我关闭redis,假设redis服务挂掉!

127.0.0.1:6379> SHUTDOWN  #关闭服务器

[root@localhost ~]# pwd  #当前目录是/root
/root
[root@localhost ~]# ll  #下面有dump.rdb这个文件
总用量 52
-rw-------. 1 root root  1208 6月  14 08:10 anaconda-ks.cfg
drwxr-xr-x. 3 root root  4096 6月  17 04:35 dufy
-rw-r--r--. 1 root root   283 6月  19 00:13 dump.rdb
-rw-r--r--. 1 root root 24772 6月  14 08:10 install.log
-rw-r--r--. 1 root root  7690 6月  14 08:09 install.log.syslog

那么当我进入/usr/local/bin 目录下启动重新启动redis,看数据是否恢复

[root@localhost ~]# cd /usr/local/bin/
[root@localhost bin]# pwd
/usr/local/bin
[root@localhost bin]# redis-server /root/dufy/redis/redis-3.0.4/redis.conf
[root@localhost bin]# redis-cli
127.0.0.1:6379> KEYS *   # 这里启动后,查看key没有恢复
(empty list or set)
127.0.0.1:6379> 

那么我再次关闭服务,从/root下启动redis看数据是否恢复

127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@localhost bin]# cd /root/
[root@localhost ~]# pwd
/root
[root@localhost ~]# redis-server /root/dufy/redis/redis-3.0.4/redis.conf
[root@localhost ~]# redis-cli
127.0.0.1:6379> KEYS *  #重启后,查看key,发现恢复成功了!
 1) "k1"
 2) "b1"
 3) "key1"
 4) "list"
 5) "du11"
 6) "du1"
 7) "b4"
 8) "k3"
 9) "myhash"
10) "b3"
11) "d"
12) "skey"
13) "mylist"
14) "du"
15) "k2"
16) "b2"
17) "myset"
127.0.0.1:6379> 

相信我这些操作,也讲明白快照恢复,上面说的这一句:将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可或者就在当前目录启动。

四:Redis优点

 适合大规模的数据恢复

对数据完整性和一致性要求不高

五:Redis缺点

    在一定间隔时间做一次备份,所以如果redis意外down掉的话,就会丢失最后一次快照后的所有修改
    -
    fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑

六:如何停止RDB
1: 配置文件注释掉

    save 900 1
    save 300 10
    save 60 10000

    启动 # save “”, 去掉 #。保存后重启
2:动态停止RDB命令
   在redis-cli中执行:

config set save ""

七:大总结

  内存中的数据对象 --->rdbsave --> 磁盘中的rdb文件
  内存中的数据对象 <---rdload <-- 磁盘中的rdb文件

    RDB是一个非常紧凑的文件
    RDB在保存RDB文件时父进程唯一需要做的就是folk出一个子进程,接下来工作全部交给子进程来做,父进程不需要再做其他IO操作,所以RDB持久化方式可以最大化redis的性能
    与AOF相比,在恢复大的数据时候,RDB方式更快一些
    数据丢失风险大
    RDB需要经常folk子进程来保存数据集到磁盘,当数据集比较大额时候,folk的过程是比较耗时的,可能会导致redis在一些毫秒级不能响应客服端请求

原文:https://blog.csdn.net/u010648555/article/details/73433717

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/112661.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 弗洛伊德算法—–最短路径算法(一)

    弗洛伊德算法—–最短路径算法(一)学习此算法的原因:昨天下午遛弯的时候,碰到闺蜜正在看算法,突然问我会不会弗洛伊德算法?我就顺道答应,然后用了半个小时的时间,学习了此算法,并用5分钟讲解给她听,在此也分享给各位需要的朋友,让你们在最短的时间内,透彻的掌握该算法。RobertW.Floyd(罗伯特弗洛伊德)1962年在“CommunicationoftheACM”上发表了该算法,同年StephenWarsha…

    2022年6月4日
    382
  • github加速插件

    github加速插件在chrome或edge的插件库里搜索github加速,安装后访问github,和在github上下东西都很快

    2025年6月15日
    0
  • Jenkins Gitlab持续集成打包平台搭建

    Jenkins Gitlab持续集成打包平台搭建

    2022年3月2日
    58
  • navate15激活码【最新永久激活】

    (navate15激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~AE…

    2022年3月28日
    79
  • linux连接Redis客户端

    linux连接Redis客户端linux命令下载redis-stable#官网下载,这里使用wget直接下载的[linux]$wgethttp://download.redis.io/redis-stable.tar.gz#解压[linux]$tar-xzvfredis-stable.tar.gz#进入解压目录[linux]$cdredis-stable#编译[linux]$make#拷贝入bin目录[linux]$cpsrc/redis-cli/usr/local/bin/验证redi

    2022年5月5日
    52
  • pycharm怎么配置tensorflow环境_ensp详细安装步骤

    pycharm怎么配置tensorflow环境_ensp详细安装步骤Tensorflow详细安装步骤及PyCharm配置Tensorflow是谷歌开源的深度学习框架,分为两个版本,GPU和CPU,主要的区别在于计算速度,GPU版本要比CPU计算速度更快,适用于处理大量复杂的数据,但需要计算机配置独立NVIDIA显卡。CPU版本没有显卡要求,安装更简单,合适新手小白和学生党,下面介绍CPU版本Tensorflow的详细安装步骤系统环境:Windows10第一步:安装Anaconda两种方式:直接在Anaconda官方网站下载,但速度很慢;建议第二种,选择镜像网站下载,

    2022年8月26日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号