> 文章列表 > 哨兵模式自启动

哨兵模式自启动

哨兵模式自启动

哨兵模式的配置具体看这里

首先在redis的解压目录下找到redis_init_script文件

#这个文件在这个目录下
[root@centos7964 utils]# pwd
/usr/local/src/redis-5.0.7/utils
#将该文件复制到/etc/init.d/目录下,并重命名为redis_init
cp redis_init_script /etc/init.d/redis_init

然后使用vim命令编辑此文件

#原文件
#!/bin/sh
#
# 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 INFOREDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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)
"redis_init_script" 50L, 1352C     

然后我们编辑后的内容为

# 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
#下面这些是我们的redis的安装目录以及配置文件
REDISPORT=6379
REDIS_SENTINELPORT=26379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
CLIEXEC_SENTINEL=/usr/local/bin/redis-sentinelPIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/bin/config/redis.conf"
#注意后面的--sentinel一定要写,不然会报错
SlCONF="/usr/local/bin/config/sentinel.conf --sentinel"
REDISPASSWORD="12345"
case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONF#因为我们要启动哨兵,所以这里要写sentinel的配置文件$EXEC $SlCONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."#由于我们设置了密码所以需要加上-a password标签$CLIEXEC -a $REDISPASSWORD -p $REDISPORT shutdown$CLIEXEC -p $REDIS_SENTINELPORT 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                                                  

  以上配置保存好执行以下命令即可启动和停止

/etc/init.d/redis_master start       //启动redis服务/etc/init.d/redis_master stop       //关闭redis服务或service redis_master start        //启动redis服务
service redis_master stop        //关闭redis服务
chkconfig redis_master on       //设为开机启动
chkconfig redis_master off       //设为开机关闭
#执行启动命令
[root@centos7964 init.d]# service redis_init start
/var/run/redis_6379.pid exists, process is already running or crashed
#连接
[root@centos7964 init.d]# redis-cli -p 6379
127.0.0.1:6379> info replication
NOAUTH Authentication required.
127.0.0.1:6379> auth 12345
OK