文章目录
前言
本章介绍了在Linux中Redis的主从复制以及添加哨兵模式,该章在同一台虚拟机中采用不同的端口来模拟不同的redis服务器
提示:以下是本篇文章正文内容,下面案例可供参考
一、redis 中集群配置以及主从复制、读写分离说明
二、复制配置文件
将redis中三个配置文件复制三份
cp /usr/local/redis/bin/redis.conf /usr/local/redis/bin/redis6379.conf
cp /usr/local/redis/bin/redis.conf /usr/local/redis/bin/redis6380.conf
cp /usr/local/redis/bin/redis.conf /usr/local/redis/bin/redis6381.conf
二、修改对应的三个配置文件
vim redis6379.conf
vim redis6380.conf
vim redis6381.conf
一共五个地方
三、启动redis
3.1启动redis服务器
在bin目录下
./redis-server redis6379.conf
./redis-server redis6380.conf
./redis-server redis6381.conf
3.2启动redis客户端
./redis-cli -h 192.168.16.132 -p 6379
./redis-cli -h 192.168.16.132 -p 6380
./redis-cli -h 192.168.16.132 -p 6381
查看三台机器的默认角色
info replication
会发现三个都是主机
四、设置主机master
在其它两台从机中通过
slaveof 主机ip 主机端口
slaveof 192.168.16.132 6379
此时再次查看6379中的信息,会出现两台从机:
此时配置完成之后,主redis能够读写,两个从redis中只能够读而不能写
五、配置哨兵模式
5.1 为什么需要配置?
当主redis出故障之后,那么此时就只能读,而不能写入,会出现问题,
那么此时我们就需要设置一个哨兵来监测主redis的状态,
如果主redis出故障了,那么哨兵会将两个从redis中根据配置的算法
自动选择一个从redis作为主redis
5.2 配置哨兵
在bin目录中,创建并编辑sentinel.conf 文件
touch sentinel.conf
vim sentinel.conf
加入 sentinel monitor myredis 192.168.16.132 6379 1
设置哨兵
./redis-sentinel sentinel.conf
总结
以上就是今天要讲的内容,本文仅仅简单介绍了redis主从复制以及配置哨兵,而redis还有大量能更加方便的功能。