博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
峰Redis学习(8)Redis 持久化AOF方式
阅读量:4309 次
发布时间:2019-06-06

本文共 3403 字,大约阅读时间需要 11 分钟。

第三节:Redis 的持久化之AOF 方式      

AOF方式:将以日志,记录每一个操作
 

优势:安全性相对RDB方式高很多;

劣势:效率相对RDB方式低很多;

 

1)AOF方式需要配置:

# 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 no默认关闭aof方式 我们修改成yes 就开启

下面那个是默认的aof文件名

# If unsure, use "everysec".appendfsync always# appendfsync everysec# appendfsync no

这里是三种同步策略:

always 是 只要发生修改,立即同步 (推荐实用 安全性最高)

everysec 是 每秒同步一次

no是不同步 

我们修改成always

 

2)保存退出,重新启动redis,然后随便加几个key

[root@bogon redis]# ll总用量 52drwxr-xr-x. 2 root root   134 3月  31 20:51 bin-rw-r--r--. 1 root root   102 4月   1 11:44 dump.rdb-rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf[root@bogon redis]# bin/redis-server redis.conf [root@bogon redis]# bin/redis-cli127.0.0.1:6379> keys *(empty list or set)127.0.0.1:6379> set test1 111OK127.0.0.1:6379> set test 222OK127.0.0.1:6379> shutdown savenot connected> exit[root@bogon redis]# ll总用量 56-rw-r--r--. 1 root root    88 4月   1 11:49 appendonly.aofdrwxr-xr-x. 2 root root   134 3月  31 20:51 bin-rw-r--r--. 1 root root   100 4月   1 11:49 dump.rdb-rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf[root@bogon redis]# rm -rf dump.rdb [root@bogon redis]# ll总用量 52-rw-r--r--. 1 root root    88 4月   1 11:49 appendonly.aofdrwxr-xr-x. 2 root root   134 3月  31 20:51 bin-rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf

这里就有一个appendonly.aof文件;

 

3)将appendonly.aof剪切到/root/目录下,启动后,发现数据没了:

[root@bogon redis]# ll总用量 52-rw-r--r--. 1 root root    88 4月   1 11:49 appendonly.aofdrwxr-xr-x. 2 root root   134 3月  31 20:51 bin-rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf[root@bogon redis]# mv appendonly.aof /root/[root@bogon redis]# cd /root/[root@bogon ~]# ll总用量 1528-rw-------. 1 root root    1269 3月  31 23:56 anaconda-ks.cfg-rw-r--r--. 1 root root      88 4月   1 11:49 appendonly.aofdrwxrwxr-x. 6 root root    4096 7月  28 2017 redis-3.2.10-rw-r--r--. 1 root root 1550261 7月  29 2017 redis-3.2.10.tar.gz[root@bogon ~]# cd /usr/local/redis/[root@bogon redis]# bin/redis-server redis.conf [root@bogon redis]# bin/redis-cli127.0.0.1:6379> keys *(empty list or set)

4)aof方式恢复数据:将/root/appendonly.aof拷贝至/usr/local/redis/下面,再重新启动redis:

127.0.0.1:6379> keys *(empty list or set)127.0.0.1:6379> shutdownnot connected> exit[root@bogon redis]# ll总用量 52-rw-r--r--. 1 root root     0 4月   1 11:52 appendonly.aofdrwxr-xr-x. 2 root root   134 3月  31 20:51 bin-rw-r--r--. 1 root root    77 4月   1 11:54 dump.rdb-rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf[root@bogon redis]# rm -rf appendonly.aof [root@bogon redis]# cd /root/[root@bogon ~]# ll总用量 1528-rw-------. 1 root root    1269 3月  31 23:56 anaconda-ks.cfg-rw-r--r--. 1 root root      88 4月   1 11:49 appendonly.aofdrwxrwxr-x. 6 root root    4096 7月  28 2017 redis-3.2.10-rw-r--r--. 1 root root 1550261 7月  29 2017 redis-3.2.10.tar.gz[root@bogon ~]# cp appendonly.aof /usr/local/redis/[root@bogon ~]# cd /usr/local/redis/[root@bogon redis]# ll总用量 56-rw-r--r--. 1 root root    88 4月   1 11:55 appendonly.aofdrwxr-xr-x. 2 root root   134 3月  31 20:51 bin-rw-r--r--. 1 root root    77 4月   1 11:54 dump.rdb-rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf[root@bogon redis]# bin/redis-server redis.conf [root@bogon redis]# bin/redis-cli127.0.0.1:6379> keys *1) "test"2) "test1"

我们发现 又有数据了

 

小结: 我们平时可以把aof文件定期备份 然后需要的时候 拷贝到redis下 重启即可;

 

 

转载于:https://www.cnblogs.com/tenWood/p/8686148.html

你可能感兴趣的文章
plsql if
查看>>
LuoGu P2002 消息扩散
查看>>
linux 下安装JDK
查看>>
简单的ASP.NET无刷新分页
查看>>
宏定义学习
查看>>
omitting directory `folder/'
查看>>
JavaScript面试题
查看>>
TCollector
查看>>
我的博客网站开发6——博文关键字搜索
查看>>
vim7.1在windows下的编码设置[转]
查看>>
同步器之Exchanger
查看>>
IO流
查看>>
专家观点:即使在云中 硬件同样至关重要
查看>>
loadrunner11录制不成功解决方法(收集)
查看>>
jQuery 基础
查看>>
USE平台构件属性无法显示的一种解决办法
查看>>
Cannot open include file: jni.h: No such file or directory解决方法
查看>>
HDU-水饺基情 二维树状数组
查看>>
visual studio 安装Entity framework失败
查看>>
测试对bug如何分析和定位
查看>>