主从复制
一主一从结构
[ root@host61 ~]
[ root@host61 ~]
bind 192.168 .88.61
port 6379
[ root@host61 ~]
[ root@host61 ~]
192.168 .88.61:6379 > info replication
role:master
connected_slaves:0
master_replid:0aebff37df915061ddf157cc33d8742f14e8773f
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
[ root@host62 ~]
[ root@host62 ~]
bind 192.162 .88.62
port 6379
:wq
[ root@host62 ~]
[ root@host62 ~]
192.162 .88.62.:6379 > replicaof 192.162 .88.61 6379
OK
192.162 .88.62.:6379 > config rewrite
OK
192.162 .88.62.:6379 > exit
[ root@host62 ~]
replicaof 192.162 .88.61 6379
[ root@host62 ~]
192.162 .88.62.:6379 > info replication
role:slave
master_host:192.162.88.61
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:476ae25abdb72c60a4ada0dcc73910a7455ecac1
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
[ root@host61 ~]
192.168 .88.61:6379 > info replication
role:master
connected_slaves:1
slave0:ip= 192.168 .88.62,port= 6379 ,state= online,offset= 322 ,lag= 0
master_replid:476ae25abdb72c60a4ada0dcc73910a7455ecac1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:322
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:322
192.168 .88.61:6379 > mset x 1 y 2 z 3
OK
192.168 .88.61:6379 > keys *
1 ) "z"
2 ) "y"
3 ) "x"
[ root@host62 ~]
192.168 .88.62.:6379 > keys *
1 ) "y"
2 ) "x"
3 ) "z"
一主多从结构
[ root@host63 ~]
[ root@host63 ~]
bind 192.168 .88.63
port 6379
[ root@host63 ~]
[ root@host63 ~]
192.168 .88.63:6379 > replicaof 192.168 .88.61 6379
OK
192.168 .88.63:6379 > config rewrite
OK
192.168 .88.63:6379 > exit
[ root@host63 ~]
replicaof 192.168 .88.61 6379
[ root@host63 ~]
192.168 .88.63:6379 > info replication
role:slave
master_host:192.168.88.61
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:1171
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:476ae25abdb72c60a4ada0dcc73910a7455ecac1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1171
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1158
repl_backlog_histlen:14
192.168 .88.63:6379 > keys *
1 ) "x"
2 ) "z"
3 ) "y"
[ root@host61 ~]
192.168 .88.61:6379 > info replication
role:master
connected_slaves:2
slave0:ip= 192.168 .88.62,port= 6379 ,state= online,offset= 3027 ,lag= 0
slave1:ip= 192.168 .88.63,port= 6379 ,state= online,offset= 3013 ,lag= 1
master_replid:209b3b7ce20c708ab1b6b95a5d1becb2454b0941
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3027
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3027
主从从结构
[ root@host63 ~]
192.168 .88.63:6379 > replicaof no one
OK
192.168 .88.63:6379 > replicaof 192.168 .88.62 6379
OK
192.168 .88.63:6379 > config rewrite
OK
192.168 .88.63:6379 > exit
[ root@host63 ~]
replicaof 192.168 .88.62 6379
[ root@host63 ~]
192.168 .88.63:6379 > info replication
role:slave
master_host:192.168.88.62
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:4545
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:476ae25abdb72c60a4ada0dcc73910a7455ecac1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4545
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4420
repl_backlog_histlen:126
[ root@host62 ~]
192.168 .88.62.:6379 > info replication
role:slave
master_host:192.168.88.61
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:4769
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip= 192.168 .88.63,port= 6379 ,state= online,offset= 4769 ,lag= 0
master_replid:476ae25abdb72c60a4ada0dcc73910a7455ecac1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4769
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:4769
- 配置主机host61,redis服务连接密码为123456
- 主机host62设置连接host61的redis服务密码
[ root@host61 ~]
192.168 .88.61:6379 > config get requirepass
1 ) "requirepass"
2 ) ""
192.168 .88.61:6379 > config set requirepass 123456
OK
192.168 .88.61:6379 > auth 123456
OK
192.168 .88.61:6379 > config rewrite
OK
192.168 .88.61:6379 > exit
[ root@host61 ~]
requirepass "123456"
[ root@host61 ~]
192.168 .88.61:6379 > auth 123456
OK
192.168 .88.61:6379 > info replication
role:master
connected_slaves:0
master_replid:476ae25abdb72c60a4ada0dcc73910a7455ecac1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5763
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5763
[ root@host62 ~]
192.168 .88.62.:6379 > info replication
role:slave
master_host:192.168.88.61
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:274
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:476ae25abdb72c60a4ada0dcc73910a7455ecac1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:7803
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:7803
192.168 .88.62:6379 > config set masterauth 123456
OK
192.168 .88.62:6379 > config rewrite
OK
192.168 .88.62:6379 > info replication
role:slave
master_host:192.168.88.61
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:5847
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip= 192.168 .88.63,port= 6379 ,state= online,offset= 5847 ,lag= 1
master_replid:476ae25abdb72c60a4ada0dcc73910a7455ecac1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5847
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:5764
repl_backlog_histlen:84
哨兵服务
- 配置主机Host67为master服务器
- 配置主机host68为Host67的slave服务器
- 配置主机host69运行哨兵服务
---
说明:
1 )可以使用一主一从或 一主多从 或 主从从 + 哨兵服务 做服务的高可用 和 数据自动备份
2 )如果主从结构中的redis服务设置连接密码的话必须全每台数据库都要设置密码且密码要一样,要么全都不设置密码。
3 )如果Redis服务有密码宕机的服务器启动服务后,要人为指定主服务器的连接密码。
---
[ root@host67 ~]
[ root@host67 ~]
bind 192.168 .88.67
port 6379
[ root@host67 ~]
[ root@host67 ~]
192.168 .88.67:6379 > info replication
role:master
connected_slaves:0
master_replid:0aebff37df915061ddf157cc33d8742f14e8773f
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
[ root@host68 ~]
[ root@host68 ~]
bind 192.168 .88.68
port 6379
[ root@host68 ~]
[ root@host68 ~]
192.168 .88.68:6379 > replicaof 192.168 .88.67 6379
OK
192.168 .88.68:6379 > config rewrite
OK
192.168 .88.68:6379 > exit
[ root@host68 ~]
replicaof 192.168 .88.67 6379
[ root@host68 ~]
192.168 .88.68:6379 > info replication
role:slave
master_host:192.168.88.67
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:476ae25abdb72c60a4ada0dcc73910a7455ecac1
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
]
]
bind 192.168 .88.69
port 26379
daemonize yes
sentinel monitor mymaster 192.168 .88.67 6379 1
[ root@host69 ~]
[ root@host69 ~]
tcp 0 0 192.168 .88.69:26379 0.0 .0.0:* LISTEN 1651 /redis-sentinel
[ root@host69 ~]
1651 :X 03 Jun 2023 21 :42:49.523
1651 :X 03 Jun 2023 21 :42:49.523
1651 :X 03 Jun 2023 21 :42:49.523
1651 :X 03 Jun 2023 21 :42:49.523 * supervised by systemd, will signal readiness
1651 :X 03 Jun 2023 21 :42:49.525 * Running mode = sentinel, port = 26379 .
1651 :X 03 Jun 2023 21 :42:49.525
1651 :X 03 Jun 2023 21 :42:49.530
1651 :X 03 Jun 2023 21 :42:49.530
1651 :X 03 Jun 2023 21 :42:49.533 * +slave slave 192.168 .88.68:6379 192.168 .88.68 6379 @ mymaster 192.168 .88.67 6379
[ root@host67 ~]
[ root@host68 ~]
192.168 .88.68:6379 > info replication
role:master
connected_slaves:0
master_replid:e4d9399cdf19c724b032859e77316019735d391e
master_replid2:bd1b8d193de170bedd8b40780ee3fb45f9590a5b
master_repl_offset:35343
second_repl_offset:32642
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:183
repl_backlog_histlen:35161
[ root@host69 ~]
1651 :X 03 Jun 2023 21 :50:23.603
1651 :X 03 Jun 2023 21 :50:23.694
1651 :X 03 Jun 2023 21 :50:23.695 * +failover-state-send-slaveof-noone slave 192.168 .88.68:6379 192.168 .88.68 6379 @ mymaster 192.168 .88.67 6379
1651 :X 03 Jun 2023 21 :50:23.796 * +failover-state-wait-promotion slave 192.168 .88.68:6379 192.168 .88.68 6379 @ mymaster 192.168 .88.67 6379
1651 :X 03 Jun 2023 21 :50:24.452
1651 :X 03 Jun 2023 21 :50:24.452
1651 :X 03 Jun 2023 21 :50:24.502
1651 :X 03 Jun 2023 21 :50:24.502
1651 :X 03 Jun 2023 21 :50:24.502 * +slave slave 192.168 .88.67:6379 192.168 .88.67 6379 @ mymaster 192.168 .88.68 6379
1651 :X 03 Jun 2023 21 :50:54.586
[ root@host67 ~]
[ root@host67 ~]
192.168 .88.67:6379 > info replication
role:slave
master_host:192.168.88.68
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:54542
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:e4d9399cdf19c724b032859e77316019735d391e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:54542
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:54379
repl_backlog_histlen:164