Redis开启AOF持久化数据丢失问题
程序员文章站
2022-07-13 22:46:34
...
一、过程演示
登录redis,并写入测试数据
redis-cli -h 172.16.254.124 -p 6379 -a redis
172.16.254.124:6379> set handong dongge
OK
172.16.254.124:6379> set city1 beijing1
OK
172.16.254.124:6379> set city2 beijing2
OK
172.16.254.124:6379> set city3 beijing3
OK
172.16.254.124:6379> set city4 beijing4
OK
查看测试数据
172.16.254.124:6379> keys *
1) "city3"
2) "city4"
3) "handong"
4) "city2"
5) "city1"
编辑配置文件redis.conf,开启AOF,并重启redis
查看所有key
172.16.254.124:6379> keys *
(empty list or set)
发现先前写入的测试数据都丢失了。
这是由于启用AOF之后,重启Redis,默认就会从aof恢复数据,就会忽略dump.rdb中的数据,就会造成重启之前数据的丢失
。
那如何避免开启AOF之后数据丢失呢?
写入测试数据:
172.16.254.124:6379> set city1 beijing1
OK
172.16.254.124:6379> set city2 beijing2
OK
172.16.254.124:6379> set city3 beijing3
OK
172.16.254.124:6379> set city4 beijing4
OK
172.16.254.124:6379> keys *
1) "city2"
2) "city4"
3) "city3"
4) "city1"
在redis-cli执行bgrewriteaof
172.16.254.124:6379> bgrewriteaof
Background append only file rewriting started
编辑配置文件redis.conf,开启AOF,并重启redis
查看数据
172.16.254.124:6379> keys *
1) "city1"
2) "city2"
3) "city4"
4) "city3"