redis持久化-RDB

redis持久化-RDB

文档

  1. redis单机安装
  2. redis常用的五种数据类型
  3. redis数据类型-位图bitmap
  4. redis数据类型-基数统计HyperLogLog
  5. redis数据类型-地理空间GEO
  6. redis数据类型-流Stream
  7. redis数据类型-位域bitfield

官方文档

  1. 官网操作命令指南页面:https://redis.io/docs/latest/commands/?name=get&group=string
  2. Redis persistence
  3. SAVE
  4. BGSAVE

RDB

说明
  1. redis版本:7.0.0
  2. RDB:redis database,redis数据库
  3. 根据配置的规则,每隔一定的时间,将内存中的数据集快照,也就是snapshot内存快照,写入磁盘,恢复时,再将硬盘中的快照文件直接读回到内存中
  4. 快照文件被称为RDB文件,全称dump.rdb
持久化配置
redis-6.0.19的相关配置
  1. 解压目录下redis.conf文件中,相关的配置

    properties 复制代码
    ################################ 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 behavior 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
  2. 配置表示,满足以下条件,将触发RDB持久化

    • 上次保存后,如果有1个键发生变化,900秒后保存

    • 上次保存后,如果有10个键发生变化,300秒后保存

    • 上次保存后,如果有10000个键发生变化,60秒后保存

redis-7.0.0的相关配置
  1. 解压目录下redis.conf文件中,相关的配置

    properties 复制代码
    ################################ SNAPSHOTTING  ################################
    
    # Save the DB to disk.
    #
    # save <seconds> <changes> [<seconds> <changes> ...]
    #
    # Redis will save the DB if the given number of seconds elapsed and it
    # surpassed the given number of write operations against the DB.
    #
    # Snapshotting can be completely disabled with a single empty string argument
    # as in following example:
    #
    # save ""
    #
    # Unless specified otherwise, by default Redis will save the DB:
    #   * After 3600 seconds (an hour) if at least 1 change was performed
    #   * After 300 seconds (5 minutes) if at least 100 changes were performed
    #   * After 60 seconds if at least 10000 changes were performed
    #
    # You can set these explicitly by uncommenting the following line.
    #
    # save 3600 1 300 100 60 10000
Snapshotting官方文档说明

Snapshotting

By default Redis saves snapshots of the dataset on disk, in a binary file called dump.rdb. You can configure Redis to have it save the dataset every N seconds if there are at least M changes in the dataset, or you can manually call the SAVE or BGSAVE commands.

For example, this configuration will make Redis automatically dump the dataset to disk every 60 seconds if at least 1000 keys changed:

复制代码
save 60 1000

This strategy is known as snapshotting.

  • 保存快照,可自动触发,或手动调用命令
自动触发保存快照
  1. 找到启动服务使用的配置文件,通常将安装目录下的redis.conf复制到指定路径做为启动文件。修改配置文件,设置保存快照触发条件

    properties 复制代码
    # save 3600 1 300 100 60 10000
    save 300 5 60 10 
    • 上次保存后,如果有5次变更,300秒后保存
    • 上次保存后,如果有10次变更,60秒后保存
  2. 修改后重启redis

修改dump文件的保存路径
  1. 找到启动服务使用的配置文件,通常将安装目录下的redis.conf复制到指定路径做为启动文件。修改配置文件,设置自定义保存路径,需提前创建好指定路径,默认路径:./

    pro 复制代码
    # 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 ./
    dir /opt/module/redis/myredis/dumpfiles
  2. 修改后重启redis

修改dump文件名称
  1. 找到启动服务使用的配置文件,通常将安装目录下的redis.conf复制到指定路径做为启动文件。修改配置文件,设置自定义文件名称,默认名称:dump.rdb

    properties 复制代码
    # The filename where to dump the DB
    # dbfilename dump.rdb
    dbfilename dump6379.rdb
    • 可以标记服务器IP、服务端口号等
  2. 修改后重启redis

获取配置信息命令
  1. config get requirepass:获取密码
  2. config get port:获取端口号
  3. config get dir:获取RDB文件保存路径
恢复快照记录

每次重启服务都是一次恢复的过程,下面模拟恢复指定快照文件dump6379.rdb

  1. 启动redis,在redis中添加几条数据
  2. 查询当前存在的keykeys *
  3. 关闭redis,shutdown
  4. dump6379.rdb文件重命名为dump6379.rdb.bak,模拟已将快照文件保存到其它位置,此时该路径下无dump6379.rdb文件
  5. 启动redis,此时查询当前存在的key,无数据。因为没有快照文件,所以数据不存在
  6. 关闭redis
  7. 此时会生成新的dump6379.rdb文件,删除此文件。将dump6379.rdb.bak文件重命名为dump6379.rdb,模拟将备份的快照文件放回指定的目录下
  8. 启动redis,此时查询当前存在的key,有值,说明数据已经恢复
手动触发保存快照
官方说明

The SAVE commands performs a synchronous save of the dataset producing a point in time snapshot of all the data inside the Redis instance, in the form of an RDB file.

You almost never want to call SAVE in production environments where it will block all the other clients. Instead usually BGSAVE is used. However in case of issues preventing Redis to create the background saving child (for instance errors in the fork(2) system call), the SAVE command can be a good last resort to perform the dump of the latest dataset.

相关命令
  1. save:save命令会阻塞其它客户端,生产环境一般不使用
  2. bgsave:Save the DB in background。子进程进行保存快照的操作,主进程继续处理客户端的请求
  3. lastsave:查看最后保存快照的时间,返回时间戳,linux系统可以使用date命令对时间戳进行格式化:date -d @1746104405
RDB的优点
官方说明

RDB advantages

  • RDB is a very compact single-file point-in-time representation of your Redis data. RDB files are perfect for backups. For instance you may want to archive your RDB files every hour for the latest 24 hours, and to save an RDB snapshot every day for 30 days. This allows you to easily restore different versions of the data set in case of disasters.
  • RDB is very good for disaster recovery, being a single compact file that can be transferred to far data centers, or onto Amazon S3 (possibly encrypted).
  • RDB maximizes Redis performances since the only work the Redis parent process needs to do in order to persist is forking a child that will do all the rest. The parent process will never perform disk I/O or alike.
  • RDB allows faster restarts with big datasets compared to AOF.
  • On replicas, RDB supports partial resynchronizations after restarts and failovers.
总结
  1. 适合大规模的数据恢复
  2. 按照业务定时备份。定时保存RDB文件,一般应保存到其它服务器上,可形成不同的数据版本,需要恢复时,可以按需选择对应的版本
  3. 对数据完整性和一致性要求不高
  4. RDB文件在内存中的加载速度要比AOF快得多
RDB的缺点
官方说明

RDB disadvantages

  • RDB is NOT good if you need to minimize the chance of data loss in case Redis stops working (for example after a power outage). You can configure different save points where an RDB is produced (for instance after at least five minutes and 100 writes against the data set, you can have multiple save points). However you'll usually create an RDB snapshot every five minutes or more, so in case of Redis stopping working without a correct shutdown for any reason you should be prepared to lose the latest minutes of data.
  • RDB needs to fork() often in order to persist on disk using a child process. fork() can be time consuming if the dataset is big, and may result in Redis stopping serving clients for some milliseconds or even for one second if the dataset is very big and the CPU performance is not great. AOF also needs to fork() but less frequently and you can tune how often you want to rewrite your logs without any trade-off on durability.
总结
  1. 如果redis不是正确的关闭,则未达到保存点的数据可能会丢失
  2. RDB需要经常fork()子进程用来保存快照,如果数据集很大fork()可能会很耗时,并且如果数据集非常大并且CPU性能不好,则可能导致Redis停止为客户端服务几毫秒甚至一秒钟
检查并修复RDB文件
  1. 进入redis安装目录下的bin目录/opt/module/redis/bin

    shell 复制代码
    cd /opt/module/redis/bin
  2. 操作redis-check-rdb,检查并修复RDB文件

    shell 复制代码
    ./redis-check-rdb /opt/module/redis/myredis/dumpfiles/dump6379.rdb
触发保存快照的场景
  1. 达到配置文件中设置的保存快照触发条件
  2. 手动执行savebgsave命令
  3. 执行flushall命令
  4. 正确执行shutdown命令,且没有设置开启AOF持久化
  5. 主从复制时,主节点自动触发
禁用快照
  • 客户端执行命令禁用快照

    shell 复制代码
    config set save ""
  • 修改redis.conf配置文件禁用快照

    properties 复制代码
    # Snapshotting can be completely disabled with a single empty string argument
    # as in following example:
    #
    # save ""
    save ""
    
    # 注释掉其它的save配置
    # save 3600 1 300 100 60 10000
其它RDB配置

找到启动服务使用的配置文件,通常将安装目录下的redis.conf复制到指定路径做为启动文件,修改配置文件

  1. stop-writes-on-bgsave-error,默认yes,表示,如果后台保存执行失败,将停止接受写入操作

    shell 复制代码
    stop-writes-on-bgsave-error yes
  2. rdbcompression,默认yes,表示,使用LZF算法进行压缩存储

    shell 复制代码
    rdbcompression yes
  3. rdbchecksum,默认yes,表示,使用CRC64算法进行数据校验

    shell 复制代码
    rdbchecksum yes
  4. rdb-del-sync-files,默认noyes表示,在未启用持久化的情况下删除复制使用的RDB文件

    shell 复制代码
    rdb-del-sync-files no
相关推荐
云计算DevOps-韩老师1 小时前
跟韩学AiOps系列之2025学MySQL系列_如何在MySQL中开启和提交事务?!
数据库·mysql
herinspace3 小时前
管家婆易指开单如何设置零售开单
运维·服务器·数据库·软件工程·sass·零售
听闻风很好吃3 小时前
Redis应用场景实战:穿透/雪崩/击穿解决方案与分布式锁深度剖析
数据库·redis·分布式
编程在手天下我有4 小时前
从读写分离到分布式服务:系统架构演进十阶段深度解析
数据库·系统架构·微服务架构·分布式系统·互联网技术·技术架构优化
计算机毕设定制辅导-无忧学长4 小时前
ActiveMQ 可靠性保障:消息确认与重发机制(二)
数据库·activemq·java-activemq
264玫瑰资源库4 小时前
红鸟3D互动系统源码一键部署教程(含多个打包版本与功能解构)
java·数据库·游戏
烟锁池塘柳04 小时前
【无报错,亲测有效】如何在Windows和Linux系统中查看MySQL版本
数据库·mysql
怀君4 小时前
Flutter——数据库Drift开发详细教程(二)
android·数据库·flutter
溜溜刘@♞5 小时前
mysql--索引
数据库·mysql
码熔burning5 小时前
【MongoDB篇】MongoDB的数据库操作!
数据库·mongodb·nosql