Redis是一個開源的高性能鍵值對數據庫。它經過提供多種鍵值數據類型來適應不一樣場景下的存儲需求,並藉助許多高層級的接口使其能夠勝任如緩存、隊列系統等不一樣的角色。git
爲了讓性能更加優異,Redis默認是把全部的數據都存在內存中的。可是當服務器重啓或程序異常崩潰時,Redis的數據就會所有丟失。所以出現了持久化的概念。持久化就是將存在內存中的數據同步到磁盤來保證持久化。web
1、Redis持久化的方式
兩種: RDB 和 AOFredis
RDB 持久化能夠在指定的時間間隔內生成數據集的時間點快照(point-in-time snapshot)。數據庫
AOF 持久化記錄服務器執行的全部寫操做命令,並在服務器啓動時,經過從新執行這些命令來還原數據集。 AOF 文件中的命令所有以 Redis 協議的格式來保存,新命令會被追加到文件的末尾。 Redis 還能夠在後臺對 AOF 文件進行重寫(rewrite),使得 AOF 文件的體積不會超出保存數據集狀態所需的實際大小。api
2、持久化的數據有什麼用?
用於重啓後的數據恢復。Redis是一個內存數據庫,不管是RDB仍是AOF,都只是其保證數據恢復的措施;因此Redis在利用RDB和AOF進行恢復的時候,都會讀取RDB或AOF文件,從新加載到內存中。緩存
其中RDB就是point-in-time snapshot快照存儲,也是默認的持久化方式。對於RDB可理解爲半持久化模式,即按照必定的策略週期性的將數據保存到磁盤。對應產生的數據文件爲dump.rdb,經過配置文件中的save參數來定義快照的週期。Redis的RDB文件不會壞掉,由於其寫操做是在一個新進程中進行的。 服務器
默認的持久化設置:
save 900 1 #當有一條Keys數據被改變時,900秒刷新到Disk一次
save 300 10 #當有10條Keys數據被改變時,300秒刷新到Disk一次
save 60 10000 #當有10000條Keys數據被改變時,60秒刷新到Disk一次異步
##########查看配置信息及當前存儲的key值###########
[root@web-yv2 ~]# redis-cli -h 10.160.35.86 -p 6379
redis 10.160.35.86:6379> info
redis_version:2.4.10
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.6
process_id:11911
uptime_in_seconds:2154256
uptime_in_days:24
lru_clock:1527581
used_cpu_sys:1145.31
used_cpu_user:1430.18
used_cpu_sys_children:56.20
used_cpu_user_children:207.71
connected_clients:147
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:12
used_memory:6762928
used_memory_human:6.45M
used_memory_rss:19816448
used_memory_peak:10441776
used_memory_peak_human:9.96M
mem_fragmentation_ratio:2.93
mem_allocator:jemalloc-2.2.5
loading:0
aof_enabled:0
changes_since_last_save:166
bgsave_in_progress:0
last_save_time:1420367541
bgrewriteaof_in_progress:0
total_connections_received:1387982
total_commands_processed:25568539
expired_keys:1838499
evicted_keys:0
keyspace_hits:529489
keyspace_misses:1838207
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:947
vm_enabled:0
role:master
db0:keys=18255,expires=17326
#########保存最新的key值################
redis 10.160.35.86:6379> BGSAVE
Background saving started
##########查看是否保存成功##############
redis 10.160.35.86:6379> LASTSAVE
(integer) 1420367903
##########關閉redis服務器##############
redis-cli -h 10.160.35.86 -p 6379 SHUTDOWN
#########查看Redis的RDB文件###########
[root@web-yv2 ~]# grep "dir" /etc/redis.conf #查看RDB文件的存放位置
# The working directory.
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
# Also the Append Only File will be created inside this directory.
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis/ #RDB文件存放於此
# directive below) it is possible to tell the slave to authenticate before
# using the following configuration directive.
# the swap file under /tmp is not secure. Create a dir with access granted
# configuration directives.
[root@web-yv2 ~]# find / -name dump.rdb #查找dump文件
/var/lib/redis/dump.rdb
##########壓縮redis文件並拷入另外一臺機器#########
[root@web-yv2 ~]# tar zcvf redis.tar.gz /var/lib/redis
[root@web-yv2 ~]# scp redis.tar.gz root@10.160.35.67:/var/lib/
#########登錄10.160.35.67機器並作相應配置#######
[root@web-yv1 ~]# vi /etc/redis.conf
dir /var/lib/redis/ #指定RDB文件路徑
#########解壓縮RDB文件##########################
[root@web-yv1 ~]# cd /var/lib/
[root@web-yv1 ~]# tar xf redis.tar.gz
#########重啓Redis服務器########################
[root@web-yv1 ~]# service redis restart
在後臺異步(Asynchronously)保存當前數據庫的數據到磁盤。
BGSAVE 命令執行以後當即返回 OK ,而後 Redis fork 出一個新子進程,原來的 Redis 進程(父進程)繼續處理客戶端請求,而子進程則負責將數據保存到磁盤,而後退出。
客戶端能夠經過 LASTSAVE 命令查看相關信息,判斷 BGSAVE 命令是否執行成功。
請移步 持久化文檔 查看更多相關細節。
可用版本:>= 1.0.0時間複雜度:O(N), N 爲要保存到數據庫中的 key 的數量。返回值:反饋信息。
redis> BGSAVE
Background saving started
LASTSAVE
返回最近一次 Redis 成功將數據保存到磁盤上的時間,以 UNIX 時間戳格式表示。
可用版本:>= 1.0.0時間複雜度:O(1)返回值:一個 UNIX 時間戳。
redis> LASTSAVE
(integer) 1324043588
http://tool.chinaz.com/Tools/unixtime.aspx