redis的主从复制和哨兵模式

常用命令解释

以下命令是根据文章总结出来的命令

# 通过指定配置文件的方式启动redis
redis-server conf/redis81.conf  

# redis-server启动后,通过指定端口的形式,启动redis客户端
redis-cli -p 6381

#  查看集群的配置信息
127.0.0.1:6381> info replication

# slaveof命令用来在主从复制模式中添加master节点
# 用法是 slaveof host 端口
# 示例
127.0.0.1:6381> slaveof 127.0.0.1 6379


# 哨兵模式启动
redis-sentinel conf/sentinel.conf

redis安装

安装gcc

# gcc环境安装
yum install gcc-c++

安装redis

# 在服务器新建目录,有关数据库的文件都存放到这下面
mkdir /opt/data-base
 
 # 下载命令
cd /opt/data-base
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
tar xzf redis-6.2.6.tar.gz
cd redis-6.2.6
make
make install


# redis本地的安装目录
cd /usr/local/bin

# 拷贝redis安装包里面的配置文件 到 本地安装目录,以后启动redis,就用本地的这个配置文件
 mkdir conf
 cp /opt/data-base/redis-6.2.6/redis.conf conf/


redis启动

## 修改启动为后台模式
vim  redis.conf
## 修改以下配置为yes
daemonize yes

# 启动服务
redis-server conf/redis.conf

# 启动客户端
[root@localhost bin]# redis-cli -p 6379
127.0.0.1:6379> 

集群环境配置

配置一主二从模式的环境,需要三台服务器,本次演示把配置文件复制三份,模拟三台服务器

# 复制三台服务器文件
[root@localhost conf]# cp redis.conf redis79.conf
[root@localhost conf]# cp redis.conf redis80.conf
[root@localhost conf]# cp redis.conf redis81.conf


# 使用vim命令修改 [redis端口.conf] 中的以下内容
port 6381
pidfile /var/run/redis_6381.pid
logfile "6381.log"
dbfilename dump81.rdb


## 启动redis,
redis-server conf/redis80.conf

## 查看三个日志文件,即可说明正常启动
-rw-r--r--. 1 root root      659 62 11:15 6379.log
-rw-r--r--. 1 root root     1084 62 11:15 6380.log
-rw-r--r--. 1 root root     1084 62 11:15 6381.log

主从复制

命令版

默认情况下,所有的机器都是master主机,主从复制模式,只需要配置从机即可

主机负责写操作,从机负责读操作

三个配置文件,redis79.conf、redis80.conf、redis81.conf

本次默认79为主机,80、81为从机

# 使用slaveof 命令修改从机
127.0.0.1:6381> slaveof 127.0.0.1 6379
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=154,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=154,lag=0
master_failover_state:no-failover
master_replid:4176a6750e36e6faabd588b5bb67b2e5e931a303
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154


# 从机信息查看
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:4
master_sync_in_progress:0
slave_read_repl_offset:336
slave_repl_offset:336
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:4176a6750e36e6faabd588b5bb67b2e5e931a303
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:336
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:155
repl_backlog_histlen:182

配置文件版

在从机的redis.conf文件中,找到如下配置

#   +------------------+      +---------------+
#   |      Master      | ---> |    Replica    |
#   | (receive writes) |      |  (exact copy) |
#   +------------------+      +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition replicas automatically try to reconnect to masters
#    and resynchronize with them.
#
# replicaof <masterip> <masterport>  # 这里配置主机的host和port

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>  # 这里配置主机的密码
#
# However this is not enough if you are using Redis ACLs (for Redis version

验证主从复制

# 在master主机写入一个key,在从机可以查到,说明环境配置没问题
127.0.0.1:6379> set name zs
OK


# 从机查看
127.0.0.1:6380> keys *
1) "name"


# 直接在从机上面写key,会报错
127.0.0.1:6380> set age 20
(error) READONLY You can't write against a read only replica.


哨兵模式

文件配置

在conf文件夹下,用vim命令新建一个名称为sentinel.conf的文件,内容如下

# sentinel monitor 别名 host port  1表示需要几个哨兵去投票决定选举新的主机
sentinel monitor myredis 127.0.0.1 6379 1

启动哨兵

[root@localhost bin]# redis-sentinel conf/sentinel.conf 
23163:X 02 Jun 2024 12:02:51.081 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23163:X 02 Jun 2024 12:02:51.081 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=23163, just started
23163:X 02 Jun 2024 12:02:51.081 # Configuration loaded
23163:X 02 Jun 2024 12:02:51.082 * Increased maximum number of open files to 10032 (it was originally set to 1024).
23163:X 02 Jun 2024 12:02:51.082 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 23163
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

23163:X 02 Jun 2024 12:02:51.083 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
23163:X 02 Jun 2024 12:02:51.190 # Sentinel ID is 8c75c241d902a7121dfe785015d4b40a109846de
23163:X 02 Jun 2024 12:02:51.190 # +monitor master myredis 127.0.0.1 6379 quorum 1
# ------------------------检测到主机中的两个从机----------------------------------
23163:X 02 Jun 2024 12:02:51.191 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
23163:X 02 Jun 2024 12:02:51.205 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379

验证哨兵模式

前提: 6379、6380、6381三台机器都启动中

首先关闭6379主机

127.0.0.1:6379> shutdown
not connected>

查看哨兵日志

[root@localhost bin]# redis-sentinel conf/sentinel.conf 
7471:X 02 Jun 2024 12:09:44.738 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7471:X 02 Jun 2024 12:09:44.739 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=7471, just started
7471:X 02 Jun 2024 12:09:44.739 # Configuration loaded
7471:X 02 Jun 2024 12:09:44.739 * Increased maximum number of open files to 10032 (it was originally set to 1024).
7471:X 02 Jun 2024 12:09:44.739 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 7471
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

7471:X 02 Jun 2024 12:09:44.740 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7471:X 02 Jun 2024 12:09:44.740 # Sentinel ID is 8c75c241d902a7121dfe785015d4b40a109846de
7471:X 02 Jun 2024 12:09:44.740 # +monitor master myredis 127.0.0.1 6379 quorum 1
7471:X 02 Jun 2024 12:11:29.300 # +sdown master myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:29.300 # +odown master myredis 127.0.0.1 6379 #quorum 1/1
7471:X 02 Jun 2024 12:11:29.300 # +new-epoch 1
7471:X 02 Jun 2024 12:11:29.300 # +try-failover master myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:29.336 # +vote-for-leader 8c75c241d902a7121dfe785015d4b40a109846de 1
7471:X 02 Jun 2024 12:11:29.336 # +elected-leader master myredis 127.0.0.1 6379
#  ========================================主机异常了========================================
7471:X 02 Jun 2024 12:11:29.336 # +failover-state-select-slave master myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:29.434 # +selected-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:29.434 * +failover-state-send-slaveof-noone slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:29.498 * +failover-state-wait-promotion slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:30.073 # +promoted-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:30.073 # +failover-state-reconf-slaves master myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:30.073 * +slave-reconf-sent slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:31.058 * +slave-reconf-inprog slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:31.058 * +slave-reconf-done slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
7471:X 02 Jun 2024 12:11:31.135 # +failover-end master myredis 127.0.0.1 6379
# ========================================选举了81为主机 ========================================
7471:X 02 Jun 2024 12:11:31.135 # +switch-master myredis 127.0.0.1 6379 127.0.0.1 6381
7471:X 02 Jun 2024 12:11:31.135 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6381
7471:X 02 Jun 2024 12:11:31.135 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6381
7471:X 02 Jun 2024 12:12:01.199 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6381

查看81机器信息,已经从 从机晋升为主机

127.0.0.1:6381> info replication
# Replication
role:master # 主机标志
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=37023,lag=0
master_failover_state:no-failover
master_replid:ac31dc40d23d434956430522c87c3eaf159b61c7
master_replid2:4176a6750e36e6faabd588b5bb67b2e5e931a303
master_repl_offset:37155
second_repl_offset:26641
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:155
repl_backlog_histlen:37001

查看80机器,主机已经切换到81

127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381  # 主机信息
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:34459
slave_repl_offset:34459
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:ac31dc40d23d434956430522c87c3eaf159b61c7
master_replid2:4176a6750e36e6faabd588b5bb67b2e5e931a303
master_repl_offset:34459
second_repl_offset:26641
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:34459

哨兵文件详细配置

详细的哨兵文件配置,可以参考其他博主的
漫步旅行_James

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值