Redis 持久化 v1.0 文章作者:哇塞大嘴好帥(哇塞大嘴好帅)

Redis 持久化

作者:哇塞大嘴好帥(哇塞大嘴好帅)

1.RDB (Redis DataBase)

​ Redis是内存数据库,如果不能将内存中的数据库保存到硬盘,那么一旦退出或者机房断电等症状,数据就会丢失。

在指定的时间间隔将内存中的数据写入磁盘。

**Redis会单独创建(fork)一个线程进行持久化(快照,数据恢复的时候就是将快照文件直接读到内存中),会先将数据写入到一个临时文件中,等持久化过程都结束了,在用这个临时文件替换上次持久化好的文件。**主进程不会进行任何IO操作,这样就保证了性能。如果需要进行大规模数据的回复,且对于数据回复的完整性不是非常灵敏,拿RDB方式要比AOF方式更加高效。RDB的缺点是最后一次持久化的数据可能丢失。

rbd保存的文件是 dump.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

1.1rdb持久化测试

##############################
# 持久化测试
#############################
# 如果60秒进行了5次修改
save 60 5
# save 900 1
# save 300 10
# save 60 10000

插入5个数据进行尝试

127.0.0.1:6379> set k1  v1
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> 粓ET
(error) ERR unknown command `粓ET`, with args beginning with: 
127.0.0.1:6379> SET K3 V4
OK
127.0.0.1:6379> SET K4 K5
OK
127.0.0.1:6379> SET V5 K4
OK
127.0.0.1:6379> KEYS *
1) "K3"
2) "a"
3) "k2"
4) "K4"
5) "c"
6) "k1"
7) "V5"
8) "b"
127.0.0.1:6379> SET K8 7
OK

这时候我们查看此目录 --这时候会发现多了一个dump.rdb

[root@VM-0-3-centos redis-6.0.7]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  dump.rdb  INSTALL  Makefile  MANIFESTO  README.md  redis.conf  runtest  runtest-cluster  runtest-moduleapi  runtest-sentinel  sentinel.conf  src  tests  TLS.md  utils

1.2数据恢复

先把上一个测试的Redis持久化文件删除

 rm -rf redis.conf 

手动持久化并且关机

127.0.0.1:6379> set a 12
OK
127.0.0.1:6379> set b 12
OK
127.0.0.1:6379> save
OK
127.0.0.1:6379> set c 13
OK
127.0.0.1:6379> SHUTDOWN
not connected> exit

开启Redis

redis-cli -p 6379

查询数据

127.0.0.1:6379> keys *		
1) "c"
2) "a"
3) "b"
127.0.0.1:6379> get a
"12"

1.3持久化触发机制

1.save规则满足的情况下

2.执行 flushdb

3.执行 flushall

4.退出redis

5.手动输入save

1.4恢复rdb文件

把rdb文件拖入配置文件设置的rdb目录即可

优点

​ 适合大规模数据恢复

缺点

​ 需要一定时间间隔进程操作

​ fork进程的时候需要一定内存。

2.AOF(Append Only File)

​ 记录每个写操作,只追加文件但不修改文件,redis重新启动之前会读取aof文件重新构造数据。

配置文件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 yes		# 是否开启aof如果开启改成yes不开启改成no

# The name of the append only file (default: "appendonly.aof")

appendfilename "appendonly.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		# 不进行

# 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.
#
# In order to mitigate this problem it's possible to use the following option
# that will prevent fsync() from being called in the main process while a
# BGSAVE or BGREWRITEAOF is in progress.
#
# This means that while another child is saving, the durability of Redis is
# the same as "appendfsync none". In practical terms, this means that it is
# possible to lose up to 30 seconds of log in the worst scenario (with the
# default Linux settings).
#
# If you have latency problems turn this to "yes". Otherwise leave it as
# "no" that is the safest pick from the point of view of durability.

no-appendfsync-on-rewrite no		# 是否开启重写规则

# 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				# 超过原大小的百分之百
auto-aof-rewrite-min-size 64mb				# 如果文件大于64mb了就会fork就会一个新的进程来将文件进行重写

我们进appenonly

vim appenonly.aof

查看appendonly就可以查询我们的写操作

*2
$6
SELECT
$1
0
*3
$3
set
$1
a
$2
12
*3
$3
set
$1
b
$2
12

注意:

​ 如果aof文件有错误,redis是启动不起来的

[root@VM-0-3-centos redis-6.0.7]# redis-server redis.conf 
18558:C 09 Sep 2020 21:24:21.467 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18558:C 09 Sep 2020 21:24:21.467 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=18558, just started
18558:C 09 Sep 2020 21:24:21.467 # Configuration loaded
[root@VM-0-3-centos redis-6.0.7]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused

如果aof文件遭到了恶意修改等特殊原因我们可以对他使用redis–check-aof进行尝试修复

redis-check-aof --fix appendonly.aof

控制台

[root@VM-0-3-centos redis-6.0.7]# redis-check-aof --fix appendonly.aof
0x              33: Expected \r\n, got: 6161
AOF analyzed: size=100, ok_up_to=51, diff=49
This will shrink the AOF from 100 bytes, with 49 bytes, to 51 bytes
Continue? [y/N]: y
Successfully truncated AOF

优点:

​ 每次修改都同步,数据会完整

​ 如果设置一秒同步一次,可能会丢失一秒数据

​ 如果不同步,效率更高

缺点:

​ AOF文件会非常大

​ Aof运行效率低比rdb慢,

2.1 持久化触发机制

​ Redis会记录上次重写时的AOF大小,默认配置是当AOF文件大小是上次rewrite后大小的100%且文件大于64M时触发

2.2 持久化测试

127.0.0.1:6379> set a 12
OK
127.0.0.1:6379> set b 13
OK

正常退出redis,查看多出来的appendonly.aof

[root@VM-0-3-centos redis-6.0.7]# ls
00-RELEASENOTES  appendonly.aof  BUGS  CONTRIBUTING  COPYING  deps  dump.rdb  INSTALL  Makefile  MANIFESTO  README.md  redis.conf  runtest  runtest-cluster  runtest-moduleapi  runtest-sentinel  sentinel.conf  src  tests  TLS.md  utils
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哇塞大嘴好帅(DaZuiZui)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值