redis的AOF恢复数据
- 启用AOF
- 使用AOF文件恢复数据
步骤一:启用AOF
127.0.0.1:6379> config set appendonly yes
OK
127.0.0.1:6379> CONFIG GET appendonly
1) "appendonly"
2) "yes"
127.0.0.1:6379> CONFIG rewrite
OK
[root@redis170 ~]# ls /var/lib/redis/
appendonly.aof dump.rdb
[root@redis170 ~]# wc -l /var/lib/redis/appendonly.aof
1 /var/lib/redis/appendonly.aof
[root@redis170 ~]# !redis
redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> mset x 1 y 2 c 3
OK
127.0.0.1:6379> exit
[root@redis170 ~]# wc -l /var/lib/redis/appendonly.aof
21 /var/lib/redis/appendonly.aof
步骤二:使用AOF文件恢复数据
1)备份aof文件
redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> CONFIG GET appendonly
1) "appendonly"
2) "no"
127.0.0.1:6379> CONFIG set appendonly yes
OK
127.0.0.1:6379> exit
[root@redis170 ~]# ll /var/lib/redis/
总用量 8
-rw-r--r-- 1 redis redis 92 5月 24 11:35 appendonly.aof
-rw-r--r-- 1 redis redis 92 5月 24 11:34 dump.rdb
127.0.0.1:6379> mset x 1 y 2 z 3
OK
127.0.0.1:6379> keys *
1) "x"
2) "z"
3) "y"
127.0.0.1:6379> exit
[root@redis170 ~]# cp /var/lib/redis/appendonly.aof /opt/
2)删除数据
redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys *
1) "x"
2) "z"
3) "y"
127.0.0.1:6379> flushall
OK3)恢复数据[root@redis170 ~]# cp /opt/appendonly.aof /var/lib/redis/
cp:是否覆盖'/var/lib/redis/appendonly.aof'? y
[root@redis170 ~]# systemctl start redis
[root@redis170 ~]# !redis
redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys *
1) "z"
2) "y"
3) "x"