1. 程式人生 > >Redis主從配置及主從切換

Redis主從配置及主從切換

CA idf lis mon 分享圖片 說明 redis-cli dir nts

  環境及部署圖

技術分享圖片

技術分享圖片

  本次配置在同一臺主機配置文件及端口號不一樣

  6379配置文件/etc/redis/6379.conf

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
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
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

  6380配置文件/etc/redis/6380.conf

bind 0.0.0.0
protected-mode yes
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slaveof 10.1.43.212 6379
masterauth 123456
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
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
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

  說明:配置文件大部分相同不同的是端口號以及備配置文件增加以下兩行

slaveof 10.1.43.212 6379
masterauth 123456

  PS:如果主redis沒有配置密碼則不需要配置masterauth

  啟動兩個redis測試同步

redis-server /etc/redis/6379.conf 
redis-server /etc/redis/6380.conf 

  連接6379插入數據

  redis-cli -p 6379 -a 123456

127.0.0.1:6379> set name minseo
OK
127.0.0.1:6379> get name
"minseo"

  連接6380查看同步的數據

  redis-cli -p 6380 -a 123456

127.0.0.1:6380> get name
"minseo"

  默認是讀寫分離的主庫可寫從庫只能讀取

127.0.0.1:6380> set name 123
(error) READONLY You can‘t write against a read only slave.

  主從切換

  停止主庫

 src/redis-cli -n 6379 shutdown

  將從庫設置成主庫

redis-cli -p 6380 -a123456 slaveof NO ONE

  主庫恢復正常了

  從庫保存數據

127.0.0.1:6380> save
OK

  把根目錄下面的數據文件dump.rdb拷貝至主庫對應的目錄下面啟動原來的主庫

  從庫恢復成從庫

redis-cli -p 6380 slaveof 10.1.43.212 6379

  

Redis主從配置及主從切換