温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-03-02 17:33:14 来源:网络 阅读:276 作者:断臂人 栏目:关系型数据库

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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI