欢迎光临
我们一直在努力

redis学习8---持久化相关测试AOF方式

1、shutdown服务或者杀掉进程测试数据是否丢失

关闭RDB持久化,启动AOF持久化,重启redis服务。

设置值

127.0.0.1:6379> mset k1 v1 k2 v2

OK

127.0.0.1:6379> keys *

1) “k2”

2) “k1”

127.0.0.1:6379> get k1 

“v1”

127.0.0.1:6379> get k2

“v2”

shutdown服务

127.0.0.1:6379> shutdown

启动redis服务

查看数据还在

redis-cli -a Redis2019!

Warning: Using a password with ‘-a’ option on the command line interface may not be safe.

127.0.0.1:6379> keys *

1) “k2”

2) “k1”

杀掉redis进程,启动redis服务

查看值还在

redis-cli -a Redis2019!

Warning: Using a password with ‘-a’ option on the command line interface may not be safe.

127.0.0.1:6379> keys *

1) “k2”

2) “k1”

2、测试服务异常导致appendonly.aof 文件乱码

AOF持久化是把操作都写进了文件appendonly.aof 

查看文件

cat appendonly.aof 

*2

$6

SELECT

$1

0

*5

$4

mset

$2

k1

$2

v1

$2

k2

$2

v2

编辑文件,填写内容代替文件错乱

vi appendonly.aof 

sfsbdd

1213fns

*2

$6

SELECT

$1

0

*5

$4

mset

$2

k1

$2

v1

$2

k2

$2

v2

dshfs

sdfksh5&

khdfjsj%$$

oguduog7&*

重启redis服务,访问redis发现失败

redis-cli -a Redis2019!

Warning: Using a password with ‘-a’ option on the command line interface may not be safe.

Could not connect to Redis at 127.0.0.1:6379: Connection refused

Could not connect to Redis at 127.0.0.1:6379: Connection refused

使用fix命令修复文件

[root@master1 data]# redis-check-aof –fix appendonly.aof 

0x              45: Expected prefix ‘*’, got: ‘d’

AOF analyzed: size=114, ok_up_to=69, diff=45

This will shrink the AOF from 114 bytes, with 45 bytes, to 69 bytes

Continue? [y/N]: y

Successfully truncated AOF

查看文件发现已经修复

cat appendonly.aof 

*2

$6

SELECT

$1

0

*5

$4

mset

$2

k1

$2

v1

$2

k2

$2

v2

启动redis服务,访问redis查看数据没有问题

redis-cli -a Redis2019!

Warning: Using a password with ‘-a’ option on the command line interface may not be safe.

127.0.0.1:6379> keys *

1) “k1”

2) “k2”

127.0.0.1:6379> del k1 

(integer) 1

127.0.0.1:6379> del k2

(integer) 1

127.0.0.1:6379> mset k5 v5 k6 v6

OK

127.0.0.1:6379> keys *

1) “k5”

2) “k6”

编辑redis.conf,打开RDB持久化

重启redis服务,访问redis,发现数据还是只有AOF持久化的数据,并没有之前RDB持久化的数据,正好证明了同时打开两种持久化配置的情况下会首先使用AOF持久化的数据。

redis-cli -a Redis2019! 

Warning: Using a password with ‘-a’ option on the command line interface may not be safe.

127.0.0.1:6379> keys *

1) “k6”

2) “k5”

3、删除所有数据,利用AOF的特点进行修复

删除数据,停止服务

127.0.0.1:6379> flushall

OK

127.0.0.1:6379> shutdown

编辑文件

vi data/appendonly.aof 

*2

$6

SELECT

$1

0

*5

$4

mset

$2

k1

$2

v1

$2

k2

$2

v2

*2

$6

SELECT

……

SELECT

$1

0

*1

$8

flushall

删除最后一行 flushall

启动redis服务

发现数据恢复

redis-cli -a Redis2019! 

Warning: Using a password with ‘-a’ option on the command line interface may not be safe.

127.0.0.1:6379> keys *

1) “k6”

2) “k5”

参考:

https://blog.csdn.net/qq_33101675/article/details/80631992

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。