Redis配置哨兵开机自启动


  • Redis哨兵

启停命令

# 启动3台redis
cd /opt/redis
./redis-server redis.conf

# 启动3台sentinel
cd /opt/redis
./redis-sentinel sentinel.conf

测试sentinel模式

cd /opt/redis
./redis-cli -h master的ip

192.168.0.205:6379> auth 设置的密码
192.168.0.205:6379> info
  • 方案一
    脚本放到/etc/init.d/redisSentinel
    添加开机自启动:
# 1.编辑rc.local文件
vi /etc/rc.local
# 2.添加redisSentinel 启动命令
/etc/init.d/redisSentinel restart
# 3.最后修改rc.local文件的执行权限--一定要执行
chmod +x  /etc/rc.local
chmod 755 /etc/rc.local
vim /etc/init.d/redisSentinel
#!/bin/sh
#
# chkconfig: 2345 10 90
# description: Start and Stop redis
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO


REDISPORT=6379
REDIS_PORT=26379
EXEC=/opt/redis/redis-server
CLIEXEC=/opt/redis/redis-cli
EXEC_SENTINEL=/opt/redis/redis-sentinel
PIDFILE=/run/redis_${REDISPORT}.pid
#Redis配置文件位置
CONF="/opt/redis/redis.conf"
#sentinel 配置位置
#SlCONF="/opt/redis/sentinel.conf"
SlCONF="/opt/redis/sentinel.conf --sentinel"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
                $EXEC $SlCONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                # -a '123' 这里是该redis服务密码 关闭redis服务须要该密码支持⚠️其他2台修改服务器IP
                $CLIEXEC -h "172.16.xx.xx" -a "123" -p $REDISPORT shutdown
                $CLIEXEC -h "172.16.xx.xx" -p $REDIS_PORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    #status)
    #    PID=$(cat $PIDFILE)
    #    if [ ! -x /proc/${PID} ]
    #    then
    #        echo 'Redis Sentinel is not running'
    #    else
    #        echo "Redis Sentinel is running ($PID)"
    #    fi
    #    ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac
chmod 777 redisSentinel
systemctl daemon-reload

# 启动
service redisSentinel start
# 停止
service redisSentinel stop
# 重启
service redisSentinel restart
### 查看状态
### service redisSentinel status

配置 redis 启动、停止脚步 redis.sh

#!/bin/bash
case $1 in
    "start")
        if(ps -ef | grep redis-server | grep -v grep > /dev/null); then
            echo "Redis is running!"
        else
            src/redis-server redis.conf
        fi
    ;;
    "stop")
        if(ps -ef | grep redis-server | grep -v grep > /dev/null); then
            kill -9 `ps -ef | grep redis-server | grep -v grep | awk '{print $2}'`
        else
            echo "Redis is not running!"
        fi
    ;;
    "restart")
        if(ps -ef | grep redis-server | grep -v grep > /dev/null); then
            kill -9 `ps -ef | grep redis-server | grep -v grep | awk '{print $2}'`
            src/redis-server redis.conf
        else
            echo "Redis is not running!"
            src/redis-server redis.conf
        fi
    ;;
    *)
        echo "Input Error! Example: sh redis.sh start | stop | restart"
    ;;
esac

配置 sentinel 启动、停止脚步 sentinel.sh

#!/bin/bash
case $1 in
    "start")
        if(ps -ef | grep redis-sentinel | grep -v grep > /dev/null); then
            echo "Redis sentinel is running!"
        else
            src/redis-sentinel sentinel.conf
        fi
    ;;
    "stop")
        if(ps -ef | grep redis-sentinel | grep -v grep > /dev/null); then
            kill -9 `ps -ef | grep redis-sentinel | grep -v grep | awk '{print $2}'`
        else
            echo "Redis sentinel is not running!"
        fi
    ;;
    "restart")
        if(ps -ef | grep redis-sentinel | grep -v grep > /dev/null); then
            kill -9 `ps -ef | grep redis-sentinel | grep -v grep | awk '{print $2}'`
            src/redis-sentinel sentinel.conf
        else
            echo "Redis sentinel is not running!"
            src/redis-sentinel sentinel.conf
        fi
    ;;
    *)
        echo "Input Error! Example: sh redis.sh start | stop | restart"
    ;;
esac
#启动redis实例
sh redis.sh start

#启动sentinel实例
sh sentinel.sh start


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM