redis5 集群自启动

环境:CentOS8

本文是单机伪集群

一、配置系统服务(推荐)

1、创建文件/etc/rc.d/init.d/redisc

#!/bin/sh
# chkconfig: 2345 80 90
# description: Redis cluster start and stop
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

EXEC=/opt/redis5-cluster/bin/redis-server
CLIEXEC=/opt/redis5-cluster/bin/redis-cli

PORT1=7001
PORT2=7002
PORT3=7003
PORT4=7004
PORT5=7005
PORT6=7006

CONF1=/opt/redis5-cluster/${PORT1}/redis.conf
CONF2=/opt/redis5-cluster/${PORT2}/redis.conf
CONF3=/opt/redis5-cluster/${PORT3}/redis.conf
CONF4=/opt/redis5-cluster/${PORT4}/redis.conf
CONF5=/opt/redis5-cluster/${PORT5}/redis.conf
CONF6=/opt/redis5-cluster/${PORT6}/redis.conf

PASSWORD=redis508

PIDFILE=/opt/redis5-cluster/${PORT1}/redis.pid

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting ..."
                $EXEC $CONF1 #Can add &, Without & process have order
                $EXEC $CONF2
                $EXEC $CONF3
                $EXEC $CONF4
                $EXEC $CONF5
                $EXEC $CONF6
                echo "Redis cluster started"
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $PORT1 -a $PASSWORD shutdown 2>/dev/null #2>/dev/null Ignore err
                $CLIEXEC -p $PORT2 -a $PASSWORD shutdown 2>/dev/null
                $CLIEXEC -p $PORT3 -a $PASSWORD shutdown 2>/dev/null
                $CLIEXEC -p $PORT4 -a $PASSWORD shutdown 2>/dev/null
                $CLIEXEC -p $PORT5 -a $PASSWORD shutdown 2>/dev/null
                $CLIEXEC -p $PORT6 -a $PASSWORD shutdown 2>/dev/null
                while [ -x /proc/${PID} ] #PID is process ID,not port
                do
                    echo "Waiting for redis cluster to shutdown ..."
                    sleep 1
                done
                echo "Redis cluster stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

2、加可执行权限

chmod +x /etc/rc.d/init.d/redisc

3、注册为系统服务(删除系统服务)

chkconfig --add redisc

chkconfig --del redisc

4、添加开机自启动(关闭开机自启动)

chkconfig redisc on

chkconfig redisc off

5、启动、关闭redis集群

service redisc start

service redisc stop

6、查看服务列表

chkconfig --list

redisc             0:关    1:关    2:开    3:开    4:开    5:开    6:关

二、配置/etc/rc.d/rc.local

加入

/opt/redis5-cluster/bin/redis-server /opt/redis5-cluster/7001/redis.conf
/opt/redis5-cluster/bin/redis-server /opt/redis5-cluster/7002/redis.conf
/opt/redis5-cluster/bin/redis-server /opt/redis5-cluster/7003/redis.conf
/opt/redis5-cluster/bin/redis-server /opt/redis5-cluster/7004/redis.conf
/opt/redis5-cluster/bin/redis-server /opt/redis5-cluster/7005/redis.conf
/opt/redis5-cluster/bin/redis-server /opt/redis5-cluster/7006/redis.conf

加可执行权限:chmod +x /etc/rc.d/rc.local

三、另外一种配置

Linux重启 Redis自动启动
https://blog.csdn.net/yexiaomodemo/article/details/94626231
redis 设置自启动
http://www.mamicode.com/info-detail-2794774.html

四、其他问题

如果redis.conf配置了bind IP,可能自启动失败,

(error) CLUSTERDOWN The cluster is down

Could not create server TCP listening socket 192.168.0.201:7001: bind: Cannot assign requested address

bind的意思不是绑定外部服务器的IP,而是绑定本机可以接受访问的IP

把bind IP注释了就好了,或者bind 0.0.0.0,没必要绑定IP

 

参考:

配置Redis集群为开机自启动
https://www.cnblogs.com/alexzhang92/p/11008102.html

你可能感兴趣的:(redis)