Redis的主从复制

一、主从复制是什么

主机数据更新后根据配置和策略, 自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主

二、主从复制怎么做

1、拷贝多个redis.conf文件(并重新命名例:redis6379.conf)

拷贝时记得要写全路径

cp -r redis.conf /opt/soft/myredis/reids6379.conf
cp -r redis.conf /opt/soft/myredis/reids6380.conf
cp -r redis.conf /opt/soft/myredis/reids6381.conf

2、分别修改配置文件

这里只示例redis6379.conf (这里的配置是在安装配置的基础上进行)

pidfile /var/run/redis_6379.pid(291行)
port 6379(98行)
dbfilename dump6379.rdb(433行)

如果在Redis安装过程中配置了Redis的密码,那就再次修改以下配置

masterauth 123456(486行)

3、启动三个Redis服务器

在哪里启动服务器,RDB、AOF文件就会保存到哪里

usr/local/bin/redis-server /myredis/redis6379.conf
usr/local/bin/redis-server /myredis/redis6380.conf
usr/local/bin/redis-server /myredis/redis6381.conf

4、查看系统进程,看三台Redis是否启动成功

ps -ef | grep redis

如果正常启动即为如下所示

root 58188 1 0 07:26 ? 00:00:00 ./redis-server *:6379
root 58194 1 0 07:26 ? 00:00:00 ./redis-server *:6380
root 58200 1 0 07:26 ? 00:00:00 ./redis-server *:6381
root 58209 58052 0 07:27 pts/0 00:00:00 grep --color=auto redis

5、查看三台主机运行情况

info replication #打印主从复制的相关信息

6379服务器信息:

[root@iZ2ze2rikqlh3ql1n2cw61Z bin]# ./redis-cli -p 6379
127.0.0.1:6379> info replication
NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:fbd16328dcd4296f8347b0b15934aced272f01b3
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

6380服务器信息:

[root@iZ2ze2rikqlh3ql1n2cw61Z bin]# ./redis-cli -p 6380
127.0.0.1:6380> auth 123456
OK
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:934a431bc633b5078ac1fc9fb890fca9b5ef6408
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

6381服务器信息:

[root@iZ2ze2rikqlh3ql1n2cw61Z bin]# ./redis-cli -p 6381
127.0.0.1:6381> auth 123456
OK
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:8d00aa536b59c128813c348b35eced8d4486f09c
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

6、配置主从(配从不陪主)

slaveof  <ip><port> # 成为某个实例的从服务器

例如:slaveof localhost:6379
将两个从机全部配置

7、全部配置完成后再次查看三台服务器的信息

info replication #打印主从复制的相关信息

6379服务器信息:

# ----------执行后查看master----------
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:fbd16328dcd4296f8347b0b15934aced272f01b3
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

6380服务器信息:

# ----------执行后查看slave6380----------
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:down
master_last_io_seconds_ago:-1
master_sync_in_progress:1
slave_read_repl_offset:1
slave_repl_offset:1
master_sync_total_bytes:-1
master_sync_read_bytes:0
master_sync_left_bytes:-1
master_sync_perc:-0.00
master_sync_last_io_seconds_ago:0
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:934a431bc633b5078ac1fc9fb890fca9b5ef6408
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

6381服务器信息:

# ----------执行后查看slave6381----------
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_read_repl_offset:1
slave_repl_offset:1
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:8d00aa536b59c128813c348b35eced8d4486f09c
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

经过以上设置,我们的主从配置已经完成了

  • 在主机上写,在从机上可以读取数据
  • 在从机上写数据报错
  • 主机挂掉,重启就行,一切如初
  • 从机重启需重设:slaveof 127.0.0.1 6379(可以将配置增加到文件中。永久生效)

注:以上配置为我们常见的一主二仆,如果要配置薪火相传便将6380从6379,6381从6380即可

三、哨兵模式

1、哨兵模式是什么

能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库。

2、配置

(1)调整服务器主从关系

首先将三台服务器调整为一仆二主(即为6380和6381都从6379)

(2)配置哨兵,填写内容

在自定义的/myredis目录下新建sentinel.conf文件,名字绝不能错

在文件中填入如下内容

#其中mymaster为监控对象起的服务器名称, 1 为至少有多少个哨兵同意迁移的数量。 
sentinel monitor mymaster 127.0.0.1 6379 1

(3)启动哨兵

/usr/local/bin/redis-sentinel /opt/soft/myredis/sentinel.conf

启动结果如下:

[root@iZ2ze2rikqlh3ql1n2cw61Z myredis]# /usr/local/bin/redis-sentinel /opt/soft/myredis/sentinel.conf
416739:X 19 Oct 2022 21:56:09.750 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
416739:X 19 Oct 2022 21:56:09.750 # Redis version=6.2.7, bits=64, commit=00000000, modified=0, pid=416739, just started
416739:X 19 Oct 2022 21:56:09.750 # Configuration loaded
416739:X 19 Oct 2022 21:56:09.750 * monotonic clock: POSIX clock_gettime
416739:X 19 Oct 2022 21:56:09.750 # A key '__redis__compare_helper' was added to Lua globals which is not on the globals allow list nor listed on the deny list.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 416739
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

416739:X 19 Oct 2022 21:56:09.754 # Sentinel ID is a58b60c40d7efd988a548f5f23995a15cedf8e16
416739:X 19 Oct 2022 21:56:09.754 # +monitor master mymaster 127.0.0.1 6379 quorum 1
416739:X 19 Oct 2022 21:56:09.754 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
416739:X 19 Oct 2022 21:56:09.757 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379

此时启动再启动三台服务器配置主从,当主服务器宕机后,哨兵会在从服务器中选出一台作为新的主机。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java.L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值