在 CentOS 上配置 Redis 主从(Master-Slave / Replication) 通常很简单,核心思路是:主节点正常启动,从节点配置指向主节点。
下面以 CentOS 7/8 + Redis 6/7 为例说明。
假设有两台服务器:
| 角色 | IP | 端口 |
|---|---|---|
| Master | 192.168.1.10 | 6379 |
| Slave | 192.168.1.11 | 6379 |
如果是单机测试,也可以在本机用不同端口(如 6379 / 6380)
# CentOS 7/8
yum install epel-release -y
yum install redis -y
# 或者编译安装(推荐新版本)
启动 Redis:
systemctl enable redis
systemctl start redis
编辑 /etc/redis.conf:
bind 0.0.0.0
protected-mode no
port 6379
生产环境建议 bind 内网 IP + 设置密码
requirepass 123456
systemctl restart redis
编辑从节点的 /etc/redis.conf:
replicaof 192.168.1.10 6379
# Redis 5 以前叫 slaveof
# 如果主节点有密码
masterauth 123456
保存后重启:
systemctl restart redis
redis-cli
> replicaof 192.168.1.10 6379
> config set masterauth 123456
redis-cli info replication
应看到:
role:slave
master_host:192.168.1.10
master_link_status:up
redis-cli info replication
应看到:
role:master
connected_slaves:1
主节点:
redis-cli set test hello
从节点:
redis-cli get test
# 返回 "hello"
✅ 同步成功
默认从节点不能写:
READONLY You can't write against a read only replica
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload
确保安全组放行 6379
如果你需要:
可以告诉我你的使用场景,我可以直接给你完整配置。