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文件,更改相关配置
- 端口
- dump.rdb
- daemonize yes
- pidfile
- 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