(五)redis持久化

來源:互聯網
上載者:User

標籤:redis持久化   save   bgrewriteaof   rdb   aof   

redis 提供了不同層級的持久化方式:

RDB持久化方式能夠在指定的時間間隔對資料進行快照儲存。

AOF持久化方式記錄每次對伺服器寫的操作,當伺服器重啟的時候會重新執行這些命令來恢複原始的資料。

如果資料不太重要,那麼不需要任何持久化操作

如果兩種持久化方式都使用,那麼當redis重啟的時候會優先載入AOF檔案來恢複原始的資料。因為在通常情況下AOF檔案儲存的資料集要比RDB檔案儲存的資料集要完整。


RDB的優點

RDB在儲存檔案時,是由redis 子進程完成的,父進程不需要再做其他IO操作

與AOF相比,恢複大的資料集時,RDB方式更快一些。

RDB可以根據時間進行儲存,能儲存不同版本的資料。

RDB的缺點

RDB是根據不同save條件進行儲存的,所以可能資料並不是即時儲存,如果資料庫異常宕機會遺失資料


[[email protected] ~]# redis-cli -p 6378127.0.0.1:6378> auth redisOK127.0.0.1:6378> config get dbfilename        #這些參數檔案可以在/etc/redis/6378.conf 中找到1) "dbfilename"                #這個參數表示save時的檔案名稱2) "dump.rdb"127.0.0.1:6378> config get dir1) "dir"                      #這個參數指定了save時的檔案位置2) "/var/lib/redis/6378"127.0.0.1:6378> config get save1) "save"                     #save參數是觸發RDB的關鍵參數2) "900 1 300 10 60 10000"    #表示每900秒如果發生一次資料更改,則發生備份。                              #每300秒發生10次資料更改則發生備份。                              #每60秒發生了10000次資料更改,則發生備份。127.0.0.1:6378> quit[[email protected] ~]# ls /var/lib/redis/6378/appendonly.aof  dump.rdb[[email protected] ~]# file /var/lib/redis/6378/dump.rdb /var/lib/redis/6378/dump.rdb: data           #rdb檔案是一個二進位的檔案,使用strings命令可以查看[[email protected] ~]# strings /var/lib/redis/6378/dump.rdbREDIS0006abcd123             #從這裡我們推斷這是一個值[[email protected] ~]# redis-cli -p 6378                   127.0.0.1:6378> auth redisOK127.0.0.1:6378> set v1 www.oracle.com       #設定v1的值OK127.0.0.1:6378> get v1"www.oracle.com"127.0.0.1:6378> save              #save和bgsave都可以出發一次手工RDBOK127.0.0.1:6378> bgsaveBackground saving started127.0.0.1:6378> quit[[email protected] ~]# strings /var/lib/redis/6378/dump.rdbREDIS0006www.oracle.com           #這個是剛才我們設定的v1的值abcd123


AOF的優點

這種方式的持久化資料將更準確,可以設定每秒鐘進行記錄。


AOF的缺點

相應的AOF的方式會佔用更大的磁碟空間


日誌重寫

因為AOF的運作方式是不斷地將命令追加到檔案的末尾,所以隨著寫入命令的不斷增加,AOF檔案的體積也會變得越來越大。

如果對一個計數器調用了100次INCR,那麼僅僅為了儲存這個計算機的當前值,AOF檔案就需要使用100條記錄。而實際上,只使用一條SET命令已經足以儲存計數器的當前值了,其餘99條記錄實際上都是多餘的。

為了處理這種情況,redis支援一種有趣的特性:可以在不打斷服務用戶端的情況下,對AOF檔案進行重建(rebuild)。執行bgrewriteaof命令,redis將產生一個新的AOF檔案,這個檔案包含重建當前資料集所需的最少命令。Redis2.4之後可以自動觸發AOF重寫。

[[email protected] ~]# redis-cli -p 6378                   127.0.0.1:6378> auth redisOK127.0.0.1:6378> config get appendfilename(empty list or set)127.0.0.1:6378> config get appendonly 1) "appendonly"2) "yes"127.0.0.1:6378> config get appendfsync1) "appendfsync"     #aof的fsync追加方式,有三種可選,每秒,每條命令,從不2) "everysec"        #這裡的是每秒鐘fsync


下面通過幾個例子來測試一下前面的說法

[[email protected] ~]# sed -i ‘s/^requirepass/#requirepass/g‘ /etc/redis/6378.conf  [[email protected] ~]# redis-cli -p 6378127.0.0.1:6378> auth redisOK127.0.0.1:6378> shutdown127.0.0.1:6378> quit[[email protected] ~]# grep ‘^save‘ /etc/redis/6378.confsave 900 1save 300 10save 60 10000[[email protected] ~]# sed -i ‘/^save/s/^/#/g‘ /etc/redis/6378.conf    #關掉rdb[[email protected] ~]# grep ‘^save‘ /etc/redis/6378.conf           [[email protected] ~]# grep ‘#save‘ /etc/redis/6378.conf #save 900 1#save 300 10#save 60 10000[[email protected] ~]# grep ‘^appendonly‘ /etc/redis/6378.conf              appendonly yes[[email protected] ~]# sed -i ‘/^appendonly/s/yes/no/g‘ /etc/redis/6378.conf    #關掉AOF[[email protected] ~]# grep ‘^appendonly‘ /etc/redis/6378.conf              appendonly no[[email protected] ~]# /etc/init.d/redis_6378 startStarting Redis server...[[email protected] ~]# redis-cli -p 6378127.0.0.1:6378> keys *    #發現有很多值,這裡可能因為redis在啟動的時候讀取了RDB檔案。1) "v1"2) "ke1"3) "s3"4) "k1"5) "s1"6) "s2"127.0.0.1:6378> flushdb   #重新整理之後就沒有值了OK127.0.0.1:6378> keys *(empty list or set)[[email protected] ~]# rm -rf /var/lib/redis/6378/*    #把這個檔案刪掉[[email protected] 6378]# cd /var/lib/redis/6378/[[email protected] 6378]# ls -lrttotal 0[[email protected] ~]# /etc/init.d/redis_6378 restartStopping ...Redis stoppedStarting Redis server...[[email protected] ~]# redis-cli -p 6378             127.0.0.1:6378> keys *(empty list or set)127.0.0.1:6378> set ename kingOK127.0.0.1:6378> set sal 5000OK127.0.0.1:6378> save      #手工做一次RDBOK127.0.0.1:6378> quit[[email protected] 6378]# ls -lrttotal 4-rw-r--r-- 1 root root 40 Oct 25 00:36 dump.rdb[[email protected] ~]# /etc/init.d/redis_6378 restartStopping ...Redis stoppedStarting Redis server...[[email protected] ~]# redis-cli -p 6378             #如果有RDB檔案,並且沒有開啟AOF,會自動讀取RDB檔案中的內容127.0.0.1:6378> keys *1) "sal"2) "ename"[[email protected] 6378]# grep ‘^appendonly‘ /etc/redis/6378.conf     #開啟AOF功能appendonly no[[email protected] 6378]# sed -i ‘/^appendonly/s/no/yes/g‘ /etc/redis/6378.conf[[email protected] 6378]# grep ‘^appendonly‘ /etc/redis/6378.conf              appendonly yes[[email protected] ~]# /etc/init.d/redis_6378 restartStopping ...Redis stoppedStarting Redis server...[[email protected] ~]# redis-cli -p 6378             127.0.0.1:6378> keys *(empty list or set)127.0.0.1:6378> set sal 5000OK127.0.0.1:6378> quit[[email protected] ~]# /etc/init.d/redis_6378 restart      Stopping ...Redis stoppedStarting Redis server...[[email protected] ~]# redis-cli -p 6378            #這裡也能說明,如果AOF功能開啟的情況下,redis不會讀取RDB檔案。127.0.0.1:6378> keys *1) "sal"


本文出自 “名字長一點會好記” 部落格,請務必保留此出處http://xiaoyiyi.blog.51cto.com/1351449/1705994

(五)redis持久化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.