redis误操作flushall或者flushdb

发布时间:2018-11-20 19:06:03 阅读:1177次

https://blog.csdn.net/jiangshubian/article/details/77883642

https://blog.csdn.net/qq_25551295/article/details/48103245

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

命令介绍:

FLUSHALL [ASYNC]

Available since 1.0.0.

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]

Available since 1.0.0.

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"

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


运维的同学,可能会碰到这样的情况,在redis不小心执行了flushdb或者flushall的操作,此时是不是打算辞职走人了?

下面来讲一下,redis执行了flushdb或者flishall之后的“后悔药”操作:

(1)先看一下我们现在redsi中已经有的数据

(2)此时我们执行了flushall的操作(flushdb的操作也是一样的)

(3)我们首先执行关闭的不存储操作:shutdown nosave

(3)杀掉redis的进程:pkill -9 redis

(4)修改redis的aof文件,删除到最后执行的flushall相关的命令(我的文件是 /var/rdb/appendonly6379.aof 文件)

(5)重启redis,(./bin/redis-server ./redis.conf) (./bin/redis-cli)

如图,我们执行flushall之前的数据,已经恢复回来了,思路其实很简单,手动将aof文件的flushall命令删除,然后重启redis就会自动加载了

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询
下一篇:redis信息相关

转载请注明:redis误操作flushall或者flushdb 出自老鄢博客 | 欢迎分享