怎么玩:
1.配从库不赔主库
2.从库配置:slaceof 主库IP主库端口
在没有SLAVEOF之前,三个机器处于都是master的角色,但是当执行SLAVEOF之后,主机的角色就是role
,从机的角色就是slave,执行SLAVEOF之后,会把主机上的所有数据按照主从复制的原则复制一份,并且从机上不能够
修改和创建数据,只能在主机上进行创建和修改数据
主机
[[email protected] Desktop]$ su
Password:
[[email protected] Desktop]# cd /etc/redis
[[email protected] redis]# ls -l
total 192
-rw-r--r--. 1 root root 46710 Nov 23 14:52 redis6379.conf
-rw-r--r--. 1 root root 46710 Nov 23 14:55 redis6380.conf
-rw-r--r--. 1 root root 46710 Nov 23 14:57 redis6381.conf
-rw-r--r--. 1 root root 46698 Nov 21 17:36 redis.conf
[[email protected] redis]# redis-server redis6379.conf
[[email protected] redis]# redis-cli
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> set k3 v3
OK
127.0.0.1:6379> keys *
1) "k2"
2) "k1"
3) "k3"
127.0.0.1:6379> set k4 v4
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=501,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=501,lag=0
master_repl_offset:501
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:500
127.0.0.1:6379> set k6 v6
OK
从机1
[[email protected] Desktop]# redis-server /etc/redis/redis6380.conf
[[email protected] Desktop]# redis-cli
127.0.0.1:6379> exit
[[email protected] Desktop]# redis-server /etc/redis/redis6380.conf
[[email protected] Desktop]# redis-cli -p 6380
127.0.0.1:6380> INFO replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6380> clear
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6380> get k4
"v4"
127.0.0.1:6380> get k2
"v2"
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:6
master_sync_in_progress:0
slave_repl_offset:515
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6380> set k6 v66
(error) READONLY You can‘t write against a read only slave.
127.0.0.1:6380> get k6
"v6"
从机2
[[email protected] 123456]# redis-server /etc/redis/redis6381.conf
[[email protected] 123456]# redis-cli -p 6381
127.0.0.1:6381> INFO replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6381> get k4
"v4"
127.0.0.1:6381> get k1
"v1"
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:2
master_sync_in_progress:0
slave_repl_offset:529
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6380> set k6 v666
(error) READONLY You can‘t write against a read only slave.
127.0.0.1:6380> get k6
"v6"
当主机关机或者停止工作的时候角色变化:
127.0.0.1:6379> SHUTDOWN
not connected> exit
127.0.0.1:6380> keys *
1) "k2"
2) "k4"
3) "k1"
4) "k3"
5) "k6"
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:4338
master_link_down_since_seconds:18
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> INFO replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:4338
master_link_down_since_seconds:208
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
当主机重新开始的时候:
[[email protected] redis]# redis-server redis6379.conf
[[email protected] redis]# redis-cli
127.0.0.1:6379> set k7 v7
OK
从机1
127.0.0.1:6380> get k7
"v7"
从机2
127.0.0.1:6381> get k7
"v7"
主机正常工作但是其中一个从机坏了!每次与master断开,都需要重新连接,除非写进配置文件redis.conf中
127.0.0.1:6379> set k8 v8
OK
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=1703,lag=1
master_repl_offset:1703
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1702
127.0.0.1:6379> get k8
"v8"
从机重新开始连接,
[[email protected] Desktop]# redis-server /etc/redis/redis6380.conf
[[email protected] Desktop]# redis-cli -p 6380
127.0.0.1:6380> INFO replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6380> get k8
(nil)
主从复制的薪火相传
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=85,lag=0
master_repl_offset:85
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:84
127.0.0.1:6379> keys *
1) "k3"
2) "k4"
3) "k1"
4) "k7"
5) "k2"
6) "k6"
7) "k8"
127.0.0.1:6379> set k9 v9
OK
127.0.0.1:6379> get k9
"v9"
从机1;80是79的从机
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:9
master_sync_in_progress:0
slave_repl_offset:85
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=71,lag=1
master_repl_offset:71
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:70
127.0.0.1:6380> get k9
"v9"
81是80的从机:
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:10
master_sync_in_progress:0
slave_repl_offset:71
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> get k9
"v9"
主从复制之反客为主:当主机停止工作的时候,从机2SLAVEOF no one,从机3重新SLAVEOF 127.0.0.1 6380
[[email protected]calhost Desktop]# redis-server /etc/redis/redis.conf
[[email protected] Desktop]# redis-cli
127.0.0.1:6379> SHUTDOWN
not connected> EXIt
[[email protected] Desktop]# redis-server /etc/redis/redis6380.conf
[[email protected] Desktop]# redis-cli -p 6380
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6380> keys *
1) "k1"
2) "k6"
3) "k4"
4) "k3"
5) "k7"
6) "k2"
7) "k9"
8) "k8"
127.0.0.1:6380> SLAVEOF no one
OK
127.0.0.1:6380> clear
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6380> set k10 v10
OK
127.0.0.1:6380> get k10
"v10"
[[email protected] Desktop]# redis-server /etc/redis/redis6381.conf
[[email protected] Desktop]# redis-cli -p 6381
127.0.0.1:6381> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6381> keys *
1) "k1"
2) "k7"
3) "k3"
4) "k9"
5) "k4"
6) "k6"
7) "k2"
8) "k8"
127.0.0.1:6381> INFO replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:43
master_link_down_since_seconds:563
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> SLAVEOF 127.0.0.1 6380
OK
127.0.0.1:6381> get k10
"v10"
127.0.0.1:6381>
复制原理:
slave启动成功连接到master后会发送一个sync命令
Master接到命令启动后台的存盘进程,同时收集所有接收到的用于修改数据集命令,
在后台进程执行完毕之后,master将传送整个数据文件到slave,以完成一次完全同步
全量复制:而slave服务在接收到数据库文件数据后,将其存盘并加载到内存中。
增量复制:Master继续将新的所有收集到的修改命令依次传给slave,完成同步
但是只要是重新连接master,一次完全同步(全量复制)将被自动执行
哨兵配置:
[[email protected] redis]# vim sentinel.conf
[[email protected] redis]# redis-sentinel sentinel.conf
[[email protected] Desktop]# redis-server /etc/redis/redis.conf
[[email protected] Desktop]# redis-cli
127.0.0.1:6379> SHUTDOWN
not connected> exi
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit
[[email protected] Desktop]# redis-server /etc/redis/redis.conf
[[email protected] Desktop]# redis-cli
127.0.0.1:6379> keys *
1) "k10"
2) "k1"
3) "k9"
4) "k8"
5) "k6"
6) "k4"
7) "k2"
8) "k3"
9) "k7"
127.0.0.1:6379> SHUTDOWN
not connected> exit
[[email protected] Desktop]#
哨兵会监控到主机停止工作并且让从机2称为主机:
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=263864,lag=1
master_repl_offset:263864
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:263576
repl_backlog_histlen:289
127.0.0.1:6380> set k10 v10
OK
从机三成为2的从机:
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:1
master_sync_in_progress:0
slave_repl_offset:264130
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> get k10
"v10"
当主机1开始工作之后:
[[email protected] Desktop]# redis-server /etc/redis/redis.conf
[[email protected] Desktop]# redis-cli
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379> get k10
"v10"
原文地址:https://www.cnblogs.com/mxf97826/p/8687664.html