主从复制

image
image
image
127.0.0.1:6379> info Replication

Replication

role:master # 角色
connected_slaves:0 # 没有从机
master_failover_state:no-failover
master_replid:4cddd7999db3811e228e85de9a53edb21ed6504a
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:6379>
复制redis.conf文件,更改相关配置

  1. 端口
  2. dump.rdb
  3. daemonize yes
  4. pidfile
  5. log文件名字
    启动服务

(ENV) ☁ etc redis-server redis79.conf
(ENV) ☁ etc ps aux|grep redis
donghao 23413 0.2 0.0 4329408 1740 ?? Ss 11:24PM 0:00.01 redis-server 127.0.0.1:6379
donghao 23384 0.1 0.0 4348864 1852 ?? Ss 11:24PM 0:00.08 redis-server 127.0.0.1:6381
donghao 23378 0.1 0.0 4347840 1788 ?? Ss 11:24PM 0:00.09 redis-server 127.0.0.1:6380
客户端手动配置 使用SLAVEOF, 从机器查看

127.0.0.1:6381> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6381> info replication

Replication

role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:28
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:0254a22babb1185f5a1b98043eae658e5f71b81a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:29
repl_backlog_histlen:0
主机查看

127.0.0.1:6379> info replication

Replication

role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=98,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=98,lag=1
master_failover_state:no-failover
master_replid:0254a22babb1185f5a1b98043eae658e5f71b81a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98
从机只能读

127.0.0.1:6380> set key1 v1
(error) READONLY You can't write against a read only replica.
如果主机断了,信息还在, 但是没有写操作了,如果主机恢复,从机可以重新获取数据

127.0.0.1:6380> info replication

Replication

role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
如果命令行配置成从机,如果宕机了重新启动会成为master主机

复制原理 image

层层链路 6379master-> 6380 slave ->6381 slave

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:670
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=670,lag=1
master_failover_state:no-failover
master_replid:f96def35521fdfde7f64b50ad13558b8caffd272
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:670
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:670
恢复成主节点(手动): SLAVEOF no one

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值