RDB文件命名为dump.rdb
只记录数据,
AOF文件命名为appendonly.aof
记录数据记录操作过程,
文件大小AOF>>RDB
两个都开启时恢复顺序AOF>RDB。
默认的Redis关闭了AOF持久化,我们需要在config中打开:
找到appendonly
改为yes
,
找到appendfsync
按照官方推荐选everysec
配置文件中关于appendfsync
的选择说明如下
# 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
这里配置好后重启后会丢失数据,因为恢复顺序AOF>RDB而AOF刚刚开启还没有记录数据。
redis-cli
auth "你的密码"
bgrewriteaof
重启Redis即可。
(这里注意如果是刚安装redis需要从rdb文件迁移数据的话需要先停止运行redis,将备份rdb文件复制过去在启动,因为在redis运行时复制rdb文件当redis停止时会将内存中的数据转移到磁盘造成覆盖从而无法恢复)
总结:想要开启AOF->命令行登录redis->输入bgrewriteaof
->重启redis。同理如果想从dump.rdb文件恢复数据,也可手动触发AOF重写bgrewriteaof
待恢复后重新开启。
bgrewriteaof
AOF重写官方说明
Syntax
BGREWRITEAOF
Available since:1.0.0Time complexity:O(1)ACL categories:@admin
, @slow
, @dangerous
Instruct Redis to start an Append Only File rewrite process. The rewrite will create a small optimized version of the current Append Only File.
If BGREWRITEAOF
fails, no data gets lost as the old AOF will be untouched.
The rewrite will be only triggered by Redis if there is not already a background process doing persistence.
Specifically:
- If a Redis child is creating a snapshot on disk, the AOF rewrite is scheduled but not started until the saving child producing the RDB file terminates. In this case the
BGREWRITEAOF
will still return a positive status reply, but with an appropriate message. You can check if an AOF rewrite is scheduled looking at theINFO
command as of Redis 2.6 or successive versions. - If an AOF rewrite is already in progress the command returns an error and no AOF rewrite will be scheduled for a later time.
- If the AOF rewrite could start, but the attempt at starting it fails (for instance because of an error in creating the child process), an error is returned to the caller.
Since Redis 2.4 the AOF rewrite is automatically triggered by Redis, however the BGREWRITEAOF
command can be used to trigger a rewrite at any time.
Please refer to the persistence documentation for detailed information.
1 条评论
555