前言
记录一下使用reids多端口配置一主两从和三个哨兵服务
环境
虚拟机:kvm 系统:Linux 版本:CentOS 镜像:Rocky8.6 redis版本:5.0.3
redis配置
1)编写/etc/redis.conf文件
vim /etc/redis.conf
69 bind 127.0.0.1 # 设置服务ip,因为是本地多端口所以设置127.0.0.1如果需要可设置本机ip
88 protected-mode no # 关闭保护模式,因为没有设置密码
136 daemonize yes # 保护进程,开启后redis可后台运行
324 replica-read-only no # 设置主节点可读可写
创建目录
创建目录位置可以自定,本文直接在root目录下创建
mkdir -p redisconf/638{0,1}
编写第二个redis.conf文件
vim redisconf/6380/redis.conf
bind 127.0.0.1
port 6380 # 设置端口为6380
protected-mode no
daemonize yes
logfile ./redis.log
pidfile /var/run/redis_6480.pid
save 900 1
save 300 10
save 60 10000
#上面3个为每900s更新1条,每300s更新10条和每60s更新10000条时保存到文件
replica-read-only yes
编写第三个redis.conf文件
vim redisconf/6381/redis.conf
bind 127.0.0.1
port 6381 # 设置端口为6381
protected-mode no
daemonize yes
logfile ./redis.log
pidfile /var/run/redis_6481.pid
save 900 1
save 300 10
save 60 10000
replica-read-only yes
2)起服务
使用systemctl起系统服务:
systemctl start redis
使用redis-server起自定义服务:
redis-server /root/redisconf/6380/redis.conf # 启动6380端口redis服务,接redis.conf文件绝对路径或相对路径都可以
redis-server /root/redisconf/6380/redis.conf # 启动6381端口
查询端口检验服务是否启动
ss -antulp | grep redis
3)配置主从服务
方法一:修改配置文件
ps:设置其他为从,剩下的默认为主
修改从服务器的配置文件:
vim root/redisconf/6380/redis.conf
在最后一行加上
replicaof 127.0.0.1 6379 # 指定6379作为主
使用kill杀死服务后重启,6381端口用同样方式配置6379为主
方法二 : 使用redis服务修改
redis-cli -h 127.0.0.1 -p 6380 # 连接6380端口服务
replicaof 127.0.0.1 6379 # 设置6379为主
config rewrite # 永久保存设置
info replication # 查看信息
exit # 退出redis服务
6381同样配置方式
4)配置哨兵服务
修改配置文件
vim /etc/redis-sentinel.conf # 修改哨兵模式配置文件
16 bind 127.0.0.1 #i 指定哨兵ip
17 protected-mode no
26 daemonize yes
84 sentinel monitor mymaster 127.0.0.1 6379 2 # 指定监控本机6379端口,2为当2个哨兵服务作出判断时运行服务
创建目录
mkdir -p senconf/2638{0,1}
ps : 这里目录为/root下完整路径为/root/senconf/26380和/root/senconf/26381
复制配置文件
cp /etc/redis-sentinel.conf senconf/26380/
cp /etc/redis-sentinel.conf senconf/26381/
修改配置文件中端口
vim senconf/26380/redis-sentinel.conf
21 port 26380 # 修改端口
31 pidfile "/var/run/redis-sentinel_26380.pid" # 修改pid文件
245 logfile /var/log/redis/sentineli_26380.log # 修改日志文件
vim senconf/26381/redis-sentinel.conf
21 port 26381
31 pidfile "/var/run/redis-sentinel_26381.pid"
245 logfile /var/log/redis/sentineli_26380.log
起服务
cd /root/senconf/26380/
redis-sentinel redis-sentinel.conf
cd /root/senconf/26381/
redis-sentinel redis-sentinel.conf
查看端口检验服务是否启动
ss -antulp | grep redis
ps:
1、哨兵服务建议一次将三个配置文件配置好在起服务,因为哨兵起服务后会自动生成id号,redis从机清单和其他哨兵清单,所以都配置好后会更加方便。如果已经生成数据且不知道怎么修改可以卸载redis重新安装,redis.conf文件会保存为/etc/redis.conf.rpmsave文件,修改名字就行,然后重新修改redis-sentinel文件。
2、文中有些127.0.0.1的地方无法修改为localhost,使用localhost会导致识别不了ip。
总结
本文记录了我通过使用不同端口起多个redis服务并配置哨兵的实现过程和遇到的问题,如果能有效帮助其他人就挺不错的·。