当前位置:网站首页>Redis数据持久化
Redis数据持久化
2022-06-13 04:01:00 【嘟嘟的程序员铲屎官】
本篇博文目录:
1.什么是数据持久化
Redis所有数据保存在内存中,如果不做任何持久化操作当Redis关闭时数据会丢失,数据持久化就是为了防止数据丢失,Redis会异步将数据保存在硬盘上。

2.数据持久化方式
- RDB(快照方式)

- AOF(写日志方式)

3.Redis数据持久化-RDB
Redis将数据已二进制形式保存到磁盘中,保存的文件后缀为rdb,下次启动的时候载入rdb文件恢复数据,从而实现持久化。

(1) 触发RDB的方式
- 自动方式
通过在配置文件进行相关配置,Redis根据配置的情况进行自动持久化。
RDB的配置项信息:

进入到配置文件所在目录,并通过Vim命令进行编辑
RDB配置信息如下:
详细配置信息:
save 60 1
dbfilename dbdump.rdb
dir /usr/local/redis-6.0.6/
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
备注:save 60 1 表示60s内有一个key变化就保存,这是一种策略,同时可以设置多个策略,如下:

启动Redis服务端:
通过RDM中在db2数据库添加一条key = Java value=best!的数据

我们会发现Redis目录下多了一个dbdump.rdb的文件

通过Vim 查看dbdump.rdb文件,会发现里面有数据,但是是乱码,可以理解因为Redis通过二进制进行保存
.
将Redis服务端关闭,然后再次启动观察数据是否依然存在:
我们会发现数据任然存在,数据持久化成功!
- 手动方式
当要进行持久化的时候,输入save或bgsave命令进行持久化(
都是单线程阻塞,意思就是在执行保存的时候不能执行其他命令),save和bgsave命令本质上差不多,一个同步运行一个在异步运行,但都是单线程阻塞。


(2) RDB的缺点

4.Redis数据持久化-AOF
使用日志的方式追加保存我们每一步操作的命令到磁盘中,这里实际保存的格式并不是命令格式,而是一种特定的格式,文件后缀为AOF,数据恢复的时候将AOF文件载入。
(1) 日志追加- AOF

(2) 日志恢复

(3) AOF的追加(fsync)策略
appendfsync 追加策略(如:appendfsync everysec)

(4) AOF三种方式对比

(5) AOF重写
只关心最后的状态,保存当前最新状态所执行的命令,例如下表中的set hello world ,set hello java 和set hello hehe命令最后的状态为set hello hehe命令所执行的状态,所以这三条命令只保存set hello hehe命令,其他命令也是同样的道理,所以最后AOF重写后保存的命令为set hello hehe ,set counter 2 和rpush mylist a b c这三条命令。

(6) AOF的实现方式
- 自动方式(使用AOF重写配置方式)
AOF重写配置选项如下:
先关闭Redis服务端:
然后通过vim命令编辑Redis的配置文件,新增Aof的配置:
详细配置信息:
appendonly yes
appendfilename aof-6666.aof
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
再次启动Redis服务端:
通过RDM添加往数据库2添加一条数据key=PHP value=Java
我们会发现Redis目录下多了一个aof-6666.aof的文件
我们通过vim命令查看该文件:
关于rewrite说明
- 手动方式(使用bgrewriteaof命令方式)

5.RDB与AOF的选择
( AOF的加载优先级相较于RDB更高 )
边栏推荐
- 单片机:Modbus 通信协议介绍
- LVS四層負載均衡集群(3)集群功能分類 - HPC
- [Yugong series] June 2022 Net architecture class 080 master cluster and database switching of distributed middleware schedulemaster
- Introduction to RFM analysis
- MCU: pcf8591 hardware interface
- [笔记]vs2015 编写汇编masm32之使用MASM32库
- 2022春学期总结
- Example of try catch finally execution sequence
- 高等数学(第七版)同济大学 习题1-3 个人解答
- EGO planner论文翻译
猜你喜欢

Use the visual studio code terminal to execute the command, and the prompt "because running scripts is prohibited on this system" will give an error

2022春学期总结

单片机:A/D(模数转换)的主要指标

ET框架-22 创建ServerInfo实体及事件

leetcode.1 --- 两数之和

Principle, composition and functions of sensors of Dajiang UAV flight control system
![[note]vs2015 compilation of masm32 using MASM32 Library](/img/f5/b47336af248d1b485208ec3f9ca12b.png)
[note]vs2015 compilation of masm32 using MASM32 Library

Single chip microcomputer: infrared remote control communication principle

单片机:NEC 协议红外遥控器
![[zeloengine] localization process /imgui Chinese culture](/img/ec/7eb7edc236b09994c2981e6f13dc68.png)
[zeloengine] localization process /imgui Chinese culture
随机推荐
【LeetCode】860. Change with lemonade (2 brushes for wrong questions)
[web] cookies and sessions
十億數據量 判斷元素是否存在
Single chip microcomputer: pcf8591 application program
解答私信@田田WX //2022-6-12 C语言 51单片机LED模拟交通灯
Use lodash to merge the values of the same fields in an array object
单片机外设介绍:温度传感器 DS18B20
Lambda end operation count
MCU: EEPROM multi byte read / write operation sequence
Lambda终结操作查找与匹配findFirst
[Yugong series] June 2022 Net architecture class 080 master cluster and database switching of distributed middleware schedulemaster
1.4.2 Capital Market Theroy
R: Airline customer value analysis practice
UE4 learning notes - functions of terrain tool
Redis-HyperLogLog-基数统计算法
Goframe day 4
MSG messages in ROS
[test development] basic concepts related to testing
Interpretation of usb-if bc1.2 charging protocol
OKR和KPI的区别