Redis的持久化之AOF方式

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


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

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


配置:

[[email protected] redis]# vi redis.conf 


编辑redis.conf


往下拉 找到:

Redis的持久化之AOF方式

appendonly no默认关闭aof方式 我们修改成yes 就开启

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

再往下拉:

Redis的持久化之AOF方式

这里是三种同步策略:

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

everysec 是 每秒同步一次

no是不同步 


我们修改成always


然后保存 退出;


我们重新启动redis,然后随便加几个key

Redis的持久化之AOF方式


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


aof方式恢复数据


我们先重置数据

[[email protected] redis]# rm -rf dump.rdb 

[[email protected] redis]# ll

总用量 48

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root 46698 7月  18 12:14 redis.conf


启动redis

[[email protected] redis]# ./bin/redis-server ./redis.conf 

[[email protected] redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

(empty list or set)

目前数据库是空


添加数据

127.0.0.1:6379> set n1 1

OK

127.0.0.1:6379> set n2 2

OK

127.0.0.1:6379> set n3 3

OK

127.0.0.1:6379> shutdown nosave

not connected> exit

[[email protected] redis]# ll

总用量 52

-rw-r--r--. 1 root root   107 7月  18 12:17 appendonly.aof

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root 46698 7月  18 12:14 redis.conf

[[email protected] redis]# 


我们把aof文件剪切到其他地方去 然后启动试下

[[email protected] redis]# mv appendonly.aof /root/

[[email protected] redis]# ll

总用量 48

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root 46698 7月  18 12:14 redis.conf

[[email protected] redis]# ./bin/redis-server ./redis.conf 

[[email protected] redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

(empty list or set)

没数据;


我们再把aof文件复制回来;

[[email protected] redis]# cp /root/appendonly.aof /usr/local/redis/

cp:是否覆盖"/usr/local/redis/appendonly.aof"? y

[[email protected] redis]# ll

总用量 52

-rw-r--r--. 1 root root   107 7月  18 12:22 appendonly.aof

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root 46698 7月  18 12:14 redis.conf

[[email protected] redis]# ./bin/redis-server ./redis.conf 

[[email protected] redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

1) "n1"

2) "n3"

3) "n2"


我们发现 以及有数据了


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