[TOC]
# 什么是redis的持久化
官網介紹:
英文:https://redis.io/topics/persistence
中文:http://www.redis.cn/topics/persistence.html
# Redis的RDB是什么
在指定的時間間隔內將內存中的數據集快照寫入磁盤,也就是行話講的Snapshot快照,它恢復時是將快照文件直接讀到內存里,Redis會單獨創建(fork)一個子進程來進行持久化,會先將數據寫入到。
一個臨時文件中,待持久化過程都結束了,再用這個臨時文件替換上次持久化好的文件。整個過程中,主進程是不進行任何IO操作的,這就確保了極高的性能。如果需要進行大規模數據的恢復,且對于數據恢復的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺點是最后一次持久化后的數據可能丟失。
備注解釋:
--fork的作用是復制一個與當前進程一樣的進程。新進程的所有數據(變量、環境變量、程序計數器等)數值都和原進程一致,但是是一個全新的進程,并作為原進程的子進程
# Redis配置文件redis.conf中關于RDB的相關配置
首先貼出來配置信息
~~~
################################ SNAPSHOTTING ################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
#
# Note: you can disable saving completely by commenting out all "save" lines.
#
# It is also possible to remove all the previously configured save
# points by adding a save directive with a single empty string argument
# like in the following example:
#
# save ""
save 900 1
save 300 10
save 60 10000
# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# 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
# 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
# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes
# The filename where to dump the DB
dbfilename dump.rdb
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./
~~~
## 如何觸發RDB快照
- 配置文件中默認的快照配置
~~~
save 900 1
save 300 10
save 60 10000
~~~
save 900 1 #刷新快照到硬盤中,必須滿足兩者要求才會觸發,即900秒之后至少1個關鍵字發生變化。
save 300 10 #必須是300秒之后至少10個關鍵字發生變化。
save 60 10000 #必須是60秒之后至少10000個關鍵字發生變化。(以上3個選項都屏蔽,則rdb禁用)
~~~
stop-writes-on-bgsave-error yes #后臺存儲錯誤停止寫。
rdbcompression yes #使用LZF壓縮rdb文件。
rdbchecksum yes #存儲和加載rdb文件時校驗。
dbfilename dump.rdb #設置rdb文件名。
dir ./ #設置工作目錄,rdb文件會寫入該目錄。
~~~
命令save或者是bgsave
SAVE:save時只管保存,其它不管,全部阻塞
BGSAVE:Redis會在后臺異步進行快照操作,快照同時還可以響應客戶端請求。可以通過lastsave
命令獲取最后一次成功執行快照的時間
**注:執行flushall命令,也會產生dump.rdb文件,但里面是空的,無意義**
## 默認RDB方式保存的是dump.rdb文件,恢復也是識別的是dump.rdb
# 配置位置,以及快照恢復
查看目錄
CONFIG GET dir獲取目錄
**將備份文件 (dump.rdb) 移動到 redis 安裝目錄并啟動服務即可 或者就在當前目錄啟動**
舉例:
我的redis啟動服務的目錄是 /usr/local/bin 下面
我啟動redis的目錄是/root 下面,然后生成的的dump.rdb 文件也是在/root 目錄下,假如redis服務器出現問題,掛掉了。那么想要根據rdb恢復數據
(1)將備份文件 (dump.rdb) 移動到 redis 安裝目錄并啟動服務
(2)當前目錄啟動
如果我的dump.rdb 在/root下面,而我到/usr/local/bin這個目錄下去啟動了redis,那么數據是無法恢復的。只能從 /root 下面啟動才能看到之前保存的數據。
如下操作:
~~~
127.0.0.1:6379> CONFIG GET dir #獲取當前操作的目錄
1) "dir"
2) "/root"
127.0.0.1:6379> KEYS * #redis中存在的key
1) "myhash"
2) "k3"
3) "mylist"
4) "b1"
5) "du1"
6) "k1"
7) "b4"
8) "key1"
9) "d"
10) "myset"
11) "du11"
12) "list"
13) "b3"
14) "du"
15) "b2"
16) "skey"
17) "k2"
127.0.0.1:6379>
~~~
下面我關閉redis,假設redis服務掛掉!
~~~
127.0.0.1:6379> SHUTDOWN #關閉服務器
[root@localhost ~]# pwd #當前目錄是/root
/root
[root@localhost ~]# ll #下面有dump.rdb這個文件
總用量 52
-rw-------. 1 root root 1208 6月 14 08:10 anaconda-ks.cfg
drwxr-xr-x. 3 root root 4096 6月 17 04:35 dufy
-rw-r--r--. 1 root root 283 6月 19 00:13 dump.rdb
-rw-r--r--. 1 root root 24772 6月 14 08:10 install.log
-rw-r--r--. 1 root root 7690 6月 14 08:09 install.log.syslog
~~~
那么當我進入/usr/local/bin 目錄下啟動重新啟動redis,看數據是否恢復
~~~
[root@localhost ~]# cd /usr/local/bin/
[root@localhost bin]# pwd
/usr/local/bin
[root@localhost bin]# redis-server /root/dufy/redis/redis-3.0.4/redis.conf
[root@localhost bin]# redis-cli
127.0.0.1:6379> KEYS * # 這里啟動后,查看key沒有恢復
(empty list or set)
127.0.0.1:6379>
~~~
那么我再次關閉服務,從/root下啟動redis看數據是否恢復
~~~
127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@localhost bin]# cd /root/
[root@localhost ~]# pwd
/root
[root@localhost ~]# redis-server /root/dufy/redis/redis-3.0.4/redis.conf
[root@localhost ~]# redis-cli
127.0.0.1:6379> KEYS * #重啟后,查看key,發現恢復成功了!
1) "k1"
2) "b1"
3) "key1"
4) "list"
5) "du11"
6) "du1"
7) "b4"
8) "k3"
9) "myhash"
10) "b3"
11) "d"
12) "skey"
13) "mylist"
14) "du"
15) "k2"
16) "b2"
17) "myset"
127.0.0.1:6379>
~~~
相信我這些操作,也講明白快照恢復,上面說的這一句:將備份文件 (dump.rdb) 移動到 redis 安裝目錄并啟動服務即可或者就在當前目錄啟動。
# Redis優點
適合大規模的數據恢復
對數據完整性和一致性要求不高
# Redis缺點
在一定間隔時間做一次備份,所以如果redis意外down掉的話,就會丟失最后一次快照后的所有修改
fork的時候,內存中的數據被克隆了一份,大致2倍的膨脹性需要考慮
# 如何停止RDB
## 配置文件注釋掉
~~~
save 900 1
save 300 10
save 60 10000
~~~
啟動 # save “”, 去掉 #。保存后重啟
## 動態停止RDB命令
在redis-cli中執行:
~~~
config set save ""
~~~
# 大總結
內存中的數據對象 --->rdbsave --> 磁盤中的rdb文件
內存中的數據對象 <---rdload <-- 磁盤中的rdb文件
* RDB是一個非常緊湊的文件
* RDB在保存RDB文件時父進程唯一需要做的就是foker出一個子進程,接下來工作全部交給子進程來做,父進程不需要再做其他IO操作,所以RDB持久化方式可以最大化redis的性能
* 與AOF相比,在恢復大的數據時候,RDB方式更快一些
* 數據丟失風險大
* RDB需要經常folk子進程來保存數據集到磁盤,當數據集比較大額時候,folk的過程是比較耗時的,可能會導致redis在一些毫秒級不能響應客服端請
- SQL
- 名詞
- mysql
- 初識mysql
- 備份和恢復
- 存儲引擎
- 數據表損壞和修復
- mysql工具
- 數據庫操作
- 增
- 刪
- 改
- 查
- 數據類型
- 整數類型
- 小數類型
- 日期時間類型
- 字符和文本型
- enum類型
- set類型
- 時間類型
- null與not null和null與空值''的區別
- 數據表操作
- 創建
- 索引
- 約束
- 表選項列表
- 表的其他語句
- 視圖
- sql增刪改查
- sql增
- sql刪
- sql改
- sql查
- sql語句練習
- 連接查詢和更新
- 常用sql語句集錦
- 函數
- 字符函數
- 數值運算符
- 比較運算符與函數
- 日期時間函數
- 信息函數
- 聚合函數
- 加密函數
- null函數
- 用戶權限管理
- 用戶管理
- 權限管理
- pdo
- 與pdo相關的幾個類
- 連接數據庫
- 使用
- pdo的錯誤處理
- pdo結果集對象
- pdo結果集對象常用方法
- pdo預處理
- 常用屬性
- mysql編程
- 事務
- 語句塊
- mysql中的變量
- 存儲函數
- 存儲過程
- 觸發器
- mysql優化
- 存儲引擎
- 字段類型
- 三范式和逆范式
- 索引
- 查詢緩存
- limit分頁優化
- 分區
- 介紹
- 分區算法
- list分區
- range范圍
- Hash哈希
- key鍵值
- 分區管理
- 特別注意
- 分表
- 數據碎片與維護
- innodb表壓縮
- 慢查詢
- explain執行計劃
- count和max,groupby優化
- 子查詢優化
- mysql鎖機制
- 介紹
- 演示
- 總結
- 樂觀鎖和悲觀鎖
- 扛得住的mysql
- 實例和故事
- 系統參數優化
- mysql體系結構
- mysql基準測試
- 索引
- mysql的復制
- win配置MySQL主從
- mysql5.7新特性
- 常見問題
- general log
- 忘記密碼
- uodo log與redo log
- 事務隔離級別
- mysql8密碼登錄
- explain
- 高效的Tree表
- on delete cascade 總結
- mongod
- 簡介
- 集合文檔操作語句
- 增刪改查
- 索引
- 數據導入和導出
- 主從復制
- php7操作mongod
- 權限管理
- redis
- redis簡介
- 3.2版本配置文件
- 3.0版本配置文件
- 2.8版本配置文件
- 配置文件總結
- 外網連接
- 持久化
- RDB備份方式保存數據
- AOF備份方式保存數據
- 總結
- win安裝redis和sentinel部署
- 事務
- Sentinel模式配置
- 分布式鎖
- 管道
- php中redis代碼
- 發布訂閱
- slowlog
- Redis4.0
- scan和keys
- elasticsearch
- 配置說明
- 啟動
- kibana
- kibana下載
- kibana配置文件
- kibana常用功能
- 常用術語
- Beats
- Beats簡介
- Filebeat
- Packetbeat
- Logstash
- 配置
- elasticsearch架構
- es1.7
- head和bigdesk插件
- 插件大全
- 倒排索引
- 單模式下API增刪改查
- mget獲取多個文檔
- 批量操作bulk
- 版本控制
- Mapping映射
- 基本查詢
- Filter過濾
- 組合查詢
- es配置文件
- es集群優化和管理
- logstash
- kibana
- es5.2
- 安裝
- 沖突處理
- 數據備份
- 缺陷不足
- 集群管理api
- 分布式事務
- CAP理論
- BASE模型
- 兩階段提交(2PC)
- TCC (Try-Confirm-Cancle)
- 異步確保型
- 最大努力通知型
- 總結