簡介 從庫配置 主從同步一 一主多從(同一臺機器上同一個redis運行在三個port上) 1.複製redis配置文件三份 2.啟動三個redis 3.細節(暫時一主一從) redis配置文件中帶有密碼時即requirepass時,如果配置文件不特殊配置(masterauth不配置),此處為常規配置 ...
簡介
主機數據更新後根據配置和策略,自動同步到備機的master/slave機制,master以寫為主,slave以讀為主
從庫配置
配置從庫,不配主庫
配置從庫:
格式: slaveof 主庫ip 主庫port
註意: 每次與master斷開之後,都需要重新連接,除非配置到redis.conf文件
配置文件細節:
主從同步一--一主多從(同一臺機器上同一個redis運行在三個port上)
1.複製redis配置文件三份
[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# mkdir test-master-slave
[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# cp redis.conf test-master-slave/redis6379.conf
[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# cp redis.conf test-master-slave/redis6380.conf
[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# cp redis.conf test-master-slave/redis6381.conf
修改另外兩份配置文件
vim redis6379.conf
daemonize yes
port 6379
pidfile /var/run/redis_6379.pid
logfile "6379.log"
dbfilename dump6379.rdb
vim redis6380.conf
daemonize yes
port 6380
pidfile /var/run/redis_6380.pid
logfile "6380.log"
dbfilename dump6380.rdb
vim redis6381.conf
daemonize yes
port 6381
pidfile /var/run/redis_6381.pid
logfile "6381.log"
dbfilename dump6381.rdb
2.啟動三個redis
[root@izm5e2q95pbpe1hh0kkwoiz test-master-slave]# cd /alidata/redis-5.0.3/bin
[root@izm5e2q95pbpe1hh0kkwoiz bin]# pwd
/alidata/redis-5.0.3/bin
[root@izm5e2q95pbpe1hh0kkwoiz bin]# ps aux | grep redis
root 20603 0.1 0.1 156392 2284 ? Ssl 2018 681:54 redis-server *:8686
root 21288 0.0 0.0 112680 984 pts/0 R+ 10:58 0:00 grep --color=auto redis
[root@izm5e2q95pbpe1hh0kkwoiz bin]# redis-server ../test-master-slave/redis6380.conf
[root@izm5e2q95pbpe1hh0kkwoiz bin]# ps aux | grep redis
root 20603 0.1 0.1 156392 2284 ? Ssl 2018 681:54 redis-server *:8686
root 23628 0.0 0.1 153832 2324 ? Ssl 10:58 0:00 redis-server 127.0.0.1:6380
root 24492 0.0 0.0 112680 980 pts/0 R+ 10:58 0:00 grep --color=auto redis
[root@izm5e2q95pbpe1hh0kkwoiz bin]# redis-server ../test-master-slave/redis6381.conf
[root@izm5e2q95pbpe1hh0kkwoiz bin]# ps aux | grep redis
root 20603 0.1 0.1 156392 2284 ? Ssl 2018 681:54 redis-server *:8686
root 23628 0.0 0.1 153832 2324 ? Ssl 10:58 0:00 redis-server 127.0.0.1:6380
root 27142 0.0 0.1 153832 2304 ? Ssl 10:58 0:00 redis-server 127.0.0.1:6381
root 28470 0.0 0.0 112680 984 pts/0 R+ 10:58 0:00 grep --color=auto redis
[root@izm5e2q95pbpe1hh0kkwoiz bin]# redis-server ../test-master-slave/redis6379.conf
[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# ps aux | grep redis
root 4705 0.1 0.1 153832 2592 ? Ssl 11:11 0:00 redis-server 127.0.0.1:6380
root 20603 0.1 0.1 156392 2296 ? Ssl 2018 681:55 redis-server *:8686
root 22492 0.0 0.1 153832 2296 ? Ssl 11:14 0:00 redis-server 127.0.0.1:6379
root 23990 0.0 0.0 112680 980 pts/0 R+ 11:14 0:00 grep --color=auto redis
root 27142 0.0 0.1 153832 2304 ? Ssl 10:58 0:00 redis-server 127.0.0.1:6381
3.細節(暫時一主一從)
# 主機器
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:d9ff48134d9b166cb7f48f4f71a54c9eddc8531c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# 還沒有配置的從機器
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_replid:e06824b502f2bc16c101653a8e36ba954cf35a53
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# 配置從redis
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
# 此時master redis狀態
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=28,lag=0
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
redis配置文件中帶有密碼時即requirepass時,如果配置文件不特殊配置(masterauth不配置),此處為常規配置-->配置文件要將bind配置為0.0.0.0,使用slaveof命令創建主從關係會失敗master_link_status:down.不帶有密碼的時候,配置文件也要將bind配置為0.0.0.0,否則還是會失敗master_link_status:down
redis的master跟slave建立主從關係之後,slave redis會從master的redis獲取所有的key
如果master redis down掉,slave redis會繼續等待,master redis運行後,slave redis自動連接
如果slave redis down掉之後,slave redis重新啟動,不會自動連接master redis,需要重新運行slaveof命令
master redis寫入的key,slave redis不能去修改master已經存在的key,在slave中是readonly的
主從同步二--薪火相傳
簡介
上一個slave可以是下一個slave的master,slave同樣可以接受其他slaves的連接和同步請求.
那麼該slave作為鏈條中下一個的master,可以有效減輕master的寫壓力
中途變更轉向:會清除之前的數據,重新建立拷貝最新的.
slaveof 新主庫ip 新主庫port
細節
# 主master
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=28,lag=0
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
# master6379的slave6380
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:2142
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=2142,lag=1
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2142
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2142
# slave6380的slave6381
127.0.0.1:6381> SLAVEOF 127.0.0.1 6380
OK
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:2128
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2128
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2129
repl_backlog_histlen:0
主從複製三--反客為主
簡介
從庫運行slaveof no one ,使當前資料庫停止與其他資料庫的同步,轉成主資料庫
其他的從庫使用"slaveof 新主庫ip 新主庫port",重新綁定最新的主庫
複製原理
slave啟動成功,連接到master後會發送一個sync命令
master接到命令啟動後臺的存檔進程,同時手機所有接收到的用於修改數據命令,
在後臺進程執行完畢之後,master將傳送整個數據文件到slave,以完成一次完全同步
全量複製: 而slave在接收到資料庫文件之後,將其存檔並載入到記憶體中
增量複製: master繼續將新的所有收集到的修改命令依次傳遞給slave,完成同步
但是只要是重新連接master,一次完全同步(全量複製)將被自動執行
哨兵模式(sentinel)
參考鏈接: https://redis.io/topics/sentinel
簡介
反客為主的自動版,能夠後退監控主機是否故障,如果故障了根據投票數自動將從庫轉換為主庫
如何操作
1.調整結構6379帶著6380,6381
2.自定義/alidata/redis-5.0.3/test-master-slave目錄下新建sentinel.conf文件,名字絕對不能錯
3.配置哨兵
sentinel monitor 被監控資料庫名字(自己定義) 被監控資料庫ip 被監控資料庫port 1
上面最後一個數字1,表示主機掛掉後slave投票讓誰接替成為主機,得票數多少後成為主機
4.啟動哨兵
redis-sentinel /alidata/redis-5.0.3/test-master-slave/sentinel.conf
如果沒有redis-sentinel,則在/alidata/redis-5.0.3/bin目錄下使用如下命令:
redis-server ../test-master-slave/sentinel.conf --sentinel
[root@izm5e2q95pbpe1hh0kkwoiz bin]# redis-server ../test-master-slave/sentinel.conf --sentinel
16452:X 10 Jan 2020 15:56:54.511 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16452:X 10 Jan 2020 15:56:54.511 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=16452, just started
16452:X 10 Jan 2020 15:56:54.511 # Configuration loaded
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 16452
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
16452:X 10 Jan 2020 15:56:54.516 # Sentinel ID is a7d6715b13d229862bd5082e1f0e7734897a1693
16452:X 10 Jan 2020 15:56:54.516 # +monitor master host6379 127.0.0.1 6379 quorum 1
16452:X 10 Jan 2020 15:56:54.517 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:56:54.520 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
5.原有的master掛掉之後,自動重新選擇master,另外的slave重新掛到新的master下,6379掛掉,投票產生新的master為6381
16452:X 10 Jan 2020 15:59:43.676 # +odown master host6379 127.0.0.1 6379 #quorum 1/1
16452:X 10 Jan 2020 15:59:43.676 # +new-epoch 1
16452:X 10 Jan 2020 15:59:43.676 # +try-failover master host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:43.682 # +vote-for-leader a7d6715b13d229862bd5082e1f0e7734897a1693 1
16452:X 10 Jan 2020 15:59:43.682 # +elected-leader master host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:43.682 # +failover-state-select-slave master host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:43.745 # +selected-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:43.745 * +failover-state-send-slaveof-noone slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:43.809 * +failover-state-wait-promotion slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:44.588 # +promoted-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:44.588 # +failover-state-reconf-slaves master host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:44.660 * +slave-reconf-sent slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:45.615 * +slave-reconf-inprog slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:45.615 * +slave-reconf-done slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
16452:X 10 Jan 2020 15:59:45.691 # +failover-end master host6379 127.0.0.1 6379
## 切換到投票出來的新的master之6381
16452:X 10 Jan 2020 15:59:45.691 # +switch-master host6379 127.0.0.1 6379 127.0.0.1 6381
16452:X 10 Jan 2020 15:59:45.691 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
16452:X 10 Jan 2020 15:59:45.691 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:00:15.768 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
## 6381變為新的master,6380為slave,只有6380一個slave
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=23490630,lag=0
master_replid:9772fad6a5ca29661aeb5e07ca72c013347c5679
master_replid2:df2697f1f530565d1fb683f209283e319faea59f
master_repl_offset:23490630
second_repl_offset:23480570
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:22442055
repl_backlog_histlen:1048576
## 6380是6381的slave
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:23491043
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9772fad6a5ca29661aeb5e07ca72c013347c5679
master_replid2:df2697f1f530565d1fb683f209283e319faea59f
master_repl_offset:23491043
second_repl_offset:23480570
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:22442468
repl_backlog_histlen:1048576
6.最先掛掉的master重新運行之後,自動掛載到重新選舉出來的master下麵,此時最原先的master變為slave
哨兵檢測到後將6379啟動,將6379變為6381的slave:
16452:X 10 Jan 2020 16:04:28.076 * +convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
6381下的slave多了6379
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=23501237,lag=2
slave1:ip=127.0.0.1,port=6379,state=online,offset=23501237,lag=1
master_replid:9772fad6a5ca29661aeb5e07ca72c013347c5679
master_replid2:df2697f1f530565d1fb683f209283e319faea59f
master_repl_offset:23501237
second_repl_offset:23480570
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:22452662
repl_backlog_histlen:1048576
6379的info replication
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:23507614
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9772fad6a5ca29661aeb5e07ca72c013347c5679
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:23507614
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:23499724
repl_backlog_histlen:7891
7.假如此時投票選舉出來的master又掛掉了,此時是什麼情況?
哨兵檢測到6381掛掉之後的動作
16452:X 10 Jan 2020 16:07:53.458 # +sdown master host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:53.458 # +odown master host6379 127.0.0.1 6381 #quorum 1/1
16452:X 10 Jan 2020 16:07:53.458 # +new-epoch 2
16452:X 10 Jan 2020 16:07:53.458 # +try-failover master host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:53.463 # +vote-for-leader a7d6715b13d229862bd5082e1f0e7734897a1693 2
16452:X 10 Jan 2020 16:07:53.463 # +elected-leader master host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:53.463 # +failover-state-select-slave master host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:53.523 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:53.523 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:53.581 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:53.643 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:53.643 # +failover-state-reconf-slaves master host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:53.694 * +slave-reconf-sent slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:54.323 * +slave-reconf-inprog slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:55.333 * +slave-reconf-done slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
16452:X 10 Jan 2020 16:07:55.386 # +failover-end master host6379 127.0.0.1 6381
## 將master從6381切換為6380
16452:X 10 Jan 2020 16:07:55.386 # +switch-master host6379 127.0.0.1 6381 127.0.0.1 6380
16452:X 10 Jan 2020 16:07:55.386 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380
16452:X 10 Jan 2020 16:07:55.386 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6380
16452:X 10 Jan 2020 16:08:25.418 # +sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6380
6380變為master,6379變更為6380的slave:
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6379,state=online,offset=23512243,lag=0
master_replid:08b69c14d46567b6d0506e84e7c51da49a9661ec
master_replid2:9772fad6a5ca29661aeb5e07ca72c013347c5679
master_repl_offset:23512243
second_repl_offset:23511276
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:22463668
repl_backlog_histlen:1048576
6379變為6380的slave:
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:23525130
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:08b69c14d46567b6d0506e84e7c51da49a9661ec
master_replid2:9772fad6a5ca29661aeb5e07ca72c013347c5679
master_repl_offset:23525130
second_repl_offset:23511276
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:23499724
repl_backlog_histlen:25407
複製缺點
由於所有的寫操作都是現在master上操作,然後同步更新到slave上,所以master通不到slave機器有一定的延遲,
當系統很繁忙的時候,延遲問題會更加嚴重,slave機器數量的增加也會使這個問題更加嚴重