redis主从复制、哨兵、集群配置

查看你可能添加到防火墙的所有可用服务列表,请运行

firewall-cmd --get-services

查看你当前防火墙上启用的服务,请使用

firewall-cmd --list-services

要在防火墙中添加redis服务

firewall-cmd --zone=public --add-service=redis --permanent

让修改生效,重新加载

firewall-cmd --reload

移除把add改为remove

一主二从

编辑配置文件

vim /etc/redis/redis.conf

1、开启daemonize yes

  默认no

2、注释掉bind 127.0.0.1

  默认 bind 127.0.0.1 -::1

3、密码,requirepass

  默认# requirepass foobared 把#号去掉foobared改为自己得密码

4、protected-mode no

  默认yes

5、指定端口,port

  默认6379

6、指定当前工作目录,dir

  默认dir /var/lib/redis

7、pid文件名称,pidfile

  默认注释,把前面#去掉 pidfile /run/redis/redis-server.pid

8、log文件名称,logfile

  默认 logfile /var/log/redis/redis-server.log

9、dump.rdb名字,dbfilename

  默认 dbfilename dump.rdb

10、aof文件

appendonly yes

  默认no

appendfilename

  默认 appendfilename "appendonly.aof"

appenddirname

  默认 appenddirname "appendonlydir"

11,从机配置

replicaof master主机ip地址 master端口 

例如:replicaof 192.168.160.135 6379

访问主机的通行密码(必须,主机也得配,因为主也会变成从)
masterauth 111111

 

先启动主再启动从

systemctl start redis 

连接上redis,然后查看主从信息

redis-cli -a 111111

info replication

 

Redis复制得三个命令

1、主从复制

  replicaof 主IP 主端口

2、另寻新主(改换门庭)

  slaveof 新主IP 新主端口

3、自立为主(农奴翻身把歌唱)

  slaveof no one

一主二从三哨兵

添加哨兵服务到防火墙,哨兵之间通信

firewall-cmd --zone=public --add-service=redis-sentinel --permanent

firewall-cmd --reload

配置哨兵

vim /etc/redis/sentinel/sentinel.conf

bind 0.0.0.0

daemonize yes

  默认yes

protected-mode no

  默认no

port 26379

  默认26379

logfile

  默认logfile /var/log/redis/redis-sentinel.log

pidfile

  默认 pidfile /var/run/redis-sentinel.pid

dir

  默认 dir /var/lib/redis

sentinel monitor <master-name> <ip> <redis-port> <quorum>

例如:sentinel monitor mymaster 192.168.160.135 2

sentinel auth-pass <master-name> <password>

例如:sentinel auth-pass mymaster 111111

三主三从

打开16379集群总线端口(这个端口是在你配置端口+10000)

firewall-cmd --permanent --add-port=16379/tcp

firewall-cmd --reload

6379端口也要添加或把redis服务添加到防火墙

配置文件:

vim /etc/redis/redis.conf

bind 0.0.0.0

daemonize yes

protected-mode no

port 6379

----------------------------------

下面6个全都采用默认

dir 

logfile 

pidfile  

dbfilename

appendirname  

appendfilename

-----------------------------------

appendonly yes

requirepass 111111

masterauth 111111

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

配置好6台后全部启动起来

构建集群,以一主一从得方式构建3主3从

  库必须为空,用FLUSHALL清空每个库

redis-cli -a 111111 --cluster create --cluster-replicas 1 192.168.160.132:6379 192.168.160.133:6379 192.168.160.134:6379 192.168.160.135:6379 192.168.160.136:6379 192.168.160.137:6379

 

redis-cli -a 111111 -c 对集群进行读写,要加上-c参数

查看信息

info replication

cluster info

cluster nodes

  

posted on 2026-07-03 15:22  wtsgtc  阅读(2)  评论(0)    收藏  举报

导航