1、shutdown服務(wù)或者殺掉進(jìn)程測(cè)試數(shù)據(jù)是否丟失
創(chuàng)新互聯(lián)建站的客戶來(lái)自各行各業(yè),為了共同目標(biāo),我們?cè)诠ぷ魃厦芮信浜希瑥膭?chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對(duì)我們的要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。專業(yè)領(lǐng)域包括成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、電商網(wǎng)站開發(fā)、微信營(yíng)銷、系統(tǒng)平臺(tái)開發(fā)。
關(guān)閉RDB持久化,啟動(dòng)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
啟動(dòng)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進(jìn)程,啟動(dòng)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、測(cè)試服務(wù)異常導(dǎo)致appendonly.aof 文件亂碼
AOF持久化是把操作都寫進(jìn)了文件appendonly.aof?
查看文件
cat appendonly.aof?
*2
$6
SELECT
$1
0
*5
$4
mset
$2
k1
$2
v1
$2
k2
$2
v2
編輯文件,填寫內(nèi)容代替文件錯(cuò)亂
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ù),訪問(wèn)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
啟動(dòng)redis服務(wù),訪問(wèn)redis查看數(shù)據(jù)沒有問(wèn)題
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ù),訪問(wèn)redis,發(fā)現(xiàn)數(shù)據(jù)還是只有AOF持久化的數(shù)據(jù),并沒有之前RDB持久化的數(shù)據(jù),正好證明了同時(shí)打開兩種持久化配置的情況下會(huì)首先使用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的特點(diǎn)進(jìn)行修復(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
啟動(dòng)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