redis误操作flushall或者flushdb

来源:互联网 发布:人工智能元年 编辑:程序博客网 时间:2024/06/08 10:34

redis是基于内容的nosql,在平时使用中,如果不小心执行了FLUSHALL或者FLUSHDB,那么是否意味着会丢失所有数据?其实不一定,如果开启了appendonly数据备份,还是能够找回相关数据的。


命令介绍:

FLUSHALL [ASYNC]

Delete all the keys of all the existing databases, not just the currently selected one. This command never fails.

The time-complexity for this operation is O(N), N being the number of keys in all existing databases.











FLUSHDB [ASYNC]

Delete all the keys of the currently selected DB. This command never fails.

The time-complexity for this operation is O(N), N being the number of keys in the database.








假设我们已经开启了appendonly数据备份机制。

删除数据前:

192.168.18.247:6489> keys *
1) "gdbbwy20170915112124299"
2) "key1"
3) "gdbbwy20170913711534690"
4) "xorDest"
5) "andDest"
6) "key:__rand_int__"
7) "key2"


192.168.18.247:6489> FLUSHALL
OK
192.168.18.247:6489> keys *
(empty list or set)


在appendonly文件中查看命令执行记录:

[root@rd2DevServer13 6489]# tail appendonly6489.aof 
$7
andDest
*2
$6
SELECT
$1
0
*1
$8
FLUSHALL


紧急恢复方式:

1)关闭redis服务

192.168.18.247:6489> SHUTDOWN

2)删除appendonly中的删除语句

sed -i 's/FLUSHALL//g' appendonly.aof

3)重启服务


192.168.18.247:6489> keys *
1) "gdbbwy20170915112124299"
2) "key1"
3) "gdbbwy20170913711534690"
4) "xorDest"
5) "andDest"
6) "key:__rand_int__"
7) "key2"


原来数据原封不动回来了。

原创粉丝点击