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

Redis配置檔案(2)SNAPSHOTTING快照/APPEND ONLY MODE追加

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 that's set to 'yes' as it's 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: don't 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

 

再次重新啟動: