redis学习--的持久化数据备份(RDB和AOF)
接上一篇:安装window下的redis,redis可视化管理工具(Redis Desktop Manager)安装,基础使用,实例化项目
一、dump.rdb文件是怎么生成的
二、什么是redis持久化
三、redis的RDB是什么?
四、redis配置文件redis.config相关配置
五、redis优点
六、redis缺点
redis比memcache作为缓存数据库强大的地方:(1)支持数据类型比较多,(2)redis持久化功能。
一、dump.rdb文件是怎么生成的
在redis服务挂掉的时候,根据redis的配置文件,会自动备份数据到本地。
dump.rdb是由redis服务器自动生成的。
默认情况下,每隔一段时间redis服务器程序会自动对数据库做一次遍历,把内存快照写在一个叫做“dump.rdb”文件里,这种持久化机制叫做SNAPSHOT。有了SNAPSHOT后,如果服务器宕机,重新启动redis服务器程序时redis会自动加载dump.rdb,将数据库恢复到上一次SNAPSHOT的状态。
至于多久一次做一次SNAPSHOT,SNAPSHOT文件的路径和文件名,你可以在redis的config文件中指定。
二、什么是redis的持久化
Redis提供了不同级别的持久化方式:
(1)RDB持久化方式:能够在指定的时间间隔能对你的数据进行快照存储。
(2)AOF持久化方式:每次对服务器写的操作,当服务器重启的时候回重新执行这些命令来恢复原始的数据,AOF命令以redis协议追加保存每次写的操作到文件末尾。redis还能对AOF文件进行后台重写,使得AOF文件的体积不至于过大。
(3)如果你只希望你的数据在服务器运行的时候存在,你可以不使用任何持久化方式。
(4)你也可以同时开启这两种持久化方式。当redis服务重启的时候回优先载入AOF文件来恢复原始的数据,因为在通常情况下AOF文件保存的数据集要比RDB文件保存的数据集要完整
三、Redis的RDB是什么
RDB在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是SNAPSHOT快照。
它恢复时是将快照文件直接写入到内存里,redis会单独创建(fork)一个子进程进行持久化吗,会先将数据写入到一个临时文件中,持久化过程都结束了,在用这个临时文件替换上从持久化好的文件。
整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能。如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是敏感,那RDB方式要比AOF方式更加高效。、
redis的缺点是最后一次持久化后的数据可能丢失。
四、redis配置文件redis.config相关配置
(一)RDB快照方式持久化磁盘
先看redis.window.config文件
################################ 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 ./
4.1如何触发RDB快照
配置文件中默认的快照配置
save 900 1
save 300 10
save 60 10000
上面的意思就是说:
(1)如果至少一个键改变,会在900秒(15分钟)之后执行save操作
(2)如果至少改变10个键,则在300秒(5分钟)之后执行save操作
(3)如果至少改变10000个键,则在60秒(1分钟)之后执行save操作
命令save:只管保存,其他不管
命令bgsave:redis会在后台异步进行快照操作,快照的同时还可以响应客户端请求。
4.2默认的RDB方式保存的是dump.rdb文件,恢复识别也是dump.rdb
4.3stop-writes-on-bgsave-error yes
如果后台保存到磁盘发生错误,将停止写操作,使用LZF压缩rdb文件,这会耗CPU, 但是可以减少磁盘占用。
4.4rdbcompression yes
保存rdb和加载rdb文件的时候校验,可以防止错误,但是要付出约10%的性能,可以关闭,提高性能。
4.5rdbchecksum yes
导出的rdb文件名
4.6dbfilename dump.rdb
设置工作目录,rdb文件会写到该目录,append only file也会存储在该目录下
4.7dir ./
redis会自动快照保存到磁盘或者调用bgsave,是后台进程完成的,其他客户端任然可以读写redis服务,后台保存快照到磁盘会占用大量的内存。
(二)AOF(append-only file)方式持久化
另外一种方式为递增的方式,将会引起数据变化的操作,持久化到文件中,重启redis的时候,通过操作命令,恢复数据。
每次执行写操作命令之后,都会将数据写到server.aofbuf中。
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec". # appendfsync always
appendfsync everysec
# appendfsync no # When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I/O against the disk, in some Linux configurations
# Redis may block too long on the fsync() call. Note that there is no fix for
# this currently, as even performing fsync in a different thread will block
# our synchronous write(2) call.
当配置为always的时候,每次server.aofbuf中的数据写入到文件之后,才会返回到客户端,这样可以保证数据不丢失,但是频繁的IO操作,会降低性能。
everysec每秒写一次,这可能会丢失一秒内的操作。
五、redis优点
(1)适合大规模的数据恢复
(2)对数据完整性和一致性要求不高
六、redis缺点
(1)在一定间隔时间做一次备份,所以redis意外的挂掉的话,就会丢失最后一次快照后的所有修改
(2)fork的时候,内存中的数据被被克隆一份,大致2倍的膨胀性需求考虑
redis学习--的持久化数据备份(RDB和AOF)的更多相关文章
- Redis提供的持久化机制(RDB和AOF)
Redis提供的持久化机制 Redis是一种面向"key-value"类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势.它虽然起步较晚,但发展却 ...
- Redis提供的持久化机制(RDB和AOF)【转载】
Redis提供的持久化机制 Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势.它虽然起步较晚,但发展却十分迅速. 近 ...
- Redis 学习笔记(四)RDB 和 AOF 持久化机制
一.Redis 持久化简介 Redis 的持久化功能是区别于 Memcached 显著特性,数据持久化可以保证系统在发生宕机和重启后数据不会丢失,对于 redis 这种存储在内存中的数据库显得尤为重要 ...
- Redis学习总结(1)——数据持久化
以前研究Redis的时候,很多东西都不太明白,理解得也不太深,现在有时间重新拾起来看看,将一些心得记录下来,希望和大家一起探讨. 一.简介 Redis是一个单线程高可用的Key-Value存储系统,和 ...
- redis持久化的方式RDB 和 AOF
redis持久化的方式RDB 和 AOF 一.对Redis持久化的探讨与理解 目前Redis持久化的方式有两种: RDB 和 AOF 首先,我们应该明确持久化的数据有什么用,答案是用于重启后的数据恢复 ...
- redis学习之——持久化RDB 和AOF
RDB: 在指定的时间间隔内将内存中的数据集快照写入磁盘, 也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里.rdb 保存的是dump.rdb文件 RDB工作原理: Redis会 ...
- Redis学习手册(持久化)
一.Redis提供了哪些持久化机制: 1). RDB持久化: 该机制是指在指定的时间间隔内将内存中的数据集快照写入磁盘. 2). AOF持久化: 该机制将以日志的形式记录服务 ...
- redis 学习笔记——持久化
redis持久化 snapshot数据快照(rdb) 这是一种定时将redis内存中的数据写入磁盘文件的一种方案,这样保留这一时刻redis中的数据镜像,用于意外回滚.redis的snapshot的格 ...
- 10分钟彻底理解Redis的持久化机制:RDB和AOF
作者:张君鸿 juejin.im/post/5d09a9ff51882577eb133aa9 什么是Redis持久化? Redis作为一个键值对内存数据库(NoSQL),数据都存储在内存当中,在处理客 ...
随机推荐
- MassTransit 实现发布/订阅
MassTransit 介绍 先看下masstransit 官网介绍:MassTransit 是一个自由.开源.轻量级的消息总线, 用于使用. NET 框架创建分布式应用程序.MassTransit ...
- GitHub 系列之「怎样使用 GitHub?」
1.写在前边的话,为什么要写CitHub? 跟朋友在交流的时候听到求职的时候发现有些公司要附Github帐号,一个优秀的 GitHub 账号当然能让你增色不少.自己之前听说过,但没有花时间研究,最后花 ...
- [Python接口自动化]从零开始学习python自动化(1):环境搭建
第一步:安装python编译环境 安装python编译环境之前,必须保证已安装jdk哈,如果为安装,请参考https://jingyan.baidu.com/article/6dad5075d1dc4 ...
- MyBatis 中@param 的用法
示例: 接口方法 public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(valu ...
- bzoj5248 [2018多省省队联测]一双木棋
直接hash+爆搜即可. #include <cstdio> #include <cstring> #include <iostream> #include < ...
- 查询APP Store已发布过的版本记录
1.国内APP使用 酷传 搜索,即可查询到版本记录 2.国外的APP 无法通过国内软件进行搜索,可在App store中直接查询.步骤如下:
- koa+mysql+vue+socket.io全栈开发之前端篇
React 与 Vue 之间的对比,是前端的一大热门话题. vue 简易上手的脚手架,以及官方提供必备的基础组件,比如 vuex,vue-router,对新手真的比较友好:react 则把这些都交给社 ...
- 【Azkaban搭建】---Azkaban 3.25.0搭建细则 超实用
一.前述 Azkaban是一个工作流调度工具,因为需要各个任务之间有依赖关系,传统的Crontab 任务已经不能满足. 所以需要建立一套工作流引擎.相比Ooize来说,Azkaban的优势是作为一个客 ...
- Python调用ansible API系列(二)执行adhoc和playbook
执行adhoc #!/usr/bin/env python # -*- coding: utf-8 -*- import sys from collections import namedtuple ...
- Hive使用必知必会系列
一.Hive的几种数据模型 内部表 (Table 将数据保存到Hive 自己的数据仓库目录中:/usr/hive/warehouse) 外部表 (External Table 相对于内部表,数据不在自 ...