一、redis的主从配置

1.先在各个机器安装redis

2.主的机器配置,找到机器的安装目录redis.conf

# redis-6379.conf (主节点配置)
port 6379
bind 192.168.1.10            # 改为你的主节点内网IP
protected-mode no            # 允许非本地访问(配合bind使用)
requirepass YourStrongPass   # 强烈建议设置,从节点连接时需要
daemonize yes                # 后台运行
pidfile /var/run/redis-6379.pid
logfile /var/log/redis-6379.log
dir /data/redis-6379         # 数据存储目录

3.从的机器配置,找到机器的安装目录redis.conf

# redis-6380.conf (从节点配置)
port 6380
bind 192.168.1.11            # 改为你的从节点内网IP
protected-mode no

# ----- 关键复制配置(必填)-----
replicaof 192.168.1.10 6379  # 指定主节点的IP和端口
masterauth YourStrongPass    # 主节点的密码(必须与主节点的requirepass一致)

# ----- 其他配置 -----
daemonize yes
pidfile /var/run/redis-6380.pid
logfile /var/log/redis-6380.log
dir /data/redis-6380

注意:注意:从节点默认是只读的(replica-read-only yes),这能防止数据意外写入从节点导致主从数据不一致,建议保持默认。

4.验证主从

redis-cli -h 192.168.1.10 -p 6379 -a YourStrongPass info replication

5.数据检测

主redis

redis-cli -h 192.168.1.10 -p 6379 -a YourStrongPass set testkey "hello"

从redis

redis-cli -h 192.168.1.11 -p 6380 -a YourStrongPass get testkey

二、哨兵(redis-sentinel)

1.查找哨兵,本身就是redis的一个组件(redis/src/)中

which redis-sentinel
usr/bin/which: no redis-sentinel in (/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin)

如果没有找到,将哨兵配置到环境变量修改用户级的 .bashrc

echo 'export PATH=/www/server/redis/src:$PATH' >> ~/.bashrc
source ~/.bashrc

验证是否配置成功

# 检查能否找到 redis-server
which redis-server
# 检查能否找到 redis-sentinel
which redis-sentinel
# 检查能否找到 redis-cli
which redis-cli

2.配置哨兵/etc/redis-sentinel/sentinel.conf  新建.三台都配置,只是绑定的Ip不一样bind

# ==================== 基础网络配置 ====================
port 26379
# ⚠️ 改成这台机器自己的内网IP
bind 170.11.12.03                 
protected-mode no
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile /data/app/redis/redis-sentinel/log/redis-sentinel.log
# 持久化目录,需要确保目录存在且有写权限
dir /data/app/redis/redis-sentinel/cache/redis-sentinel         

# ==================== 核心监控配置 ====================
# 格式:sentinel monitor <主节点名称> <主节点IP> <主节点端口> <quorum>
sentinel monitor mymaster 170.11.12.01 6379 2

# 主节点密码(必须与 requirepass 一致)
sentinel auth-pass mymaster 123456

# 主节点密码(如果主从节点密码相同,用上面一条即可;如果不同,还需要配置下面这条)
# sentinel auth-user mymaster your-username   # Redis 6.0+ ACL 用户认证时需要

# ==================== 故障检测与转移参数 ====================
# 主观下线时间(毫秒):超过此时间未收到PONG,哨兵认为该节点主观下线
sentinel down-after-milliseconds mymaster 30000

# 故障转移超时时间(毫秒)
sentinel failover-timeout mymaster 180000

# 故障转移时,同时进行同步的从节点数量(设为1可最大程度保证数据一致性)
sentinel parallel-syncs mymaster 1

# 通知脚本(可选,故障转移时执行,用于报警)
# sentinel notification-script mymaster /path/to/notification.sh

# 客户端重新配置脚本(可选,故障转移后执行,用于更新客户端配置)
# sentinel client-reconfig-script mymaster /path/to/reconfig.sh
参数 示例值 说明
bind 170.11.12.03  必须改成每台机器的实际内网IP,不能写 127.0.0.1,否则其他哨兵无法通信。
sentinel monitor mymaster 170.11.12.01  6379 2 mymaster 是主节点别名(三台机器必须一致),2 是 quorum(法定票数),表示至少2个哨兵认为主节点挂了才会触发故障转移。
sentinel auth-pass YourStrongPass 必须填写,和你主节点的 requirepass 密码一致,否则哨兵无法操作Redis节点。
down-after-milliseconds 30000 30秒无响应即判定为“主观下线”(SDOWN)。网络环境稳定可设小一点,反之设大一点防止误判。
failover-timeout 180000 故障转移总超时3分钟。如果转移过程中出现问题,超过这个时间会中断本次转移。
parallel-syncs 1 故障转移后,同时向新主节点同步的从节点数量。设为1可以逐个同步,减少对主节点的压力。

三台机器的配置文件只有 bind 这一行不一样,其余完全一致:

机器IP bind 配置
170.11.12.01 bind 170.11.12.01
170.11.12.02 bind 170.11.12.02
170.11.12.03  bind 170.11.12.03 

3.在每台机器上创建哨兵需要的目录:

mkdir -p /data/app/redis/redis-sentinel/cache/redis-sentinel  
chown redis:redis /data/app/redis/redis-sentinel/cache/redis-sentinel     # 如果 Redis 以 redis 用户运行

注意:dir 目录必须存在且有写权限,否则哨兵启动会报错。

4.启动哨兵时指定这个文件即可:

redis-sentinel /etc/redis-sentinel/sentinel.conf

 5.验证

ps aux | grep sentinel

6.连接哨兵查看状态:

/www/server/redis/src/redis-cli -h 170.11.12.1 -p 26379 info sentinel

7. 哨兵系统自动启动

第一步:确认命令路径

先确认 redis-sentinel 和 redis-cli 的完整路径:

which redis-sentinel
which redis-cli

第二步:创建 systemd 服务文件

sudo vim /etc/systemd/system/redis-sentinel.service

将以下内容粘贴进去(请把 ExecStart 和 ExecStop 中的路径替换为你第一步查到的实际路径):

[Unit]
Description=Redis Sentinel
After=network.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/www/server/redis/src/redis-sentinel /etc/redis-sentinel/sentinel.conf --sentinel
# 关键修复:指定正确的 IP 地址
ExecStop=/www/server/redis/src/redis-cli -h 10.100.1.12 -p 26379 shutdown
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

保存并退出(:wq)。

第三步:优雅接管当前哨兵进程

配置文件中的 daemonize 设置

如果 sentinel.conf 中有 daemonize yes,哨兵会自己后台化,这会导致 systemd 认为进程退出(因为主进程 fork 后退出),从而触发自动重启

检查配置文件:

grep daemonize /etc/redis-sentinel/sentinel.conf

如果有 daemonize yes,需要改为 daemonize no(因为 systemd 管理时不需要哨兵自己后台化):

sudo sed -i 's/daemonize yes/daemonize no/' /etc/redis-sentinel/sentinel.conf
# 1. 用 redis-cli 优雅停止当前哨兵
/www/server/redis/src/redis-cli -h 10.100.1.12 -p 26379 shutdown

# 或者如果上面命令报错,用 kill 信号
# sudo kill -TERM 294131

# 2. 确认哨兵已停止
ps aux | grep sentinel | grep -v grep
# 应该没有输出

第四步:启用并启动 systemd 服务

# 1. 重新加载 systemd
sudo systemctl daemon-reload

# 2. 启用开机自启
sudo systemctl enable redis-sentinel.service

# 3. 启动哨兵
sudo systemctl start redis-sentinel.service

# 4. 查看状态
sudo systemctl status redis-sentinel.service

应该看到类似这样的输出:

● redis-sentinel.service - Redis Sentinel
   Loaded: loaded (/etc/systemd/system/redis-sentinel.service; enabled; vendor preset: disabled)
   Active: active (running) since ...
 Main PID: xxxxx (redis-sentinel)
   CGroup: /system.slice/redis-sentinel.service
           └─xxxxx /usr/local/bin/redis-sentinel /etc/redis-sentinel/sentinel.conf --sentinel

第五步:验证哨兵是否正常工作

# 1. 查看哨兵监控的主节点
/www/server/redis/src/redis-cli -h 10.100.1.12 -p 26379 sentinel masters

# 应该输出类似:
# 1) "name"
# 2) "mymaster"
# 3) "ip"
# 4) "172.21.215.58"
# 5) "port"
# 6) "6379"
# ...

# 2. 查看哨兵日志(最近 10 行)
sudo journalctl -u redis-sentinel.service -n 10

# 3. 确认端口在监听
sudo netstat -tlnp | grep 26379

全部完成后,确认开机自启状态

sudo systemctl is-enabled redis-sentinel.service

三、总结

通过命令连接哨兵是不需要密码的,因为配置文件已经有redis的密码了,但是应用程序连接哨兵是需要密码的

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐