#RDB 和 AOF
RDB 是一個非常緊湊(compact)的文件,它保存了 Redis 在某個時間點上的數(shù)據(jù)集次乓,可以 24 小時備份一次磷雇,可以每小時備份一次 魄幕,RDB 非常適用于災(zāi)難恢復(fù)(disaster recovery):它只有一個文件,并且內(nèi)容都非常緊湊偶妖,RDB 可以最大化 Redis 的性能姜凄。如果需要盡量避免在服務(wù)器故障時丟失數(shù)據(jù),那么 RDB 并不適合趾访,因為RDB 文件需要保存整個數(shù)據(jù)集的狀態(tài)态秧,因此可能至少 5 分鐘才保存一次 RDB 文件,在這種情況下扼鞋,一旦發(fā)生故障停機申鱼,可能會丟失好幾分鐘的數(shù)據(jù)。
AOF 文件是一個只進行追加操作的日志文件云头,AOF 持久化會讓 Redis 變得非常耐久(much more durable):設(shè)置不同的 fsync 策略捐友,比如無 fsync ,每秒鐘一次 fsync 盘寡,或者每次執(zhí)行寫入命令時 fsync 楚殿。AOF 的默認(rèn)策略為每秒鐘 fsync 一次,在這種配置下,Redis 仍然可以保持良好的性能脆粥,并且就算發(fā)生故障停機砌溺,也最多只會丟失一秒鐘的數(shù)據(jù)( fsync 會在后臺線程執(zhí)行,所以主線程可以繼續(xù)努力地處理命令請求)变隔。對于相同的數(shù)據(jù)集來說规伐,AOF 文件的體積通常要大于 RDB 文件的體積。根據(jù)所使用的 fsync 策略匣缘,AOF 的速度可能會慢于 RDB 猖闪。
一般來說,如果想達到足以媲美 PostgreSQL 的數(shù)據(jù)安全性, 你應(yīng)該同時使用兩種持久化功能肌厨。如果你非常關(guān)心你的數(shù)據(jù),但仍然可以承受數(shù)分鐘以內(nèi)的數(shù)據(jù)丟失培慌, 那么你可以只使用 RDB 持久化。有很多用戶都只使用 AOF 持久化柑爸, 但我們并不推薦這種方式: 因為定時生成 RDB 快照(snapshot)非常便于進行數(shù)據(jù)庫備份吵护, 并且 RDB 恢復(fù)數(shù)據(jù)集的速度也要比 AOF 恢復(fù)的速度要快, 除此之外表鳍, 使用 RDB 還可以避免之前提到的 AOF 程序的 bug 馅而。
#連接實例
redis-cli -h 10.205.20.8 -p 6379 -c
-c表示集群模式
#注意
錯誤 /usr/bin/env: ruby: No such file or directory,解決方法:
apt-get install ruby
Redis集群需要開放端口號+10000(集群內(nèi)部使用端口)譬圣,不然集群連接不上
#啟動Redis集群鏡像瓮恭,Docker中必須使用host模式
docker run -itd --net host --name redis-cluster 10.170.232.47:5000/redis3.2
進入容器,分別啟動以下實例:
redis-server /opt/cachecloud/conf/static/redis-cluster-6379.conf > /opt/cachecloud/logs/redis-6379.log 2>&1 &
redis-server /opt/cachecloud/conf/static/redis-cluster-6380.conf > /opt/cachecloud/logs/redis-6380.log 2>&1 &
redis-server /opt/cachecloud/conf/static/redis-cluster-6381.conf > /opt/cachecloud/logs/redis-6381.log 2>&1 &
redis-server /opt/cachecloud/conf/static/redis-cluster-6382.conf > /opt/cachecloud/logs/redis-6382.log 2>&1 &
redis-server /opt/cachecloud/conf/static/redis-cluster-6383.conf > /opt/cachecloud/logs/redis-6383.log 2>&1 &
redis-server /opt/cachecloud/conf/static/redis-cluster-6384.conf > /opt/cachecloud/logs/redis-6384.log 2>&1 &
創(chuàng)建集群
redis-trib.rb create --replicas 1 10.205.20.8:6379 10.205.20.8:6380 10.205.20.8:6381 10.205.20.8:6382 10.205.20.8:6383 10.205.20.8:6384
選項–replicas 1 表示為集群中的每個主節(jié)點創(chuàng)建一個從節(jié)點
#redis-cluster-6379.conf配置
bind 10.172.240.66
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 0
daemonize no
loglevel notice
databases 16
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
repl-ping-slave-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 10mb
repl-backlog-ttl 7200
maxmemory 256mb
maxmemory-policy volatile-lru
no-appendfsync-on-rewrite yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 512mb 128mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
port 6379
dbfilename dump-6379.rdb
dir /opt/cachecloud/data
appendonly no
appendfsync everysec
appendfilename appendonly-6379.aof
auto-aof-rewrite-percentage 94
auto-aof-rewrite-min-size 64mb
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage no
aof-rewrite-incremental-fsync yes