Redis利用持久化進行資料移轉

來源:互聯網
上載者:User

Redis利用持久化進行資料移轉

前言

      Redis是一個開源的高效能索引值對資料庫。它通過提供多種索引值資料類型來適應不同情境下的儲存需求,並藉助許多高層級的介面使其可以勝任如緩衝、隊列系統等不同的角色。

Redis持久化瞭解

      為了讓效能更加優異,Redis預設是把所有的資料都存在記憶體中的。但是當伺服器重啟或程式異常崩潰時,Redis的資料就會全部丟失。因此出現了持久化的概念。持久化就是將存在記憶體中的資料同步到磁碟來保證持久化。

1、Redis持久化的方式
    兩種: RDB 和 AOF

      RDB 持久化可以在指定的時間間隔內產生資料集的時間點快照(point-in-time snapshot)。

      AOF 持久化記錄伺服器執行的所有寫操作命令,並在伺服器啟動時,通過重新執行這些命令來還原資料集。 AOF 檔案中的命令全部以 Redis 協議的格式來儲存,新命令會被追加到檔案的末尾。 Redis 還可以在後台對 AOF 檔案進行重寫(rewrite),使得 AOF 檔案的體積不會超出儲存資料集狀態所需的實際大小。

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

附加資訊

Redis–BGSAVE
在後台非同步(Asynchronously)儲存當前資料庫的資料到磁碟。
BGSAVE 命令執行之後立即返回 OK ,然後 Redis fork 出一個新子進程,原來的 Redis 進程(父進程)繼續處理用戶端請求,而子進程則負責將資料儲存到磁碟,然後退出。
用戶端可以通過 LASTSAVE 命令查看相關資訊,判斷 BGSAVE 命令是否執行成功。
請移步 持久化文檔 查看更多相關細節。
可用版本:>= 1.0.0時間複雜度:O(N), N 為要儲存到資料庫中的 key 的數量。傳回值:反饋資訊。
redis> BGSAVE
Background saving started

Redis–LASTSAVE
LASTSAVE
返回最近一次 Redis 成功將資料儲存到磁碟上的時間,以 UNIX 時間戳記格式表示。
可用版本:>= 1.0.0時間複雜度:O(1)傳回值:一個 UNIX 時間戳記。
redis> LASTSAVE
(integer) 1324043588

Ubuntu 14.04下Redis安裝及簡單測試

Redis叢集明細文檔

Ubuntu 12.10下安裝Redis(圖文詳解)+ Jedis串連Redis

Redis系列-安裝部署維護篇

CentOS 6.3安裝Redis

Redis安裝部署學習筆記

Redis設定檔redis.conf 詳解

Redis 的詳細介紹:請點這裡
Redis 的:請點這裡

本文永久更新連結地址:

相關文章

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.