1. 程式人生 > >Redis配置文件(2)SNAPSHOTTING快照/APPEND ONLY MODE追加

Redis配置文件(2)SNAPSHOTTING快照/APPEND ONLY MODE追加

span mode 實戰 ssi ever write 數據完整性 數據安全 view

redis.conf文件

1.Save a. save 秒鐘 寫操作次數
RDB是整個內存的壓縮過的Snapshot,RDB的數據結構,可以配置復合的快照觸發條件,
默認
是1分鐘內改了1萬次,
或5分鐘內改了10次,
或15分鐘內改了1次。

182 # Save the DB on disk: 184 # save <seconds> <changes> 186 # Will save the DB if both the given number of seconds and the given 187 # number of write operations against the DB occurred. 189 # In the example below the behaviour will be to save: 190 # after 900 sec (15 min) if at least 1 key changed 191 # after 300 sec (5 min) if at least 10 keys changed 192 # after 60 sec if at least 10000 keys changed

b.如果想禁用RDB持久化的策略,只要不設置任何save指令,或者給save傳入一個空字符串參數也可以

194 #   Note: you can disable saving completely by commenting out all "save" lines.
195 #
196 #   It is also possible to remove all the previously configured save
197 #   points by adding a save directive with a single empty string argument
198 #   like in the following example:
199 #
200 #   save ""
202 save 900 1 203 save 300 10 204 save 60 10000
set key value1

save
此時立馬執行,形成最新的dump文件

stop-writes-on-bgsave-error 如果配置成no,表示你不在乎數據不一致或者有其他的手段發現和控制
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue
to work as usual even if there are problems with disk, # permissions, and so forth. stop-writes-on-bgsave-error yes

yes:出錯了就立即停止

rdbcompression rdbcompression:對於存儲到磁盤中的快照,可以設置是否進行壓縮存儲。如果是的話,redis會采用 LZF算法進行壓縮。如果你不想消耗CPU來進行壓縮的話,可以設置為關閉此功能
# Compress string objects using LZF when dump .rdb databases?
# For default thats set to yes as its almost always a win.
# If you want to save some CPU in the saving child set it to no but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes

dbfilename

默認生成的rdb文件名

# The filename where to dump the DB
dbfilename dump.rdb

dir

# Note that you must specify a directory here, not a file name.
dir ./

實戰測試:

修改配置文件

200 #   save ""
202 save 900 1
203 save 120 10
204 save 60 10000

兩分鐘10次操作

技術分享圖片

之後會生成一個dump.rdb文件,具體生成文件的默認名請修改:dbfilename

進行復制(備份)

技術分享圖片

模擬事故:

此時直接刪除所有的數據

技術分享圖片

此時再次重新登陸則是會顯示為空,即是存在dump.rdb文件

技術分享圖片

在管不redis時,迅速斬斷,保存文件dump.rdb

技術分享圖片

此時刪除之前的dump.rdb文件,並且把之前的dump_cp.rdb備份文件復制一份命名為dump.rdb 此時又可以進行之前的keys的值獲取

技術分享圖片

APPEND ONLY MODE追加

appendonly:默認開關狀態,可以同時和RDB一起開著

# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.

appendonly no
默認是關著的

appendfilename:默認的文件名

# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"

appendfsync

# no: dont fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.
always:同步持久話每次發生數據變更立即記錄到磁盤,性能比較差但是數據完整性好
everysec:出場默認的推薦的,異步操作,每秒記錄,如果一秒內宕機,有數據丟失

# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
# If unsure, use "everysec".
# appendfsync always
appendfsync everysec

no-appendfsync-on-rewrite:重寫時是否可以運用Appendfsync, 用默認no即可,保證數據安全性。
# If you have latency problems turn this to "yes". Otherwise leave it as
# "no" that is the safest pick from the point of view of durability.
no-appendfsync-on-rewrite no

auto-aof-rewrite-min-size:設置重寫的基準值 auto-aof-rewrite-percentage:設置重寫的基準值
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

實戰測試: aof測試 首先開啟aof測試
# Please check http://redis.io/topics/persistence for more information.
appendonly yes

此時已經生成文件,aof自動開啟

技術分享圖片

創造性的意外事件

技術分享圖片

查看appendonly.aof 發現已經記下了我們寫的命令 記錄了我們執行的每一條命令

技術分享圖片

重新啟動 裏面的內容為空

技術分享圖片

刪除appendonly.aof 的最後一句

技術分享圖片

此時重新啟動 獲取到我們之前的內容

技術分享圖片

假設出現下面的情況: appendonly.aof文件出錯:可能是網絡,斷電.......

技術分享圖片

此時RDB和AOF文件都在

技術分享圖片

此時啟動Redis

技術分享圖片

在dump.rdb文件完整的情況下,appendonly.aof文件出錯,說明後者的優先級大於前者

那麽此時的處理方式是:

redis-check-aof --fix appendonly.aof

技術分享圖片

再次重新啟動:

技術分享圖片

Redis配置文件(2)SNAPSHOTTING快照/APPEND ONLY MODE追加