自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

學(xué)會(huì)這15點(diǎn),讓你分分鐘拿下Redis數(shù)據(jù)庫(kù)

運(yùn)維 數(shù)據(jù)庫(kù)運(yùn)維 存儲(chǔ)軟件 Redis
Redis 是一個(gè)開(kāi)源的使用 ANSI C 語(yǔ)言編寫(xiě)、遵守 BSD 協(xié)議、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value 數(shù)據(jù)庫(kù),并提供多種語(yǔ)言的 API。

REmote DIctionary Server(Redis) 是一個(gè)由 Salvatore Sanfilippo 寫(xiě)的 Key-Value 存儲(chǔ)系統(tǒng)。

[[232977]]

Redis 簡(jiǎn)介

Redis 是一個(gè)開(kāi)源的使用 ANSI C 語(yǔ)言編寫(xiě)、遵守 BSD 協(xié)議、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value 數(shù)據(jù)庫(kù),并提供多種語(yǔ)言的 API。

它通常被稱(chēng)為數(shù)據(jù)結(jié)構(gòu)服務(wù)器,因?yàn)橹担╲alue)可以是字符串(String),哈希(Map),列表(list),集合(sets) 和有序集合(sorted sets)等類(lèi)型。

大家都知道 Redis 是基于 Key-Value 的 NoSQL 數(shù)據(jù)庫(kù),因此,先來(lái)了解一下關(guān)于 Key 相關(guān)的知識(shí)點(diǎn):

  • 任何二進(jìn)制的序列都可以作為 key 使用
  • Redis 有統(tǒng)一的規(guī)則來(lái)設(shè)計(jì) key
  • 對(duì) Key-Value 允許的最大長(zhǎng)度是 512MB

支持的語(yǔ)言

  1. ActionScript Bash  C  C#  C++  Clojure Common Lisp 
  2. Crystal  D  Dart  Elixir  emacs  lisp  Erlang   
  3. Fancy  gawk  GNU Prolog  Go  Haskell  Haxe  Io Java  Javascript   
  4. Julia  Lua  Matlab  mruby  Nim  Node.js  Objective-C   
  5. OCaml Pascal  Perl  PHP  Pure Data  Python  R  Racket   
  6. Rebol  Ruby  Rust Scala  Scheme  Smalltalk  Swift  Tcl  VB  VCL 

Redis 的應(yīng)用場(chǎng)景到底有哪些?

  • 最常用的就是會(huì)話緩存
  • 消息隊(duì)列,比如支付
  • 活動(dòng)排行榜或計(jì)數(shù)
  • 發(fā)布、訂閱消息(消息通知)
  • 商品列表、評(píng)論列表等

Redis 安裝

關(guān)于 Redis 安裝與相關(guān)的知識(shí)點(diǎn)介紹請(qǐng)參考:Nosql數(shù)據(jù)庫(kù)服務(wù)之redis

安裝的大概步驟如下:

Redis 是 C 語(yǔ)言開(kāi)發(fā)的,安裝 Redis 需要 C 語(yǔ)言的編譯環(huán)境,如果沒(méi)有 gcc 需要在線安裝:yum install gcc-c++

  • 第一步,獲取源碼包:wget http://download.redis.io/releases/redis-3.0.0.tar.gz
  • 第二步,解壓縮 Redis:tar zxvf redis-3.0.0.tar.gz
  • 第三步,編譯。進(jìn)入redis源碼目錄(cd redis-3.0.0)。執(zhí)行 make
  • 第四步,安裝。make install PREFIX=/usr/local/redis

#PREFIX 參數(shù)指定 Redis 的安裝目錄

Redis 數(shù)據(jù)類(lèi)型

Redis 一共支持五種數(shù)據(jù)類(lèi)型:

  • string(字符串)
  • hash(哈希)
  • list(列表)
  • set(集合)
  • zset(sorted set 有序集合)

String(字符串):它是 Redis 最基本的數(shù)據(jù)類(lèi)型,一個(gè) Key 對(duì)應(yīng)一個(gè) Value,需要注意是一個(gè)鍵值最大存儲(chǔ) 512MB。

  1. 127.0.0.1:6379> set key "hello world" 
  2. OK 
  3. 127.0.0.1:6379> get key 
  4. "hello world" 
  5. 127.0.0.1:6379> getset key "nihao" 
  6. "hello world" 
  7. 127.0.0.1:6379> mset key1 "hi" key2 "nihao" key3 "hello" 
  8. OK 
  9. 127.0.0.1:6379> get key1 
  10. "hi" 
  11. 127.0.0.1:6379> get key2 
  12. "nihao" 
  13. 127.0.0.1:6379> get key3 
  14. "hello" 

相關(guān)命令介紹:

  • set 為一個(gè) Key 設(shè)置 Value(值) 
  • get 獲得某個(gè) Key 對(duì)應(yīng)的 Value(值)
  • getset 為一個(gè) Key 設(shè)置 Value(值)并返回對(duì)應(yīng)的值
  • mset 為多個(gè) Key 設(shè)置 Value(值)

Hash(哈希):Redis Hash是一個(gè)鍵值對(duì)的集合, 是一個(gè) String 類(lèi)型的 Field 和 Value 的映射表,適合用于存儲(chǔ)對(duì)象。

  1. 127.0.0.1:6379> hset redishash 1 "001" 
  2. (integer) 1 
  3. 127.0.0.1:6379> hget redishash 1 
  4. "001" 
  5. 127.0.0.1:6379> hmset redishash 1 "001" 2 "002" 
  6. OK 
  7. 127.0.0.1:6379> hget redishash 1 
  8. "001" 
  9. 127.0.0.1:6379> hget redishash 2 
  10. "002" 
  11. 127.0.0.1:6379> hmget redishash 1 2 
  12. 1) "001" 
  13. 2) "002" 

相關(guān)命令介紹:

  • hset 將 Key 對(duì)應(yīng)的 Hash 中的 Field 配置為 Value,如果 Hash 不存則自動(dòng)創(chuàng)建
  • hget 獲得某個(gè) Hash 中的 Field 配置的值
  • hmset 批量配置同一個(gè) Hash 中的多個(gè) Field 值
  • hmget 批量獲得同一個(gè) Hash 中的多個(gè) Field 值

List(列表):是 Redis 簡(jiǎn)單的字符串列表,它按插入順序排序。

  1. 127.0.0.1:6379> lpush word  hi 
  2. (integer) 1 
  3. 127.0.0.1:6379> lpush word  hello 
  4. (integer) 2 
  5. 127.0.0.1:6379> rpush word  world 
  6. (integer) 3 
  7. 127.0.0.1:6379> lrange word 0 2 
  8. 1) "hello" 
  9. 2) "hi" 
  10. 3) "world" 
  11. 127.0.0.1:6379> llen word 
  12. (integer) 3 

相關(guān)命令介紹:

  • lpush 向指定的列表左側(cè)插入元素,返回插入后列表的長(zhǎng)度
  • rpush 向指定的列表右側(cè)插入元素,返回插入后列表的長(zhǎng)度
  • llen 返回指定列表的長(zhǎng)度
  • lrange 返回指定列表中指定范圍的元素值

Set(集合):是 String 類(lèi)型的無(wú)序集合,也不可重復(fù)。

  1. 127.0.0.1:6379> sadd redis redisset 
  2. (integer) 1 
  3. 127.0.0.1:6379> sadd redis redisset1 
  4. (integer) 1 
  5. 127.0.0.1:6379> sadd redis redisset2 
  6. (integer) 1 
  7. 127.0.0.1:6379> smembers redis 
  8. 1) "redisset1" 
  9. 2) "redisset" 
  10. 3) "redisset2" 
  11. 127.0.0.1:6379> sadd redis redisset2 
  12. (integer) 0 
  13. 127.0.0.1:6379> smembers redis 
  14. 1) "redisset1" 
  15. 2) "redisset" 
  16. 3) "redisset2" 
  17. 127.0.0.1:6379> smembers redis 
  18. 1) "redisset1" 
  19. 2) "redisset3" 
  20. 3) "redisset" 
  21. 4) "redisset2" 
  22. 127.0.0.1:6379> srem redis redisset 
  23. (integer) 1 
  24. 127.0.0.1:6379> smembers redis 
  25. 1) "redisset1" 
  26. 2) "redisset3" 
  27. 3) "redisset2" 

相關(guān)命令介紹:

  • sadd 添加一個(gè) String 元素到 Key 對(duì)應(yīng)的 set 集合中,成功返回 1,如果元素存在返回 0
  • smembers 返回指定的集合中所有的元素
  • srem 刪除指定集合的某個(gè)元素

Zset(sorted set 有序集合):是 String 類(lèi)型的有序集合,也不可重復(fù)。

sorted set 中的每個(gè)元素都需要指定一個(gè)分?jǐn)?shù),根據(jù)分?jǐn)?shù)對(duì)元素進(jìn)行升序排序,如果多個(gè)元素有相同的分?jǐn)?shù),則以字典序進(jìn)行升序排序,sorted set 因此非常適合實(shí)現(xiàn)排名。

  1. 127.0.0.1:6379> zadd nosql 0 001 
  2. (integer) 1 
  3. 127.0.0.1:6379> zadd nosql 0 002 
  4. (integer) 1 
  5. 127.0.0.1:6379> zadd nosql 0 003 
  6. (integer) 1 
  7. 127.0.0.1:6379> zcount nosql 0 0  
  8. (integer) 3 
  9. 127.0.0.1:6379> zcount nosql 0 3 
  10. (integer) 3 
  11. 127.0.0.1:6379> zrem nosql 002 
  12. (integer) 1 
  13. 127.0.0.1:6379> zcount nosql 0 3 
  14. (integer) 2 
  15. 127.0.0.1:6379> zscore nosql 003 
  16. "0" 
  17. 127.0.0.1:6379> zrangebyscore nosql 0 10 
  18. 1) "001" 
  19. 2) "003" 
  20. 127.0.0.1:6379> zadd nosql 1 003 
  21. (integer) 0 
  22. 127.0.0.1:6379> zadd nosql 1 004 
  23. (integer) 1 
  24. 127.0.0.1:6379> zrangebyscore nosql 0 10 
  25. 1) "001" 
  26. 2) "003" 
  27. 3) "004" 
  28. 127.0.0.1:6379> zadd nosql 3 005 
  29. (integer) 1 
  30. 127.0.0.1:6379> zadd nosql 2 006 
  31. (integer) 1 
  32. 127.0.0.1:6379> zrangebyscore nosql 0 10 
  33. 1) "001" 
  34. 2) "003" 
  35. 3) "004" 
  36. 4) "006" 
  37. 5) "005" 

相關(guān)命令介紹:

  • zadd 向指定的 sorteset 中添加 1 個(gè)或多個(gè)元素
  • zrem 從指定的 sorteset 中刪除 1 個(gè)或多個(gè)元素
  • zcount 查看指定的 sorteset 中指定分?jǐn)?shù)范圍內(nèi)的元素?cái)?shù)量
  • zscore 查看指定的 sorteset 中指定分?jǐn)?shù)的元素
  • zrangebyscore 查看指定的 sorteset 中指定分?jǐn)?shù)范圍內(nèi)的所有元素

鍵值相關(guān)的命令

  1. 127.0.0.1:6379> exists key 
  2. (integer) 1 
  3. 127.0.0.1:6379> exists key1 
  4. (integer) 1 
  5. 127.0.0.1:6379> exists key100 
  6. (integer) 0 
  7. 127.0.0.1:6379> get key 
  8. "nihao,hello" 
  9. 127.0.0.1:6379> get key1 
  10. "hi" 
  11. 127.0.0.1:6379> del key1 
  12. (integer) 1 
  13. 127.0.0.1:6379> get key1 
  14. (nil) 
  15. 127.0.0.1:6379> rename key key0 
  16. OK 
  17. 127.0.0.1:6379> get key 
  18. (nil) 
  19. 127.0.0.1:6379> get key0 
  20. "nihao,hello" 
  21. 127.0.0.1:6379> type key0 
  22. string 
  • exists:#確認(rèn) Key 是否存在
  • del:#刪除 Key
  • expire:#設(shè)置 Key 過(guò)期時(shí)間(單位秒)
  • persist:#移除 Key 過(guò)期時(shí)間的配置
  • rename:#重命名 Key
  • type:#返回值的類(lèi)型

Redis 服務(wù)相關(guān)的命令

  1. 127.0.0.1:6379> select 0 
  2. OK 
  3. 127.0.0.1:6379> info 
  4. # Server 
  5. redis_version:3.0.6 
  6. redis_git_sha1:00000000 
  7. redis_git_dirty:0 
  8. redis_build_id:347e3eeef5029f3 
  9. redis_mode:standalone 
  10. os:Linux 3.10.0-693.el7.x86_64 x86_64 
  11. arch_bits:64 
  12. multiplexing_api:epoll 
  13. gcc_version:4.8.5 
  14. process_id:31197 
  15. run_id:8b6ec6ad5035f5df0b94454e199511084ac6fb12 
  16. tcp_port:6379 
  17. uptime_in_seconds:8514 
  18. uptime_in_days:0 
  19. hz:10 
  20. lru_clock:14015928 
  21. config_file:/usr/local/redis/redis.conf 
  22. -------------------省略N行 
  23. 127.0.0.1:6379> CONFIG GET 0 
  24. (empty list or set
  25. 127.0.0.1:6379> CONFIG GET 15 
  26. (empty list or set
  • slect:#選擇數(shù)據(jù)庫(kù)(數(shù)據(jù)庫(kù)編號(hào) 0-15)
  • quit:#退出連接
  • info:#獲得服務(wù)的信息與統(tǒng)計(jì)
  • monitor:#實(shí)時(shí)監(jiān)控
  • config get:#獲得服務(wù)配置
  • flushdb:#刪除當(dāng)前選擇的數(shù)據(jù)庫(kù)中的 Key
  • flushall:#刪除所有數(shù)據(jù)庫(kù)中的 Key

Redis 的發(fā)布與訂閱

Redis 發(fā)布與訂閱(pub/sub)是它的一種消息通信模式,一方發(fā)送信息,一方接收信息。

下圖是三個(gè)客戶端同時(shí)訂閱同一個(gè)頻道:

下圖是有新信息發(fā)送給頻道 1 時(shí),就會(huì)將消息發(fā)送給訂閱它的三個(gè)客戶端:

Redis 事務(wù)

Redis 事務(wù)可以一次執(zhí)行多條命令:

  • 發(fā)送 exec 命令前放入隊(duì)列緩存,結(jié)束事務(wù)
  • 收到 exec 命令后執(zhí)行事務(wù)操作,如果某一命令執(zhí)行失敗,其它命令仍可繼續(xù)執(zhí)行
  • 一個(gè)事務(wù)執(zhí)行的過(guò)程中,其它客戶端提交的請(qǐng)求不會(huì)被插入到事務(wù)執(zhí)行的命令列表中

一個(gè)事務(wù)經(jīng)歷三個(gè)階段:

  • 開(kāi)始事務(wù)(命令:multi)
  • 命令執(zhí)行
  • 結(jié)束事務(wù)(命令:exec)
  1. 127.0.0.1:6379> MULTI 
  2. OK 
  3. 127.0.0.1:6379> set key key1 
  4. QUEUED 
  5. 127.0.0.1:6379> get key 
  6. QUEUED 
  7. 127.0.0.1:6379> rename key key001 
  8. QUEUED 
  9. 127.0.0.1:6379> exec 
  10. 1) OK 
  11. 2) "key1" 
  12. 3) OK 

Redis 安全配置

可以通過(guò)修改配置文件設(shè)備密碼參數(shù)來(lái)提高安全性:#requirepass foobared。

去掉注釋#號(hào)就可以配置密碼,沒(méi)有配置密碼的情況下查詢?nèi)缦拢?/p>

  1. 127.0.0.1:6379> CONFIG GET requirepass  
  2. 1) "requirepass"  
  3. 2) "" 

配置密碼之后,就需要進(jìn)行認(rèn)證:

  1. 127.0.0.1:6379> CONFIG GET requirepass 
  2. (error) NOAUTH Authentication required. 
  3. 127.0.0.1:6379> AUTH foobared #認(rèn)證 
  4. OK 
  5. 127.0.0.1:6379> CONFIG GET requirepass 
  6. 1) "requirepass" 
  7. 2) "foobared" 

Redis 持久化

Redis 持久有兩種方式:

  • Snapshotting(快照)
  • Append-only file(AOF)

Snapshotting(快照):

  • 將存儲(chǔ)在內(nèi)存的數(shù)據(jù)以快照的方式寫(xiě)入二進(jìn)制文件中,如默認(rèn) dump.rdb中。
  • save 900 1,#900 秒內(nèi)如果超過(guò) 1 個(gè) Key 被修改,則啟動(dòng)快照保存。
  • save 300 10,#300 秒內(nèi)如果超過(guò) 10 個(gè) Key 被修改,則啟動(dòng)快照保存。
  • save 60 10000,#60 秒內(nèi)如果超過(guò) 10000 個(gè) Key 被修改,則啟動(dòng)快照保存。

Append-only file(AOF):

  • 使用 AOF 持久時(shí),服務(wù)會(huì)將每個(gè)收到的寫(xiě)命令通過(guò) write 函數(shù)追加到文件中(appendonly.aof)
  • AOF 持久化存儲(chǔ)方式參數(shù)說(shuō)明:

appendonly yes,#開(kāi)啟 AOF 持久化存儲(chǔ)方式 

appendfsync always,#收到寫(xiě)命令后就立即寫(xiě)入磁盤(pán),效率最差,效果最好

appendfsync everysec,#每秒寫(xiě)入磁盤(pán)一次,效率與效果居中

appendfsync no,#完全依賴 OS,效率最佳,效果沒(méi)法保證

Redis 性能測(cè)試

自帶相關(guān)測(cè)試工具:

  1. [root@test ~]# redis-benchmark --help 
  2. Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>] 
  3.  
  4.  -h <hostname>      Server hostname (default 127.0.0.1) 
  5.  -p <port>          Server port (default 6379) 
  6.  -s <socket>        Server socket (overrides host and port) 
  7.  -a <password>      Password for Redis Auth 
  8.  -c <clients>       Number of parallel connections (default 50) 
  9.  -n <requests>      Total number of requests (default 100000) 
  10.  -d <size>          Data size of SET/GET value in bytes (default 2) 
  11.  -dbnum <db>        SELECT the specified db number (default 0) 
  12.  -k <boolean>       1=keep alive 0=reconnect (default 1) 
  13.  -r <keyspacelen>   Use random keys for SET/GET/INCR, random values for SADD 
  14.   Using this option the benchmark will expand the string __rand_int__ 
  15.   inside an argument with a 12 digits number in the specified range 
  16.   from 0 to keyspacelen-1. The substitution changes every time a command 
  17.   is executed. Default tests use this to hit random keys in the 
  18.   specified range. 
  19.  -P <numreq>        Pipeline <numreq> requests. Default 1 (no pipeline). 
  20.  -q                 Quiet. Just show query/sec values 
  21.  --csv              Output in CSV format 
  22.  -l                 Loop. Run the tests forever 
  23.  -t <tests>         Only run the comma separated list of tests. The test 
  24.                     names are the same as the ones produced as output
  25.  -I                 Idle mode. Just open N idle connections and wait. 
  26.  
  27. Examples: 
  28.  
  29.  Run the benchmark with the default configuration against 127.0.0.1:6379: 
  30.    $ redis-benchmark 
  31.  
  32.  Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1: 
  33.    $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20 
  34.  
  35.  Fill 127.0.0.1:6379 with about 1 million keys only using the SET test: 
  36.    $ redis-benchmark -t set -n 1000000 -r 100000000 
  37.  
  38.  Benchmark 127.0.0.1:6379 for a few commands producing CSV output
  39.    $ redis-benchmark -t ping,set,get -n 100000 --csv 
  40.  
  41.  Benchmark a specific command line: 
  42.    $ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0 
  43.  
  44.  Fill a list with 10000 random elements: 
  45.    $ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__ 
  46.  
  47.  On user specified command lines __rand_int__ is replaced with a random integer 
  48.  with a range of values selected by the -r option

實(shí)際測(cè)試同時(shí)執(zhí)行 100 萬(wàn)的請(qǐng)求:

  1. [root@test ~]# redis-benchmark -n 1000000 -q 
  2. PING_INLINE: 152578.58 requests per second 
  3. PING_BULK: 150308.14 requests per second 
  4. SET: 143266.47 requests per second 
  5. GET: 148632.58 requests per second 
  6. INCR: 145857.64 requests per second 
  7. LPUSH: 143781.45 requests per second 
  8. LPOP: 147819.66 requests per second 
  9. SADD: 138350.86 requests per second 
  10. SPOP: 134282.27 requests per second 
  11. LPUSH (needed to benchmark LRANGE): 141302.81 requests per second 
  12. LRANGE_100 (first 100 elements): 146756.67 requests per second 
  13. LRANGE_300 (first 300 elements): 148104.27 requests per second 
  14. LRANGE_500 (first 450 elements): 152671.75 requests per second 
  15. LRANGE_600 (first 600 elements): 148104.27 requests per second 
  16. MSET (10 keys): 132731.62 requests per second 

Redis 的備份與恢復(fù)

Redis 自動(dòng)備份有兩種方式:

  1. 通過(guò) dump.rdb 文件實(shí)現(xiàn)備份
  2. 使用 aof 文件實(shí)現(xiàn)自動(dòng)備份

dump.rdb 備份

Redis 服務(wù)默認(rèn)的自動(dòng)文件備份方式(AOF 沒(méi)有開(kāi)啟的情況下),在服務(wù)啟動(dòng)時(shí),就會(huì)自動(dòng)從 dump.rdb 文件中去加載數(shù)據(jù)。

具體配置在 redis.conf:

  • save 900 1 
  • save 300 10 
  • save 60 10000

也可以手工執(zhí)行 save 命令實(shí)現(xiàn)手動(dòng)備份。

  1. 127.0.0.1:6379> set name key 
  2. OK 
  3. 127.0.0.1:6379> SAVE 
  4. OK 
  5. 127.0.0.1:6379> set name key1 
  6. OK 
  7. 127.0.0.1:6379> BGSAVE 
  8. Background saving started 

Redis 快照到 dump 文件時(shí),會(huì)自動(dòng)生成 dump.rdb 的文件。

  1. # The filename where to dump the DB 
  2. dbfilename dump.rdb 
  3. -rw-r--r-- 1 root root   253 Apr 17 20:17 dump.rdb 
  • SAVE 命令表示使用主進(jìn)程將當(dāng)前數(shù)據(jù)庫(kù)快照到 dump 文件
  • BGSAVE 命令表示,主進(jìn)程會(huì) fork 一個(gè)子進(jìn)程來(lái)進(jìn)行快照備份
  • 兩種備份不同之處,前者會(huì)阻塞主進(jìn)程,后者不會(huì)。

恢復(fù)舉例

  1. # Note that you must specify a directory here, not a file name
  2. dir /usr/local/redisdata/ 
  3. 備份文件存儲(chǔ)路徑 
  4. 127.0.0.1:6379> CONFIG GET dir 
  5. 1) "dir" 
  6. 2) "/usr/local/redisdata" 
  7. 127.0.0.1:6379> set key 001 
  8. OK 
  9. 127.0.0.1:6379> set key1 002 
  10. OK 
  11. 127.0.0.1:6379> set key2 003 
  12. OK 
  13. 127.0.0.1:6379> save 
  14. OK 

將備份文件備份到其他目錄:

  1. [root@test ~]# ll /usr/local/redisdata/ 
  2. total 4 
  3. -rw-r--r-- 1 root root 49 Apr 17 21:24 dump.rdb 
  4. [root@test ~]# date 
  5. Tue Apr 17 21:25:38 CST 2018 
  6. [root@test ~]# cp ./dump.rdb /tmp/ 

刪除數(shù)據(jù):

  1. 127.0.0.1:6379> del key1 
  2. (integer) 1 
  3. 127.0.0.1:6379> get key1 
  4. (nil) 

關(guān)閉服務(wù),將原備份文件拷貝回 save 備份目錄:

  1. [root@test ~]# redis-cli -a foobared shutdown 
  2. [root@test ~]# lsof -i :6379 
  3. [root@test ~]# cp /tmp/dump.rdb /usr/local/redisdata/ 
  4. cp: overwrite ‘/usr/local/redisdata/dump.rdb’? y 
  5. [root@test ~]# redis-server /usr/local/redis/redis.conf & 
  6. [1] 31487 

登錄查看數(shù)據(jù)是否恢復(fù):

  1. [root@test ~]# redis-cli -a foobared 
  2. 127.0.0.1:6379> mget key key1 key2 
  3. 1) "001" 
  4. 2) "002" 
  5. 3) "003" 

AOF 自動(dòng)備份

Redis服務(wù)默認(rèn)是關(guān)閉此項(xiàng)配置:

  1. ###### APPEND ONLY MODE ########## 
  2. appendonly no 
  3.  
  4. # The name of the append only file (default"appendonly.aof"
  5. appendfilename "appendonly.aof" 
  6.  
  7. # appendfsync always 
  8. appendfsync everysec 
  9. # appendfsync no 

配置文件的相關(guān)參數(shù),前面已經(jīng)詳細(xì)介紹過(guò)。AOF 文件備份,是備份所有的歷史記錄以及執(zhí)行過(guò)的命令,和 MySQL binlog 很相似,在恢復(fù)時(shí)就是重新執(zhí)次一次之前執(zhí)行的命令。

需要注意的就是在恢復(fù)之前,和數(shù)據(jù)庫(kù)恢復(fù)一樣需要手工刪除執(zhí)行過(guò)的del或誤操作的命令。

AOF 與 dump 備份不同:

  • AOF 文件備份與 dump 文件備份不同
  • 服務(wù)讀取文件的優(yōu)先順序不同,會(huì)按照以下優(yōu)先級(jí)進(jìn)行啟動(dòng)

如果只配置 AOF,重啟時(shí)加載 AOF 文件恢復(fù)數(shù)據(jù)

如果同時(shí) 配置了 RBD 和 AOF,啟動(dòng)是只加載 AOF 文件恢復(fù)數(shù)據(jù)

如果只配置 RBD,啟動(dòng)時(shí)將加載 dump 文件恢復(fù)數(shù)據(jù)

注意:只要配置了 AOF,但是沒(méi)有 AOF 文件,這個(gè)時(shí)候啟動(dòng)的數(shù)據(jù)庫(kù)會(huì)是空的。

Redis 生產(chǎn)優(yōu)化介紹

內(nèi)存管理優(yōu)化 

  • hash-max-ziplist-entries 512
  • hash-max-ziplist-value 64
  • list-max-ziplist-entries 512
  • list-max-ziplist-value 64

#list 的成員與值都不太大的時(shí)候會(huì)采用緊湊格式來(lái)存儲(chǔ),相對(duì)內(nèi)存開(kāi)銷(xiāo)也較小

在 Linux 環(huán)境運(yùn)行 Redis 時(shí),如果系統(tǒng)的內(nèi)存比較小,這個(gè)時(shí)候自動(dòng)備份會(huì)有可能失敗,需要修改系統(tǒng)的 vm.overcommit_memory 參數(shù),這個(gè)參數(shù)是 Linux 系統(tǒng)的內(nèi)存分配策略。

  • 0 表示內(nèi)核將檢查是否有足夠的可用內(nèi)存供應(yīng)用進(jìn)程使用;如果有足夠的可用內(nèi)存,內(nèi)存申請(qǐng)?jiān)试S;否則,內(nèi)存申請(qǐng)失敗,并把錯(cuò)誤返回給應(yīng)用進(jìn)程。
  • 1 表示內(nèi)核允許分配所有的物理內(nèi)存,而不管當(dāng)前的內(nèi)存狀態(tài)如何。
  • 2 表示內(nèi)核允許分配超過(guò)所有物理內(nèi)存和交換空間總和的內(nèi)存。

Redis官方的說(shuō)明是,建議將 vm.overcommit_memory 的值修改為 1,可以用下面幾種方式進(jìn)行修改:

  • 編輯 /etc/sysctl.conf 改 vm.overcommit_memory=1,然后 sysctl -p 使配置文件生效
  • sysctl vm.overcommit_memory=1
  • echo 1 > /proc/sysm/overcommit_memory 

內(nèi)存預(yù)分配

持久化機(jī)制

  • 定時(shí)快照:效率不高,會(huì)丟失數(shù)據(jù)
  • AOF:保持?jǐn)?shù)據(jù)完整性(一個(gè)實(shí)例的數(shù)量不要太大2G最大)

優(yōu)化總結(jié):

  • 根據(jù)業(yè)務(wù)需要選擇合適的數(shù)據(jù)類(lèi)型
  • 當(dāng)業(yè)務(wù)場(chǎng)景不需持久化時(shí)就關(guān)閉所有持久化方式(采用ssd磁盤(pán)來(lái)提升效率)
  • 不要使用虛擬內(nèi)存的方式,每秒實(shí)時(shí)寫(xiě)入 AOF
  • 不要讓 Redis 所在的服務(wù)器物理內(nèi)存使用超過(guò)內(nèi)存總量的 3/5
  • 要使用 maxmemory
  • 大數(shù)據(jù)量按業(yè)務(wù)分開(kāi)使用多個(gè) Redis 實(shí)例

Redis 集群應(yīng)用

集群是將多個(gè) Redis 實(shí)例集中在一起,實(shí)現(xiàn)同一業(yè)務(wù)需求,或者實(shí)現(xiàn)高可用與負(fù)載均衡到底有哪些集群方案呢?

Haproxy+Keepalived+Redis 集群

  • 通過(guò) Redis 的配置文件,實(shí)現(xiàn)主從復(fù)制、讀寫(xiě)分離
  • 通過(guò) Haproxy 的配置,實(shí)現(xiàn)負(fù)載均衡,當(dāng)從故障時(shí)也會(huì)及時(shí)從集群中T除
  • 利用 Keepalived 來(lái)實(shí)現(xiàn)負(fù)載的高可用

Redis 官方 Sentinel 集群管理工具

Redis集群生產(chǎn)環(huán)境高可用方案實(shí)戰(zhàn)過(guò)程:

  • Sentinel 負(fù)責(zé)對(duì)集群中的主從服務(wù)監(jiān)控、提醒和自動(dòng)故障轉(zhuǎn)移
  • Redis 集群負(fù)責(zé)對(duì)外提供服務(wù)
  • 關(guān)于 Redis Sentinel Cluster 集群配置可參考

Redis Cluster 

Redis Cluster 是 Redis 的分布式解決方案,在 Redis 3.0 版本正式推出的,有效解決了 Redis 分布式方面的需求。

當(dāng)遇到單機(jī)內(nèi)存、并發(fā)、流量等瓶頸時(shí),可以采用Cluster架構(gòu)達(dá)到負(fù)載均衡的目的。

  • 官方推薦,毋庸置疑。
  • 去中心化,集群最大可增加 1000 個(gè)節(jié)點(diǎn),性能隨節(jié)點(diǎn)增加而線性擴(kuò)展。
  • 管理方便,后續(xù)可自行增加或摘除節(jié)點(diǎn),移動(dòng)分槽等等。
  • 簡(jiǎn)單,易上手。

責(zé)任編輯:武曉燕 來(lái)源: 民工哥Linux運(yùn)維
相關(guān)推薦

2018-05-14 09:05:40

Redis數(shù)據(jù)庫(kù)String

2018-06-21 15:15:05

數(shù)據(jù)庫(kù)Redis書(shū)籍

2019-03-11 12:50:51

offer書(shū)單面試

2015-12-22 11:48:50

javascript閉包

2017-07-17 14:17:37

閉包匿名函數(shù) 作用域

2020-04-14 14:30:43

Redis數(shù)據(jù)庫(kù)開(kāi)源

2020-02-15 16:48:28

機(jī)器學(xué)習(xí)算法人工智能

2019-08-20 14:40:35

Redis數(shù)據(jù)庫(kù)

2019-12-31 14:10:58

Excel文章SQL

2017-06-13 10:08:19

AI交通識(shí)別

2021-05-08 12:10:49

Python代碼抓點(diǎn)圖片

2024-08-21 08:27:30

擴(kuò)展數(shù)據(jù)庫(kù)服務(wù)器

2017-06-28 09:26:28

人臉識(shí)別AIFR技術(shù)

2021-01-13 11:03:20

Python數(shù)據(jù)代碼

2023-07-03 07:20:50

2023-03-10 22:08:20

2009-11-20 17:06:49

Oracle數(shù)據(jù)庫(kù)字符

2013-12-19 09:20:59

2020-12-08 09:52:22

數(shù)據(jù)庫(kù)工具技術(shù)

2015-10-29 18:05:36

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)