实战环境
Master(主机):192.168.95.88
Slave1(从机):192.168.95.133
Slave2(从机):192.168.95.131
操作系统:OpenEuler24.03
不考虑selinux、防火墙等因素,确保3台主机能通且有网络
在 Master、Slave1、Slave2 三台机器上都执行以下命令:
yum -y install redis
Master(主机):
①cp /etc/redis/redis.conf /etc/redis/redis.conf.bak
② vim /etc/redis.conf 进入编辑器里修改如下内容
1. 允许其他机器访问(必须改)
bind 127.0.0.1 ← 注释掉这一行(加 #)
bind 0.0.0.0 # 或者直接写成 bind 192.168.95.88
2. 关闭保护模式(必须)
protected-mode no
3. 后台运行(推荐)
daemonize yes
③退出并重启服务
Slave1 Slave2(从机):
①cp /etc/redis/redis.conf /etc/redis/redis.conf.bak
② vim /etc/redis.conf 进入编辑器里修改如下内容
1. 允许其他机器访问(必须改)
bind 127.0.0.1 ← 注释掉
bind 0.0.0.0
2. 关闭保护模式(必须)
protected-mode no
3. 后台运行
daemonize yes
4. 【关键】配置主从复制(新增或取消注释)
replicaof 192.168.95.88 6379
指向 Master 的 IP 和端口
5. 如果 Master 以后要加密码,这里再加(当前无密码可忽略)
masterauth yourpassword
③退出并重启服务
配置完毕,我们只需要完成如下验证:
redis-cli
进入redis界面输入如下命令
INFO replication
关键信息:master_link_status:up
搞定!