#!/bin/bash
#AUTHOR:ZHD
#DATE:2019-05-19
#FUNCTION:快速搭建REDIS,配置文件解析,准备集群环境。
#VERSION:1.0
#输出不同颜色的字体
function cecho {
echo -e "\033[$1m$2\033[0m"
}
#检查REDIS是否安装成功
function check_redis {
netstat -ntulp | grep redis-server &> /dev/null && cecho 32 "REDIS BUILD SUCCESSFULLY!!" || cecho 31 " REDIS BUILD FAIL,PLEASE CHECK IT!!"
}
#定义redis启动函数
function redis_start {
/etc/init.d/redis_6379 start
}
#定义redis停止函数
function redis_stop {
/etc/init.d/redis_6379 stop
}
#定义一键安装REDIS函数
function redis_install {
cecho 32 "系统正在装包,请稍等......."
yum -y install expect &> /dev/null
yum -y install gcc &> /dev/null
cd /root/soft/redis
tar -xf redis-4.0.8.tar.gz &> /dev/null
cd redis-4.0.8/
cecho 32 "源码编译安装过程中,这会需要一点时间,请稍等......"
make &> /dev/null && make install &> /dev/null
cd /root/soft/redis/redis-4.0.8/utils
expect <<EOF &> /dev/null
spawn ./install_server.sh
expect "]" {send "\r"}
expect "]" {send "\r"}
expect "]" {send "\r"}
expect "]" {send "\r"}
expect "]" {send "\r"}
expect "abort." {send "\r"}
expect "#" {send "exit\rr"}
EOF
}
redis_install
redis_start
check_redis
#配置文件解析
function file_resolution {
/etc/init.d/redis_6379 stop
read -p "请输入要更改的端口号:" port_number
read -p "请设置要更改的IP地址:" IP_add
read -p "是否开启redis集群服务?(yes/no)" yes
read -p "设置集群配置文件编号:" number
read -p "设置集群超时时间:" timess
if [ $yes = yes ];then
sed -i '815s/# cluster-enabled yes/cluster-enabled yes/' /etc/redis/6379.conf
else
exit 0
fi
sed -i "823s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-$number.conf/" /etc/redis/6379.conf
sed -i "829s/# cluster-node-timeout 15000/cluster-node-timeout $timess/" /etc/redis/6379.conf
sed -i "93s/port 6379/port $port_number/" /etc/redis/6379.conf
sed -i "70s/bind 127.0.0.1/bind $IP_add/" /etc/redis/6379.conf
sed -i "43c \$CLIEXEC -p $port_number -h $IP_add shutdown" /etc/init.d/redis_6379
/etc/init.d/redis_6379 start
}
file_resolution