redis 持久化 rdb 与 aof
一、rdb
redis 默认使用rdb 进行持久化,打开redis.con 配置文件就能查看其相关配置
################################ 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
save 【指定的时间间隔;时间单位是秒】 :【执行指令次数操作(对数据进行更新或者插入,删除)查询不算】
2、
rdb 文件名与存储位置设置
# 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 ./
3、rdb 数据文件回复,将dump.rdb 备份文件 复制到redis 存储rdb文件的位置即可
cp backup_dump.rdb dump.rdb
二、aof
redis 默认配置是不开启aof的,如果需要启动需要自己开启配置
############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.
appendonly no
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"
将appendonly yes 即可 appendfilename 可以修改 aof 持久化文件名
appendonly yes
aof appendfsync (aof 持久化模式,数据集改变追加到aof文件中)
# The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# 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
3种模式
appendfsync always 每次redis命令都会同步到aof 文件中
appendfsync everysec 每一秒(类似定时)会同步到aof 文件中(redis默认选项)
appendfsync no 由系统来处理同步
aof 重写机制
# Automatic rewrite of the append only file.
# Redis is able to automatically rewrite the log file implicitly calling
# BGREWRITEAOF when the AOF log size grows by the specified percentage.
#
# This is how it works: Redis remembers the size of the AOF file after the
# latest rewrite (if no rewrite has happened since the restart, the size of
# the AOF at startup is used).
#
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.
auto-aof-rewrite-percentage 100 #aof文件至少要达到64M才会自动重写
auto-aof-rewrite-min-size 64mb #oaf文件重写后文件大小增长到100% 才会触发重写
当然aof还可以手动重写;进入redis客户端执行命令bgrewriteaof重写aof
此时一定要先停止服务,避免其他人有写入操作,重写aof 前删除(appendonly.aof)所有flushall操作。
vim appendonly.aof
*1
$8
flushall
删除flushall三行,保存退出
三、redis 混合持久化
redis4.0 以上版本才支持;必须先开启aof
# When rewriting the AOF file, Redis is able to use an RDB preamble in the
# AOF file for faster rewrites and recoveries. When this option is turned
# on the rewritten AOF file is composed of two different stanzas:
#
# [RDB file][AOF tail]
#
# When loading Redis recognizes that the AOF file starts with the "REDIS"
# string and loads the prefixed RDB file, and continues loading the AOF
# tail.
aof-use-rdb-preamble yes #开启混合持久化开关
本文详细介绍了Redis的两种持久化方式:RDB(快照)和AOF(追加日志)。RDB在指定条件(如时间间隔、写操作次数)下创建数据快照,用于数据恢复;AOF则记录每次写操作,提供更高的数据安全性。AOF有三种fsync策略,控制数据同步到磁盘的频率。当AOF文件达到一定大小增长比例时,Redis会自动触发重写以压缩日志。同时,Redis 4.0以上版本支持混合持久化,结合RDB和AOF的优点。通过配置文件可启用和调整这些特性。
2739

被折叠的 条评论
为什么被折叠?



