mset k1 v1 k2 v2OK127.0.0.1:6379> keys *..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

redis學(xué)習(xí)8---持久化相關(guān)測試AOF方式

發(fā)布時間:2020-03-02 17:33:14 來源:網(wǎng)絡(luò) 閱讀:292 作者:斷臂人 欄目:關(guān)系型數(shù)據(jù)庫

1、shutdown服務(wù)或者殺掉進程測試數(shù)據(jù)是否丟失


關(guān)閉RDB持久化,啟動AOF持久化,重啟redis服務(wù)。


設(shè)置值

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服務(wù)

127.0.0.1:6379> shutdown


啟動redis服務(wù)


查看數(shù)據(jù)還在

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服務(wù)


查看值還在

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、測試服務(wù)異常導(dǎo)致appendonly.aof 文件亂碼

AOF持久化是把操作都寫進了文件appendonly.aof?


查看文件

cat appendonly.aof?

*2

$6

SELECT

$1

0

*5

$4

mset

$2

k1

$2

v1

$2

k2

$2

v2


編輯文件,填寫內(nèi)容代替文件錯亂

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服務(wù),訪問redis發(fā)現(xiàn)失敗

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命令修復(fù)文件

[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


查看文件發(fā)現(xiàn)已經(jīng)修復(fù)

cat appendonly.aof?

*2

$6

SELECT

$1

0

*5

$4

mset

$2

k1

$2

v1

$2

k2

$2

v2


啟動redis服務(wù),訪問redis查看數(shù)據(jù)沒有問題

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服務(wù),訪問redis,發(fā)現(xiàn)數(shù)據(jù)還是只有AOF持久化的數(shù)據(jù),并沒有之前RDB持久化的數(shù)據(jù),正好證明了同時打開兩種持久化配置的情況下會首先使用AOF持久化的數(shù)據(jù)。

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、刪除所有數(shù)據(jù),利用AOF的特點進行修復(fù)


刪除數(shù)據(jù),停止服務(wù)

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服務(wù)


發(fā)現(xiàn)數(shù)據(jù)恢復(fù)

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問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI