您好,登錄后才能下訂單哦!
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。