当前位置: 代码迷 >> 综合 >> redis6.0.9 三台哨兵搭建
  详细解决方案

redis6.0.9 三台哨兵搭建

热度:100   发布时间:2023-11-25 21:53:53.0

redis6.0.9 三台哨兵搭建

  • 环境准备
    • 服务器(CentOS Linux release 8.2.2004)
    • 安装包(redis-6.0.9.tar.gz)
  • 开干
    • 安装
    • 配置文件
      • reids1
        • redis开机自启动脚本
        • redis服务配置文件:redis.cnf
        • redis哨兵配置文件:sentinel.conf
        • redis访问权限列表配置文件:/etc/redis/users.acl
      • reids2
        • redis开机自启动脚本
        • redis服务配置文件:redis.cnf
        • redis哨兵配置文件:sentinel.conf
      • reids3
        • redis开机自启动脚本
        • redis服务配置文件:redis.cnf
        • redis哨兵配置文件:sentinel.conf

redis 6.0出ACL(Access Control List,访问权限列表)新特性了,用之

环境准备

服务器(CentOS Linux release 8.2.2004)

reids1:172.165.165.101
reids2:172.165.165.102
reids3:172.165.165.103

安装包(redis-6.0.9.tar.gz)

linux版本下载地址:http://download.redis.io/releases/
windows版本下载地址:https://github.com/tporadowski/redis/releases

redis-6.0.9.tar.gz

开干

由于,三台服务器的配置文件都大同小异,所以只上重要的地方,具体配置文件详情请下载配置文件自行查看

安装

这个太没有技术含量,自行百度,或者看官网说明:https://redis.io/download

配置文件

请以官方说明为准,用别人的软件,一定要看别人官方的说明。
官方文档地址:https://redis.io/documentation
个人redis.conf、sentinel.conf翻译:https://blog.csdn.net/qq_37809967/article/details/110508298
配置文件压缩包:

reids1

redis开机自启动脚本

从 /usr/local/redis-6.0.9/utils 复制 redis_init_script 获取,并进行部分自定义修改

REDISPORT=6379
# 根据自己的redis base地址配置
EXEC=/usr/local/src/redis-server
CLIEXEC=/usr/local/src/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;*)echo "Please use start or stop as first argument";;
esac

redis服务配置文件:redis.cnf

# 访问 ip:端口限制
# 使用以下配置则表示这个redis服务,在客户端只能通过 172.165.165.101:6379连接,这样保证即使redis部署的机器存在多个网络环境,其余的网络环境也无法访问redis服务
protected-mode yes
bind 172.165.165.101
port 6379
# 主 同步账号
masteruser replica-user
# 主 同步账号密码
masterauth b45564eca06dc15f104505136a96e5a26739e84c1ac8cc2f302d4c03da0f4af0
# 告诉主通过下面的ip端口可以连接到自己
replica-announce-ip 172.165.165.101
replica-announce-port 6379
# 守护进程后台运行
daemonize yes
# 指定pid文件,这里有点小坑坑,如果使用官方推荐的脚本启动redis的话,一定要保证这个pid文件对的上脚本里面停止指令的pid文件查找
pidfile /data/redis_6379.pid

redis哨兵配置文件:sentinel.conf

# 开启保护模式设定可访问 ip (同redis服务一样)
bind 127.0.0.1 172.165.165.101
# 开启保护模式
protected-mode yes
# 指定端口
port 26379
# 守护进程后台运行
daemonize yes
# pid文件
pidfile /var/run/redis-sentinel_26379.pid
# 日志文件
logfile "/data/redis/log/sentinel_26379.log"

redis访问权限列表配置文件:/etc/redis/users.acl

三个redis的用户配置必须一样。所以acl配置文件都是一样的

user default off nopass ~* +@all
user admin-user on #6073ee0c1f33a7c55c023cfb5ae41202634ec5d47f5839766d7e9c72784692c7 ~* +@all
user redis-user on #b29890a1832f93ab561d6e5da14ac6cd6688a72674da66ba342e52dfb3fced0b ~* +@all -@dangerous +get +set
user stop-user on #aa655477ccee0866310f71602923f37351245c53fe77c178cf2f79b04d0b2cd0 +SHUTDOWN
user sentinel-user on #f52269d79c568147710decedb57dac72400211768eb1137097b5f67233f4290d -@all +subscribe +publish +client|setname +client|kill +config|rewrite +role +exec +slaveof +info +multi +ping +script|kill
user replica-user on #3ee15bb3d499740ccdc5ebf49b6f8b3f4a43dfc74b1597f6f3822023cbab008a -@all +ping +psync +replconf

reids2

redis开机自启动脚本

从 /usr/local/redis-6.0.9/utils 复制 redis_init_script 获取,并进行部分自定义修改

REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;*)echo "Please use start or stop as first argument";;
esac

redis服务配置文件:redis.cnf

protected-mode yes
bind 172.165.165.102
port 6379
masteruser replica-user
masterauth b45564eca06dc15f104505136a96e5a26739e84c1ac8cc2f302d4c03da0f4af0
replica-announce-ip 172.165.165.102
replica-announce-port 6379
daemonize yes
pidfile /data/redis_6379.pid

redis哨兵配置文件:sentinel.conf

bind 127.0.0.1 172.165.165.102
protected-mode yes
port 26379
daemonize yes
pidfile /var/run/redis-sentinel_26379.pid
logfile "/data/redis/log/sentinel_26379.log"

reids3

redis开机自启动脚本

从 /usr/local/redis-6.0.9/utils 复制 redis_init_script 获取,并进行部分自定义修改

REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;*)echo "Please use start or stop as first argument";;
esac

redis服务配置文件:redis.cnf

protected-mode yes
bind 172.165.165.103
port 6379
masteruser replica-user
masterauth b45564eca06dc15f104505136a96e5a26739e84c1ac8cc2f302d4c03da0f4af0
replica-announce-ip 172.165.165.103
replica-announce-port 6379
daemonize yes
pidfile /data/redis_6379.pid

redis哨兵配置文件:sentinel.conf

bind 127.0.0.1 172.165.165.103
protected-mode yes
port 26379
daemonize yes
pidfile /var/run/redis-sentinel_26379.pid
logfile "/data/redis/log/sentinel_26379.log"

配置文件压缩包:
链接:https://pan.baidu.com/s/1bILDqHbrlNl8FsrfyO_hWQ
提取码:1234

由于博主水平有限,文中可能存在错误。发现博主哪里写的不对,请指出。一定及时改正,谢谢!