在 CentOS 上配置 Redis 主从(Master-Slave,Redis 5+ 也叫 Replica)一般分为安装 Redis → 配置主节点 → 配置从节点 → 验证几个步骤。下面以 CentOS 7/8 为例说明。
假设:
两个节点都先安装 Redis(推荐用 yum 或编译安装)。
yum install -y epel-release
yum install -y redis
启动并设置开机自启:
systemctl enable redis
systemctl start redis
主节点一般默认就是主,只需确认配置即可。
编辑配置文件:
vi /etc/redis.conf
关键配置:
bind 0.0.0.0
protected-mode no
port 6379
如果你需要密码:
requirepass 123456
重启 Redis:
systemctl restart redis
在 从节点 上编辑配置文件:
vi /etc/redis.conf
slaveof 192.168.1.10 6379
masterauth 123456 # 如果主节点有密码
replicaof 192.168.1.10 6379
masterauth 123456
其他建议配置:
bind 0.0.0.0
protected-mode no
重启 Redis:
systemctl restart redis
redis-cli
info replication
看到类似:
role:slave
master_host:192.168.1.10
master_link_status:up
说明主从同步正常。
在 Master 写数据:
redis-cli -h 192.168.1.10 -a 123456
set test hello
在 Slave 读数据:
redis-cli -h 192.168.1.11
get test
能读到说明成功。
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload
master_link_statusmasterauthtail -f /var/log/redis/redis.log
从节点默认只读:
replica-read-only yes
如果你需要 一主多从 / 哨兵模式 / 集群模式 的配置,我也可以继续帮你写。