主从复制可以将数据从一台数据库服务器(主服务器)复制到一台或多台其他数据库服务器(从服务器)。主人记录更新,然后波及到奴隶。从机输出一条消息,表明它已成功接收到更新,从而允许发送后续更新。主从复制可以是同步的也可以是异步的。区别只是变化传播的时间。如果同时对master和slave进行修改,则为同步。如果更改排队并稍后写入,则它是异步的。
复制 version : '3'
services :
redis-alpha :
image : redis:latest
restart : unless-stopped
container_name : redis-alpha
ports :
- 6379:6379
volumes :
- ./data_alpha:/data
redis-bravo :
image : redis:latest
restart : unless-stopped
container_name : redis-bravo
ports :
- 6380:6379
volumes :
- ./data_bravo:/data
redis-charli :
image : redis:latest
restart : unless-stopped
container_name : redis-charli
ports :
- 6381:6379
volumes :
- ./data_charli:/data
复制 127.0.0.1:6379 > info replication # 查看当前集群状态
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:531f747f2e473336f8d0a52605f0b312be20d7ec
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
复制 # docker-compose exec redis-bravo redis-cli
127.0.0.1:6379 > SLAVEOF redis-alpha 6379
OK
127.0.0.1:6379 > info replication
# Replication
role:slave
master_host:redis-alpha
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_read_repl_offset:0
slave_repl_offset:0
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:00e1c2bbaa1b45c810255d61e187d2d04bc55224
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0
# docker-compose exec redis-charli redis-cli
127.0.0.1:6379 > SLAVEOF redis-alpha 6379
OK
127.0.0.1:6379 > info replication
# Replication
role:slave
master_host:redis-alpha
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_read_repl_offset:126
slave_repl_offset:126
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:00e1c2bbaa1b45c810255d61e187d2d04bc55224
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:127
repl_backlog_histlen:0
复制 # docker-compose exec redis-alpha redis-cli
127.0.0.1:6379 > info replication
# Replication
role:master
connected_slaves:2
slave0:ip =172.21.0.2,port=6379,state=online,offset=154,lag=0
slave1:ip =172.21.0.4,port=6379,state=online,offset=154,lag=0
master_failover_state:no-failover
master_replid:00e1c2bbaa1b45c810255d61e187d2d04bc55224
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154