温馨提示×

centos redis主从怎么配置

小樊
39
2026-09-08 21:27:35
栏目: 云计算

下面以 CentOS 7/8 为例,介绍 Redis 主从(Master-Slave) 的配置方式,适用于 Redis 5/6/7。


一、环境假设

角色 IP 端口
Master 192.168.1.10 6379
Slave 192.168.1.11 6379

二、安装 Redis(主从都要)

# CentOS 7/8
yum install -y epel-release
yum install -y redis

启动并开机自启:

systemctl enable redis
systemctl start redis

三、配置 Master(主节点)

编辑配置文件:

vi /etc/redis.conf

关键配置:

bind 0.0.0.0
protected-mode no
port 6379
daemonize yes

(如有密码)

requirepass 123456

重启:

systemctl restart redis

四、配置 Slave(从节点)

编辑从节点配置:

vi /etc/redis.conf

核心配置:

bind 0.0.0.0
port 6379
replicaof 192.168.1.10 6379

如果 Master 有密码:

masterauth 123456

重启:

systemctl restart redis

五、验证主从状态

Slave 上执行:

redis-cli
info replication

应看到:

role:slave
master_host:192.168.1.10
master_link_status:up

Master 上:

redis-cli
info replication

应看到:

role:master
connected_slaves:1

六、测试同步

Master:

redis-cli -a 123456 set test hello

Slave:

redis-cli get test

返回 hello 即成功。


七、常见问题

1. 防火墙

firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload

2. 只读

从节点默认只读:

replica-read-only yes

3. Redis 7 名称变化

Redis 7 中:

  • slaveofreplicaof
  • slavereplica

八、可选:一主多从

多个 Slave 只需都配置:

replicaof 192.168.1.10 6379

如果你需要 哨兵(Sentinel)Cluster 集群 配置,也可以告诉我。

0