Redis 哨兵搭建

Redis哨兵(sentinel)搭建 7.2.5

文章目录

  • 一、单节点哨兵
    • 1. 环境介绍
    • 2. 环境前准备工作
    • 3. 安装 Redis 7.2.5
    • 4. redis 配置修改并且启动
      • 4.1 修改配置文件
      • 4.2 编写启动脚本
    • 5. 开启主从
      • 5.1 开启
      • 5.2 主库实例查看主从信息
    • 6. 创建sentinel的配置文件并启动
      • 6.1 创建配置文件
      • 6.2 启动
      • 6.3 手动停止主库运行,模拟主库宕机
      • 6.4 手动修复宕机的主库,sentinel会自动发现
  • 二、多节点哨兵
    • 1.Redis配置文件修改并启动
      • 1.1 redis_master 节点
      • 1.2 redis_slave1 节点
      • 1.3 redis_slave2 节点
    • 2. 开启主从
      • 2.1 redis_slave1 节点
      • 2.2 redis_slave2 节点
      • 2.3 redis_master 节点
    • 3. 编写sentinel配置文件并启动
      • 3.1 redis_master 节点
      • 3.2 redis_slave1 节点
      • 3.3 redis_slave2 节点
    • 4. 验证sentinel能够自动切换
      • 4.1 手动停止主库运行,模拟主库宕机
      • 4.2 当16370 实例宕机后,查看集群情况
      • 4.3 恢复16370 实例会成为slaves节点
    • 5. 手动停掉一个sentinel实例
      • 5.1 将redis_master主机中的sentinel实例给停止掉
      • 5.2 验证主从切换
      • 5.3 恢复Redis 实例
    • 6. 手动停掉两个sentinel 实例
      • 6.1 将redis_slave1 主机中的sentinel 实例停止
      • 6.2 验证主从切换

一、单节点哨兵

1. 环境介绍

操作系统Centos 7
内核版本Linux 3.10.0-957.el7.x86_64
主机名称master
IP192.168.1.100
端口master:16370 slaver1:16371 slaver2: 16372 sentinel:16379

2. 环境前准备工作

# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld# 修改 hostname
hostnamectl set-hostname xxxx  # 修改后退出当前终端重新连接即可# 更新下软件源
# 地址 https://developer.aliyun.com/mirror/# 时区调整,时间校准
date -R
timedatectl set-timezone Asia/Shanghai
yum -y install ntp
ntpdate ntp1.aliyun.com# 关闭 selinux: 
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0# 修改内存分配控制
vi /etc/sysctl.confvm.overcommit_memory = 1
# 设置TPC积压值
net.core.somaxconn= 1024# 使其配置文件生效
sysctl -p /etc/sysctl.conf

3. 安装 Redis 7.2.5

# 下载地址
http://download.redis.io/releases/# 下载
wget http://download.redis.io/releases/redis-7.2.5.tar.gz
# 解压
tar -xf redis-7.2.5.tar.gz -C /opt/redis# 编译
make MALLOC=libc# 安装 注意如果安装提示python3不存在 执行 yum install -y python3
make install PREFIX=/usr/local/redis# 配置环境变量
vi /etc/profile.d/redis.sh#!/bin/bashexport REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin # 使其配置生效
source /etc/profile.d/redis.sh

4. redis 配置修改并且启动

4.1 修改配置文件

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/1637{0..2}
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/1637{0..2}
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/1637{0..2}# 创建redis pid存储目录
mkdir /usr/local/redis/run# redis配置文件 16370
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16370
# pid存储目录
pidfile /usr/local/redis/run/redis_16370.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16370.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16370
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes  # redis配置文件 16371
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16371
# pid存储目录
pidfile /usr/local/redis/run/redis_16371.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16371.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16371
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes  # redis配置文件 16372
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16372
# pid存储目录
pidfile /usr/local/redis/run/redis_16372.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16372.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16372
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes  # 复制下面命令即可
cat > /usr/local/redis/conf/redis_16370.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16370
# pid存储目录
pidfile /usr/local/redis/run/redis_16370.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16370.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16370
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOFcat > /usr/local/redis/conf/redis_16371.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16371
# pid存储目录
pidfile /usr/local/redis/run/redis_16371.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16371.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16371
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOFcat > /usr/local/redis/conf/redis_16372.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16372
# pid存储目录
pidfile /usr/local/redis/run/redis_16372.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16372.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16372
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

4.2 编写启动脚本

# 启动脚本
cat > /usr/local/redis/bin/master-slave_start.sh << EOF
#!/bin/bashredis-server /usr/local/redis/conf/redis_16370.conf
redis-server /usr/local/redis/conf/redis_16371.conf
redis-server /usr/local/redis/conf/redis_16372.conf
EOF# 赋予可执行权限
chmod +x /usr/local/redis/bin/master-slave_start.sh# 关闭脚本
vi /usr/local/redis/bin/master-slave_shutdown.sh#!/bin/bashkill -9 $(ps -ef | grep 1637 | grep -v grep | awk '{print $2}')# 赋予可执行权限
chmod +x /usr/local/redis/bin/master-slave_shutdown.sh

5. 开启主从

5.1 开启

# 以"192.168.1.100:16370"实例为主库,以"192.168.1.100:16371"实例和"192.168.1.100:16372"实例为从库。
# -a 参数指定密码
# 从库实例开启主从redis-cli -h 192.168.1.100 -p 16371 -a dongdong slaveof 192.168.1.100 16370redis-cli -h 192.168.1.100 -p 16372 -a dongdong slaveof 192.168.1.100 16370# Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# 警告:在命令行界面上使用带有“-a”或“-u”选项的密码可能不安全。  可以忽略# 解决方式# redis-cli -h 192.168.1.100 -p 16371 -a dongdong --no-auth-warning  slaveof 192.168.1.100 16370 

5.2 主库实例查看主从信息

redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.100,port=16371,state=online,offset=8428,lag=0
slave1:ip=192.168.1.100,port=16372,state=online,offset=8428,lag=1
master_failover_state:no-failover
master_replid:a292603e2c96a6f451055e6d84a60795f81f2928
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:8428
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:8428

6. 创建sentinel的配置文件并启动

6.1 创建配置文件

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16379
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16379
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16379# 创建redis pid存储目录
mkdir /usr/local/redis/runcat > /usr/local/redis/conf/sentinel.conf <<EOF
bind 192.168.1.100
port 16379
dir /usr/local/redis/data/16379
sentinel monitor redis_master 192.168.1.100 16370 1
sentinel down-after-milliseconds redis_master 5000
sentinel auth-pass redis_master dongdong
EOF配置文件参数说明如下:sentinel monitor redis_master 192.168.1.100 16370 1:指定Redis初始集群的master库节点并命名为"redis_master",注意这个"1"的含义,他表示sentinel需要最少的投票数。这在于多sentinel的场景下很有用,由于本案例只是用了1个sentinel,因此我就设置为1。在生产环境中,建议配置多sentinel的模式,建议最少设置为3(建议设置奇数)个sentinel物理节点,而后将此处的"1"改为"2",表示3个sentinel中最少有2个节点认为master库宕机时,才会真正意义上认为master库宕机。       sentinel down-after-milliseconds redis_master 5000:监控sentinel集群时,如果超过了5000毫秒(即5秒)仍然没有响应,则sentinel会判定master库宕机了。sentinel auth-pass redis_master dongdong:由于sentinel需要访问Redis集群,因此我们要设置访问整个集群的密码,我这里指定的密码为"dongdong",这意味着所有的节点都使用相同的密码。

6.2 启动

# 启动 sentinel进程
redis-sentinel /usr/local/redis/conf/sentinel.conf &> /usr/local/redis/log/16379/sentinel_16379.log &root@master ~]# redis-sentin el /usr/local/redis/conf/sentinel.conf &> /usr/local/redis/log/16379/sentinel_16379.log &
[1] 29961# 查看日志
tail -100f /usr/local/redis/log/16379/sentinel_16379.log
[root@master ~]# tail -100f /usr/local/redis/log/16379/sentinel_16379.log
30008:X 17 Jul 2024 19:51:15.356 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30008:X 17 Jul 2024 19:51:15.356 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=30008, just started
30008:X 17 Jul 2024 19:51:15.356 * Configuration loaded
30008:X 17 Jul 2024 19:51:15.356 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30008:X 17 Jul 2024 19:51:15.356 * monotonic clock: POSIX clock_gettime
30008:X 17 Jul 2024 19:51:15.357 * Running mode=sentinel, port=16379.
30008:X 17 Jul 2024 19:51:15.357 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30008:X 17 Jul 2024 19:51:15.357 * Sentinel ID is a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b
30008:X 17 Jul 2024 19:51:15.357 # +monitor master redis_master 192.168.1.100 16370 quorum 1# 通过redis-cli 进入 查看
redis-cli -h 192.168.1.100 -p 16379[root@master ~]# redis-cli -h 192.168.1.100 -p 16379
192.168.1.100:16379> ping
PONG
192.168.1.100:16379> 

6.3 手动停止主库运行,模拟主库宕机

# 切换前主从状态正常
[root@master ~]# ss -npl | grep redis
tcp    LISTEN     0      128    192.168.1.100:16370              *:*                   users:(("redis-server",pid=29871,fd=6))
tcp    LISTEN     0      128    192.168.1.100:16371              *:*                   users:(("redis-server",pid=29873,fd=6))
tcp    LISTEN     0      128    192.168.1.100:16372              *:*                   users:(("redis-server",pid=29881,fd=6))
tcp    LISTEN     0      128    192.168.1.100:16379              *:*                   users:(("redis-sentinel",pid=30008,fd=6))# 查看集群状态 master
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication [root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.100,port=16371,state=online,offset=111085,lag=0
slave1:ip=192.168.1.100,port=16372,state=online,offset=111085,lag=1
master_failover_state:no-failover
master_replid:208227bbd586d90841dcf753ba669dbde246bba7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:111085
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:111085# 查看集群状态 slaver
redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replication [root@master ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_read_repl_offset:118087
slave_repl_offset:118087
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:208227bbd586d90841dcf753ba669dbde246bba7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:118087
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:118073# 查看集群状态 slaver
redis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication [root@master ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:126869
slave_repl_offset:126869
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:208227bbd586d90841dcf753ba669dbde246bba7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126869
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:126855# 手动将主库宕机,观察从库被sentinel实现了自动切换:
redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# 再查看集群状态 master
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication  # 由于将主库服务停了,因此无法再次连接主库
[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.100:16370: Connection refused# 再查看集群状态 slaves  发现变成了master节点
redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replication [root@master ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.100,port=16372,state=online,offset=150234,lag=0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:150234
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:150220# 查看集群状态 slaver
redis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication [root@master ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16371
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_read_repl_offset:154406
slave_repl_offset:154406
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:154406
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:154392# 查看sentinel的日志信息,不难发现有记录切换的过程
tail -100f /usr/local/redis/log/16379/sentinel_16379.log[root@master ~]# tail -100f /usr/local/redis/log/16379/sentinel_16379.log
30008:X 17 Jul 2024 19:51:15.356 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30008:X 17 Jul 2024 19:51:15.356 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=30008, just started
30008:X 17 Jul 2024 19:51:15.356 * Configuration loaded
30008:X 17 Jul 2024 19:51:15.356 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30008:X 17 Jul 2024 19:51:15.356 * monotonic clock: POSIX clock_gettime
30008:X 17 Jul 2024 19:51:15.357 * Running mode=sentinel, port=16379.
30008:X 17 Jul 2024 19:51:15.357 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30008:X 17 Jul 2024 19:51:15.357 * Sentinel ID is a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b
30008:X 17 Jul 2024 19:51:15.357 # +monitor master redis_master 192.168.1.100 16370 quorum 1
30008:X 17 Jul 2024 20:15:40.962 # +sdown master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.962 # +odown master redis_master 192.168.1.100 16370 #quorum 1/1
30008:X 17 Jul 2024 20:15:40.962 # +new-epoch 1
30008:X 17 Jul 2024 20:15:40.962 # +try-failover master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.968 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:40.968 # +vote-for-leader a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b 1
30008:X 17 Jul 2024 20:15:40.968 # +elected-leader master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.968 # +failover-state-select-slave master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.026 # +selected-slave slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.027 * +failover-state-send-slaveof-noone slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.093 * +failover-state-wait-promotion slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.015 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:42.015 # +promoted-slave slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.015 # +failover-state-reconf-slaves master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.096 * +slave-reconf-sent slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.040 * +slave-reconf-inprog slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.040 * +slave-reconf-done slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.094 # +failover-end master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.094 # +switch-master redis_master 192.168.1.100 16370 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.095 * +slave slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.095 * +slave slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.100 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:48.121 # +sdown slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371

6.4 手动修复宕机的主库,sentinel会自动发现

# 修复之前,16370端口是连接不上的
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.100:16370: Connection refused[root@master ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.100,port=16372,state=online,offset=190264,lag=1
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:190410
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:190396[root@master ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:192204
slave_repl_offset:192204
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:192204
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:192190# 修复之后,之前宕机的主库会自动加入集群并成为slaves节点
redis-server /usr/local/redis/conf/redis_16370.conf
[root@master ~]# redis-server /usr/local/redis/conf/redis_16370.conf
[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:199215
slave_repl_offset:199215
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:199215
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:199047
repl_backlog_histlen:169# 查看 
redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replicationredis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication [root@master ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2 # slaves 变成两个了
slave0:ip=192.168.1.100,port=16372,state=online,offset=203533,lag=1
slave1:ip=192.168.1.100,port=16370,state=online,offset=203533,lag=1
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:203533
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:203519[root@master ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:207851
slave_repl_offset:207851
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:23729dc7d7aeafa6f85a2a647442965c80b379ed
master_replid2:208227bbd586d90841dcf753ba669dbde246bba7
master_repl_offset:207851
second_repl_offset:134150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:207837# 注意观察sentinel日志哟,会有新日志记录
tail -100f /usr/local/redis/log/16379/sentinel_16379.log
[root@master ~]# tail -100f /usr/local/redis/log/16379/sentinel_16379.log
30008:X 17 Jul 2024 19:51:15.356 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30008:X 17 Jul 2024 19:51:15.356 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=30008, just started
30008:X 17 Jul 2024 19:51:15.356 * Configuration loaded
30008:X 17 Jul 2024 19:51:15.356 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30008:X 17 Jul 2024 19:51:15.356 * monotonic clock: POSIX clock_gettime
30008:X 17 Jul 2024 19:51:15.357 * Running mode=sentinel, port=16379.
30008:X 17 Jul 2024 19:51:15.357 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30008:X 17 Jul 2024 19:51:15.357 * Sentinel ID is a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b
30008:X 17 Jul 2024 19:51:15.357 # +monitor master redis_master 192.168.1.100 16370 quorum 1
30008:X 17 Jul 2024 20:15:40.962 # +sdown master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.962 # +odown master redis_master 192.168.1.100 16370 #quorum 1/1
30008:X 17 Jul 2024 20:15:40.962 # +new-epoch 1
30008:X 17 Jul 2024 20:15:40.962 # +try-failover master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.968 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:40.968 # +vote-for-leader a91bf648c16246f3f7c7e1aa2b1fc6fad38f772b 1
30008:X 17 Jul 2024 20:15:40.968 # +elected-leader master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:40.968 # +failover-state-select-slave master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.026 # +selected-slave slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.027 * +failover-state-send-slaveof-noone slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:41.093 * +failover-state-wait-promotion slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.015 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:42.015 # +promoted-slave slave 192.168.1.100:16371 192.168.1.100 16371 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.015 # +failover-state-reconf-slaves master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:42.096 * +slave-reconf-sent slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.040 * +slave-reconf-inprog slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.040 * +slave-reconf-done slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.094 # +failover-end master redis_master 192.168.1.100 16370
30008:X 17 Jul 2024 20:15:43.094 # +switch-master redis_master 192.168.1.100 16370 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.095 * +slave slave 192.168.1.100:16372 192.168.1.100 16372 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.095 * +slave slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:15:43.100 * Sentinel new configuration saved on disk
30008:X 17 Jul 2024 20:15:48.121 # +sdown slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:30:15.529 # -sdown slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371
30008:X 17 Jul 2024 20:30:25.504 * +convert-to-slave slave 192.168.1.100:16370 192.168.1.100 16370 @ redis_master 192.168.1.100 16371# 除了提到上面的变化外,还有一点变化就是配置文件也修改了,每个"slave"节点都新增了 下面三行 本案例只有192.168.1.100:16370 和 192.168.1.100:16372是 slave
# Generated by CONFIG REWRITE
replicaof 192.168.1.100 16371
latency-tracking-info-percentiles 50 99 99.9
user default on sanitize-payload #162fffe9f429e1fa5d351c11f5a7f2f3031806c5125eeb84993f68738cbb6c84 ~* &* +@all# Generated by CONFIG REWRITE
replicaof 192.168.1.100 16371
latency-tracking-info-percentiles 50 99 99.9
user default on sanitize-payload #162fffe9f429e1fa5d351c11f5a7f2f3031806c5125eeb84993f68738cbb6c84 ~* &* +@all

二、多节点哨兵

一主两从三哨兵

一共三台机器,每台机器部署一个哨兵

注意: 后面启动哨兵可能会出现两个警告

WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.

警告:必须启用内存超量使用!没有它,在内存不足的情况下,后台保存或复制可能会失败。要解决此问题,请将“vm.overcommit_memory=1”添加到/etc/sysctl.conf中,然后重新启动或运行命令“sysctl vm.overcommit_memory=1”以使其生效。

修改内存分配控制

vi /etc/sysctl.conf

vm.overcommit_memory = 1

使其配置文件生效

sysctl -p /etc/sysctl.conf

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

警告:无法强制执行TCP积压设置511,因为/proc/sys/net/core/somaxconn设置为较低的值128。

vi /etc/sysctl.conf

net.core.somaxconn= 1024

sysctl -p /etc/sysctl.conf

操作系统Centos 7Centos 7Centos 7
内核版本Linux 3.10.0-957.el7.x86_64Linux 3.10.0-957.el7.x86_64Linux 3.10.0-957.el7.x86_64
主机名称redis_masterredis_salve1redis_salve2
IP192.168.1.100192.168.1.200192.168.1.250
端口16370、2637016371、2637016372、26370

安装前准备工作已经Redis安装,请参照上面步骤进行

1.Redis配置文件修改并启动

1.1 redis_master 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16370
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16370
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16370# 创建redis pid存储目录
mkdir /usr/local/redis/run/16370# 修改配置文件
# 复制下面命令即可
cat > /usr/local/redis/conf/16370/redis_16370.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16370
# pid存储目录
pidfile /usr/local/redis/run/16370/redis_16370.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16370.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16370
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF# 启动Redis
redis-server /usr/local/redis/conf/16370/redis_16370.conf

1.2 redis_slave1 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16371
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16371
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16371# 创建redis pid存储目录
mkdir /usr/local/redis/run/16371# 修改配置文件
# 复制下面命令即可
cat > /usr/local/redis/conf/16371/redis_16371.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.200   
# 端口  
port 16371
# pid存储目录
pidfile /usr/local/redis/run/16371/redis_16371.pid   
# 日志存储目录
logfile /usr/local/redis/log/16371/redis_16371.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16371
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF# 启动Redis
redis-server /usr/local/redis/conf/16371/redis_16371.conf

1.3 redis_slave2 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16372
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16372
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16372# 创建redis pid存储目录
mkdir /usr/local/redis/run/16372# 修改配置文件
# 复制下面命令即可
cat > /usr/local/redis/conf/16372/redis_16372.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.250   
# 端口  
port 16372
# pid存储目录
pidfile /usr/local/redis/run/16372/redis_16372.pid   
# 日志存储目录
logfile /usr/local/redis/log/16372/redis_16372.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16372
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的“requirepass”保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF# 启动Redis
redis-server /usr/local/redis/conf/16372/redis_16372.conf

2. 开启主从

2.1 redis_slave1 节点

# 从库实例开启主从
redis-cli -h 192.168.1.200 -p 16371 -a dongdong slaveof 192.168.1.100 16370[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong slaveof 192.168.1.100 16370
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK# 从库实例查看主从信息
redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication [root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_read_repl_offset:210
slave_repl_offset:210
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:210
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:196

2.2 redis_slave2 节点

# 从库实例开启主从
redis-cli -h 192.168.1.250 -p 16372 -a dongdong slaveof 192.168.1.100 16370[root@redis_slave2 ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong slaveof 192.168.1.100 16370
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK# 从库实例查看主从信息
redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication [root@redis_slave2 ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_read_repl_offset:266
slave_repl_offset:266
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:266
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:224

2.3 redis_master 节点

# 主库实例查看主从信息
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication [root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.200,port=16371,state=online,offset=42,lag=0
slave1:ip=192.168.1.250,port=16372,state=online,offset=42,lag=1
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56

3. 编写sentinel配置文件并启动

3.1 redis_master 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/26370
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/26370
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/26370# 创建redis pid存储目录
mkdir /usr/local/redis/run/26370cat > /usr/local/redis/conf/26370/sentinel.conf <<EOF
bind 192.168.1.100
port 26370
dir /usr/local/redis/data/26370
sentinel monitor redis_master 192.168.1.100 16370 2
sentinel down-after-milliseconds redis_master 5000
sentinel auth-pass redis_master dongdong
EOF# 启动 sentinel进程
redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &[root@redis_master ~]# redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &
[1] 30569# 查看redis-sentinel 进程
[root@redis_master ~]# ps -ef | grep redis
root      30552      1  0 10:17 ?        00:00:01 redis-server 192.168.1.100:16370
root      30569  30499  0 10:33 pts/1    00:00:00 redis-sentinel 192.168.1.100:26370 [sentinel]  # 已经成功启动了
root      30574  30499  0 10:34 pts/1    00:00:00 grep --color=auto redis# 查看日志信息
tail -100f /usr/local/redis/log/26370/sentinel_26370.log[root@redis_master ~]# tail -100f /usr/local/redis/log/26370/sentinel_26370.log
30569:X 18 Jul 2024 10:33:50.636 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30569:X 18 Jul 2024 10:33:50.636 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=30569, just started
30569:X 18 Jul 2024 10:33:50.636 * Configuration loaded
30569:X 18 Jul 2024 10:33:50.636 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30569:X 18 Jul 2024 10:33:50.636 * monotonic clock: POSIX clock_gettime
30569:X 18 Jul 2024 10:33:50.637 * Running mode=sentinel, port=26370.
30569:X 18 Jul 2024 10:33:50.637 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30569:X 18 Jul 2024 10:33:50.638 * Sentinel new configuration saved on disk
30569:X 18 Jul 2024 10:33:50.639 * Sentinel ID is 088b671eb4ac4c8d8a57805c1be88e4f76cf7ff8
30569:X 18 Jul 2024 10:33:50.639 # +monitor master redis_master 192.168.1.100 26370 quorum 2
30569:X 18 Jul 2024 10:33:52.638 * +sentinel sentinel 088b671eb4ac4c8d8a57805c1be88e4f76cf7ff8 192.168.1.100 26370 @ redis_master 192.168.1.100 26370
30569:X 18 Jul 2024 10:33:52.644 * Sentinel new configuration saved on disk

3.2 redis_slave1 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/26370
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/26370
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/26370# 创建redis pid存储目录
mkdir /usr/local/redis/run/26370cat > /usr/local/redis/conf/26370/sentinel.conf <<EOF
bind 192.168.1.200
port 26370
dir /usr/local/redis/data/26370
sentinel monitor redis_master 192.168.1.100 16370 2
sentinel down-after-milliseconds redis_master 5000
sentinel auth-pass redis_master dongdong
EOF# 启动 sentinel进程
redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &[root@redis_slave1 ~]# redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &
[1] 38894# 查看redis-sentinel 进程
[root@redis_slave1 ~]# ps -ef | grep redis
root      38879      1  0 10:19 ?        00:00:01 redis-server 192.168.1.200:16371
root      38894  38840  0 10:36 pts/0    00:00:00 redis-sentinel 192.168.1.200:26370 [sentinel]
root      38899  38840  0 10:37 pts/0    00:00:00 grep --color=auto redis# 查看日志信息
tail -100f /usr/local/redis/log/26370/sentinel_26370.log[root@redis_slave1 ~]# tail -100f /usr/local/redis/log/26370/sentinel_26370.log
38894:X 18 Jul 2024 10:36:54.115 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
38894:X 18 Jul 2024 10:36:54.116 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
38894:X 18 Jul 2024 10:36:54.116 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=38894, just started
38894:X 18 Jul 2024 10:36:54.116 * Configuration loaded
38894:X 18 Jul 2024 10:36:54.116 * Increased maximum number of open files to 10032 (it was originally set to 1024).
38894:X 18 Jul 2024 10:36:54.116 * monotonic clock: POSIX clock_gettime
38894:X 18 Jul 2024 10:36:54.116 * Running mode=sentinel, port=26370.
38894:X 18 Jul 2024 10:36:54.116 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
38894:X 18 Jul 2024 10:36:54.119 * Sentinel new configuration saved on disk
38894:X 18 Jul 2024 10:36:54.119 * Sentinel ID is 93a83bf4770f1f8eda48b96ead065382ab2b5fe6
38894:X 18 Jul 2024 10:36:54.119 # +monitor master redis_slave1 192.168.1.200 26370 quorum 2
38894:X 18 Jul 2024 10:36:56.164 * +sentinel sentinel 93a83bf4770f1f8eda48b96ead065382ab2b5fe6 192.168.1.200 26370 @ redis_slave1 192.168.1.200 26370
38894:X 18 Jul 2024 10:36:56.170 * Sentinel new configuration saved on disk

3.3 redis_slave2 节点

# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/26370
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/26370
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/26370# 创建redis pid存储目录
mkdir /usr/local/redis/run/26370cat > /usr/local/redis/conf/26370/sentinel.conf <<EOF
bind 192.168.1.250
port 26370
dir /usr/local/redis/data/26370
sentinel monitor redis_master 192.168.1.100 16370 2
sentinel down-after-milliseconds redis_master 5000
sentinel auth-pass redis_master dongdong
EOF# 启动 sentinel进程
redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &[root@redis_slave2 ~]# redis-sentinel /usr/local/redis/conf/26370/sentinel.conf &> /usr/local/redis/log/26370/sentinel_26370.log &
[1] 39310# 查看redis-sentinel 进程
[root@redis_slave2 ~]# ps -ef | grep redis
root      39290      1  0 10:20 ?        00:00:03 redis-server 192.168.1.250:16372
root      39310  39251  0 10:50 pts/0    00:00:00 redis-sentinel 192.168.1.250:26370 [sentinel]
root      39316  39251  0 10:51 pts/0    00:00:00 grep --color=auto redis# 查看日志信息tail -100f /usr/local/redis/log/26370/sentinel_26370.log[root@redis_slave2 ~]# tail -100f /usr/local/redis/log/26370/sentinel_26370.log
39310:X 18 Jul 2024 10:50:57.403 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
39310:X 18 Jul 2024 10:50:57.403 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
39310:X 18 Jul 2024 10:50:57.403 * Redis version=7.2.5, bits=64, commit=00000000, modified=0, pid=39310, just started
39310:X 18 Jul 2024 10:50:57.403 * Configuration loaded
39310:X 18 Jul 2024 10:50:57.404 * Increased maximum number of open files to 10032 (it was originally set to 1024).
39310:X 18 Jul 2024 10:50:57.404 * monotonic clock: POSIX clock_gettime
39310:X 18 Jul 2024 10:50:57.404 * Running mode=sentinel, port=26370.
39310:X 18 Jul 2024 10:50:57.405 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
39310:X 18 Jul 2024 10:50:57.407 * Sentinel new configuration saved on disk
39310:X 18 Jul 2024 10:50:57.407 * Sentinel ID is 9227635399651de294a88fa16281232b9cbbccaf
39310:X 18 Jul 2024 10:50:57.407 # +monitor master redis_slave2 192.168.1.250 26370 quorum 2
39310:X 18 Jul 2024 10:50:59.427 * +sentinel sentinel 9227635399651de294a88fa16281232b9cbbccaf 192.168.1.250 26370 @ redis_slave2 192.168.1.250 26370
39310:X 18 Jul 2024 10:50:59.434 * Sentinel new configuration saved on disk

4. 验证sentinel能够自动切换

4.1 手动停止主库运行,模拟主库宕机

# 查看主库的状态
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replicationWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2  
slave0:ip=192.168.1.200,port=16371,state=online,offset=2898,lag=0
slave1:ip=192.168.1.250,port=16372,state=online,offset=2898,lag=0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2898
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2898# 查看从库状态
redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_read_repl_offset:3066
slave_repl_offset:3066
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3066
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:3052redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication[root@redis_slave2 ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.100
master_port:16370
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:3094
slave_repl_offset:3094
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:466362240f16a7414b9236f931c6f97252ee00be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3094
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:3052# 各节点运行redis进程情况
root@redis_master ~]# ss -npl | grep redis
tcp    LISTEN     0      128    192.168.1.100:16370              *:*                   users:(("redis-server",pid=30552,fd=6))
tcp    LISTEN     0      128    192.168.1.100:26370              *:*                   users:(("redis-sentinel",pid=30569,fd=6))[root@redis_slave1 ~]# ss -npl | grep redis
tcp    LISTEN     0      128    192.168.1.200:16371              *:*                   users:(("redis-server",pid=38879,fd=6))
tcp    LISTEN     0      511    192.168.1.200:26370              *:*                   users:(("redis-sentinel",pid=38908,fd=6))[root@redis_slave2 ~]# ss -npl | grep redis
tcp    LISTEN     0      128    192.168.1.250:16372              *:*                   users:(("redis-server",pid=39290,fd=6))
tcp    LISTEN     0      128    192.168.1.250:26370              *:*                   users:(("redis-sentinel",pid=39310,fd=6))# 一切正常
# 手动模拟主库宕机 再来查看情况
redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# 再次执行命令
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.100:16370: Connection refused  # 发现连接不上 因为我们把主库宕了

4.2 当16370 实例宕机后,查看集群情况

# 查看集群状态 
redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication  [root@redis_master ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master # 此时的slave1主机变成了master 节点
connected_slaves:1   # slave 也变成了1个
slave0:ip=192.168.1.250,port=16372,state=online,offset=14837,lag=1
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:b23a9a971ad73ed23fb81186529c671e54acbac4
master_repl_offset:14837
second_repl_offset:14231
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14837# 查看集群状态
redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication  [root@redis_slave2 ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.200 
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:87953
slave_repl_offset:87953
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:b23a9a971ad73ed23fb81186529c671e54acbac4
master_repl_offset:87953
second_repl_offset:14231
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:87953# 现在集群里面的情况就是 一主一从

4.3 恢复16370 实例会成为slaves节点

# 修复之前,16370端口是连接不上的
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication[root@master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.100:16370: Connection refused# 修复之后,之前宕机的主库会自动加入集群并成为slaves节点
# 启动Redis
redis-server /usr/local/redis/conf/16370/redis_16370.conf# 可能需要过了几秒才会显示哦 因为信息还没有同步过来哦
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.200
master_port:16371
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:165354
slave_repl_offset:165354
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:165354
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:164602
repl_backlog_histlen:753

5. 手动停掉一个sentinel实例

5.1 将redis_master主机中的sentinel实例给停止掉

# 查看redis进程
[root@redis_master ~]# ps -ef | grep redis
root      30646  30499  0 11:23 pts/1    00:00:06 redis-sentinel 192.168.1.100:26370 [sentinel]
root      30663      1  0 11:42 ?        00:00:01 redis-server 192.168.1.100:16370
root      30672  30499  0 11:50 pts/1    00:00:00 grep --color=auto redis# 杀掉 进程id为30646服务
kill -9 30646# 再次查看redis进程 sentinel 已经被杀掉了
[root@redis_master ~]# ps -ef | grep redis
root      30663      1  0 11:42 ?        00:00:01 redis-server 192.168.1.100:16370
root      30676  30499  0 11:51 pts/1    00:00:00 grep --color=auto redis

5.2 验证主从切换

# 查看各节点状况 此时slave1这台是主库
# 查询从库状态 redis_master主机
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.200
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:251306
slave_repl_offset:251306
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:251306
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:164602
repl_backlog_histlen:86705# 查询从库状态 redis_slave2主机
redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication 
[root@redis_master ~]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.200
master_port:16371
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:254136
slave_repl_offset:254136
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:b23a9a971ad73ed23fb81186529c671e54acbac4
master_repl_offset:254136
second_repl_offset:14231
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:254136# 查询主库状态
redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication 
[root@redis_master ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.250,port=16372,state=online,offset=261722,lag=0
slave1:ip=192.168.1.100,port=16370,state=online,offset=261736,lag=0
master_failover_state:no-failover
master_replid:c6e88908dbe348999f5406625612e76ee4d6ffd1
master_replid2:b23a9a971ad73ed23fb81186529c671e54acbac4
master_repl_offset:261882
second_repl_offset:14231
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:261882# 此时干掉主库
redis-cli -h 192.168.1.200 -p 16371 -a dongdong shutdown
[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# 再次查询
[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.1.200:16371: Connection refused# 此时查询集群个节点状态 看看哪个节点成功master 此时集群中只有两个节点 分别是redis_master主机 和 redis_slave2主机
# 先查看redis_master
redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.250
master_port:16372
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:56377
slave_repl_offset:56377
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:3288009c32135c1d1df6e8d56ceff7d45e572875
master_repl_offset:56377
second_repl_offset:55465
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:30454
repl_backlog_histlen:25924# 是正常的

5.3 恢复Redis 实例

# 查看各节点 redis 服务是否停用 如果停用 则重新启动
ps -ef | grep redis# redis_master
redis-server /usr/local/redis/conf/16370/redis_16370.conf# redis_slave1
redis-server /usr/local/redis/conf/16371/redis_16371.conf# redis_slave2
redis-server /usr/local/redis/conf/16372/redis_16372.conf

6. 手动停掉两个sentinel 实例

6.1 将redis_slave1 主机中的sentinel 实例停止

[root@redis_slave1 ~]# ps -ef | grep redis
root      39196      1  0 13:43 ?        00:00:02 redis-server 192.168.1.200:16371
root      39207  38840  0 13:45 pts/0    00:00:03 redis-sentinel 192.168.1.200:26370 [sentinel]
root      39220  38840  0 13:57 pts/0    00:00:00 grep --color=auto rediskill -9 39207root@redis_slave1 ~]# ps -ef | grep redis
root      39196      1  0 13:43 ?        00:00:02 redis-server 192.168.1.200:16371
root      39224  38840  0 13:59 pts/0    00:00:00 grep --color=auto redis

6.2 验证主从切换

# 查询redis_master主机集群状态
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.200
master_port:16371
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:140766
slave_repl_offset:140766
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:3288009c32135c1d1df6e8d56ceff7d45e572875
master_repl_offset:140766
second_repl_offset:55465
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:30454
repl_backlog_histlen:110313# 查询redis_slvae1主机集群状态
[root@redis_slave1 ~]# redis-cli -h 192.168.1.200 -p 16371 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.100,port=16370,state=online,offset=143290,lag=0
slave1:ip=192.168.1.250,port=16372,state=online,offset=143290,lag=1
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:3288009c32135c1d1df6e8d56ceff7d45e572875
master_repl_offset:143436
second_repl_offset:55465
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:143422# 查询redis_slave2主机集群状态
[root@redis_slave2 conf]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.200
master_port:16371
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:eb44c8dbaaf01ca3b091a4005ecb563cc467dc76
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# 停掉redis_slvae1主机的redis 服务 看看是否可以进行主从切换
[root@redis_slave1 ~]# ps -ef | grep redis
root      39196      1  0 13:43 ?        00:00:03 redis-server 192.168.1.200:16371
root      39242  38840  0 14:03 pts/0    00:00:00 grep --color=auto rediskill -9 39196# 再次查看redis_master主机集群状态
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.200
master_port:16371
master_link_status:down  # master 已经是 down 状态 说明没有进行主从切换
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:154925
slave_repl_offset:154925
master_link_down_since_seconds:49
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:3288009c32135c1d1df6e8d56ceff7d45e572875
master_repl_offset:154925
second_repl_offset:55465
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:30454
repl_backlog_histlen:124472# 再次查询redis_slvae2主机集群状态
[root@redis_slave2 conf]# redis-cli -h 192.168.1.250 -p 16372 -a dongdong  info replication  
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.200
master_port:16371
master_link_status:down # master 已经是 down 状态 说明没有进行主从切换
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:154925
slave_repl_offset:154925
master_link_down_since_seconds:133
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a375e44a67edd4fb81880032ef927964da6c376f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154925
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:149549
repl_backlog_histlen:5377# 是因为我们再配置文件中指定了 sentinel monitor redis_master 192.168.1.100 16370 2  这个2 表示3个sentinel中最少有2个节点认为master库宕机时,才会真正意义上认为master库宕机。    # 把redis_slvae1主机的redis 服务恢复
redis-server /usr/local/redis/conf/16371/redis_16371.conf# 查看状态
[root@redis_master ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong  info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.200
master_port:16371
master_link_status:up  # 恢复redis_slvae1主机的redis后 master服务up了
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:3474
slave_repl_offset:3474
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:527f1bfc1c67513d0813d0c0fddb07964640d907
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3474
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:476
repl_backlog_histlen:2999

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/50919.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Qt-线程-创建线程的三种方法

文章目录 1: 派生于QThread2: 派生与QRunable3: moveToThread 1: 派生于QThread 派生于Qthread 是创建的Qt创建线程的方法 &#xff0c;重写虚函数void QThread::run()&#xff0c;在run写具体的内容&#xff0c;外部通过start调用&#xff0c;即可执行线程体run() 注意 :派生…

算法-插入排序

插入排序步骤 前面文章分享了两种排序算法&#xff1a;冒泡排序和选择排序。虽然它们的效率都是O(N2)&#xff0c;但其实选择排序比冒泡排序快一倍。现在来学第三种排序算法——插入排序。你会发现&#xff0c;顾及最坏情况以外的场景将是多么有用。 插入排序包括以下步骤。 …

2024固定资产管理软件排名 6款好用的企业资产管理软件

固定资产管理是企业财务管理的重要组成部分&#xff0c;选择一款好用的固定资产管理软件可以显著提升资产跟踪和维护的效率。本文将介绍几款功能强大且易于操作的固定资产管理软件&#xff0c;帮助企业优化资产管理流程&#xff0c;确保资产数据的准确性和实时性&#xff0c;从…

软件测试:Postman 工具的使用。开发及测试均需要掌握的测试工具

工具介绍 各个模块功能的介绍如下&#xff1a; 1、New&#xff1a;在这里创建新的请求、集合或环境&#xff1b;还可以创建更高级的文档、Mock Server 和 Monitor以及API。 2、Import&#xff1a;这用于导入集合或环境。有一些选项&#xff0c;例如从文件&#xff0c;文件夹导…

SpringBoot3:轻松使用Jasypt实现配置文件信息加密

文章目录 前言一、概述1.1 Jasypt库简介1.2 Jasypt库的主要特点 二、开发环境三、Jasypt集成到SpringBoot33.1 引入依赖3.2 配置Jasypt3.3 加密配置文件信息3.3.1 方案一&#xff08;不推荐&#xff09;a.编写测试类生成加密后的配置文件信息b.运行c.修改原本的配置文件信息 3.…

探索 GPT-4o mini:成本效益与创新的双重驱动

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

MySQL中的MVCC实现原理与处理流程

1. 什么是MVCC? MVCC,全称Multi-Version Concurrency Control,即多版本并发控制。它是一种并发控制方法,广泛用于数据库管理系统中,以实现对数据库的并发访问。在编程语言中,它实现事务内存。MVCC在MySQL InnoDB中的实现主要是为了提高数据库的并发性能,用更好的方式处…

如何解决 uni-app 项目中 “文件查找失败:‘crypto-js‘“ 的问题

在开发使用 uni-app 框架的项目时&#xff0c;遇到依赖问题是常见的。本文将介绍如何解决编译过程中出现的 “文件查找失败&#xff1a;‘crypto-js’” 错误&#xff0c;并说明这种错误为什么会发生以及如何避免。 问题背景 在对 uni-app 项目进行编译时&#xff0c;我们可能…

【优秀python web系统毕设】基于python的全国招聘数据分析可视化系统,包括随机森林算法

1.1 研究背景 自1997年互联网开始在国内的招聘行业发展至今已有二十几年的历史&#xff0c;互联网招聘进入了蓬勃发展的“黄金时代”。根据智研咨询发布的《2023年中国互联网招聘行业发展现状》报告显示&#xff0c;截至2023年5月&#xff0c;中国互联网招聘平台中&#xff0c…

2-46 基于matlab的声音信号的短时能量、短时过零率、端点检测

基于matlab的声音信号的短时能量、短时过零率、端点检测。通过计算计算短时能量、调整能量门限&#xff0c;然后开始端点检测。输出可视化结果。程序已调通&#xff0c;可直接运行。 2-46 短时能量 短时过零率 端点检测 - 小红书 (xiaohongshu.com)

C++初阶学习第四弹——类与对象(中)

目录 一. 类的默认成员函数 二.六种默认成员函数 1、构造函数 1.1 构造函数的作用 1.2 特性 1.3 默认构造函数 2、析构函数 2.1 析构函数的作用 2.2 析构函数的用法 3、拷贝构造函数 3.1 拷贝构造函数的作用 3.2 特征 3.3 默认拷贝构造函数 三.总结 类与对象&…

Stable Diffusion WebUI本地环境搭建

一、项目代码下载 git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui 二、环境配置 conda create --n stafu python3.10.6 实际上跟自己创建的环境没有关系&#xff0c;项目启动会自动复制这个环境&#xff0c;之后项目根据这个基础环境构建 也可以在自己…

在一个事物方法中开启新事物,完成对数据库的修改

在Java中&#xff0c;使用Transactional注解来管理事务非常常见。但是&#xff0c;在一个已经标记为Transactional的方法内部调用另一个也标记了Transactional的方法时&#xff0c;如果不正确处理&#xff0c;可能会导致一些意料之外的行为。这是因为默认情况下&#xff0c;Spr…

【VUE】封装一个追随鼠标的漂浮组件框架

红色箭头代表鼠标位置&#xff0c;蓝色区域跟随鼠标出现&#xff0c;鼠标进行其他操作的时候&#xff0c;蓝色区域隐藏。 vue全码 <template><divmousemove"updatePosition"mouseleave"hideDiv"class"container":style"{ positi…

《Milvus Cloud向量数据库指南》——BGE-M3:多功能、多语言、多粒度的文本表示学习模型

引言 在自然语言处理(NLP)领域,随着大数据时代的到来,对文本信息的精准处理与高效检索成为了研究热点。BERT(Bidirectional Encoder Representations from Transformers)作为近年来NLP领域的里程碑式模型,以其强大的上下文理解能力在多项任务中取得了显著成效。然而,面…

《C语言实现各种排序算法》

文章目录 一、排序1、排序的各种方式分类 二、插入排序1、直接插入排序2、希尔排序3、希尔排序时间复杂度分析 三、选择排序1、直接选择排序2、堆排序 四、交换排序1、冒泡排序2、快速排序 一、排序 在生活中各种场景中都会有排序的身影存在&#xff0c;在网购时会有价格排序&a…

Apache Nifi挂接MQTT与Kafka实践

目录 1. 说明&#xff1a; 2. 方案设计&#xff1a; 2.1 资源配置&#xff1a; 2.2 交互Topics: 3. 实现步骤 3.1 Nifi 桌面 3.2 MqttToKafka 3.2.1 配置 3.2.2 测试 3.2.3 结果 3.3 KafkaToMqtt 3.3.1 配置 3.3.1 测试 3.3.1 结果 ​编辑 4. 总结&#xff…

Mysql的RedoLog、BingLog、UndoLog

UndoLog undo log 是一种用于撤销回退的日志。在事务没提交之前&#xff0c;MySQL 会先记录更新前的数据到 undo log 日志文件里面&#xff0c;当事务回滚时&#xff0c;可以利用 undo log 来进行回滚。 每当 InnoDB 引擎对一条记录进行操作&#xff08;修改、删除、新增&#…

vue3 命令运行窗口暴露网络地址,以及修改端口号

一般情况下这里的地址是隐藏的 这里加上 --host 可以暴露网络地址&#xff0c;再加上--port --8080 就可以将端口号修改为8080&#xff08;修改后边的数字就可以修改为你想要的端口号&#xff09;

物联网时代下的5G融合定位,可以实现哪些功能?

5G具有高带宽、高频谱&#xff08;毫米波&#xff09;、多天线阵列等特性&#xff0c;通过提升无线定位算法的能力、室内数字系统建设、完善5G定位服务流程以及与其它定位技术和平台的结合&#xff0c;可提高5G定位精度。室内高精度定位服务为5G定位扩展到更多应用场景和领域构…