1. 创建 Redis 集群的管理脚本
redis-cluster 文件修改说明:
请根据实际情况修改配置文件中的以下圈红内容
redis-cluster 文件配置说明:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# 相关配置项,应该根据实际环境信息进行改动,包括for循环中的PIDFILE和CONF
REDISPORT=(7000 7001 7002 7003 7004 7005)
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
# redis集群密码,如未设置密码,该配置项可去除,第43行中redis集群关闭指令也需要做相应修改
PASSWD=admin123456
for((i=0;i<${#REDISPORT[@]};i++)) do
PIDFILE=/var/run/redis_${REDISPORT[i]}.pid # 进程守护文件
CONF=/usr/local/redis_cluster/${REDISPORT[i]}/redis.conf # redis节点配置文件
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis cluster server ${REDISPORT[i]} ..."
#不输出启动信息:nohup $EXEC $CONF > /dev/null 2>&1 &
$EXEC $CONF &&
sleep 1
if [ -f $PIDFILE ]
then
echo "Redis cluster ${REDISPORT[i]} startup succeeded!"
else
echo "ERROR: Redis cluster ${REDISPORT[i]} startup failed!"
fi
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping Redis cluster ${REDISPORT[i]} ..."
# 如果redis集群未设置密码,关闭指令改为:$CLIEXEC -p ${REDISPORT[i]} shutdown
$CLIEXEC -p ${REDISPORT[i]} -a $PASSWD shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis cluster ${REDISPORT[i]} to shutdown ..."
sleep 1
done
echo "Redis cluster ${REDISPORT[i]} stopped!"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
done
2. Centos 6.x & Redhat 6.x环境
将 redis-cluster 文件放到 /etc/init.d/ 下,并赋予执行权限:
chmod +x /etc/init.d/redis-cluster
相关命令:
chkconfig --add redis-cluster # 注册为系统服务
chkconfig redis-cluster on # 开机自启动
service redis-cluster stop # 关闭 redis 集群
service redis-cluster start # 启动 redis 集群
3. Centos 7.x & Redhat 7.x环境
将 redis-cluster 文件放到 /etc/rc.d/init.d/下,并赋予执行权限:
chmod +x /etc/rc.d/init.d/redis-cluster
相关命令:
chkconfig --add redis-cluster # 注册为系统服务
chkconfig redis-cluster on # 开机自启动
service redis-cluster stop # 关闭 redis 集群
service redis-cluster start # 启动 redis 集群