2018-02-22 09:48:39 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2019-08-19 19:19:57 +08:00
|
|
|
###################################################################################
|
|
|
|
# 控制台颜色
|
|
|
|
BLACK="\033[1;30m"
|
|
|
|
RED="\033[1;31m"
|
|
|
|
GREEN="\033[1;32m"
|
|
|
|
YELLOW="\033[1;33m"
|
|
|
|
BLUE="\033[1;34m"
|
|
|
|
PURPLE="\033[1;35m"
|
|
|
|
CYAN="\033[1;36m"
|
|
|
|
RESET="$(tput sgr0)"
|
|
|
|
###################################################################################
|
|
|
|
|
|
|
|
printf "${BLUE}"
|
2019-05-07 11:08:13 +08:00
|
|
|
cat << EOF
|
2019-05-07 12:07:59 +08:00
|
|
|
|
2018-02-24 14:16:40 +08:00
|
|
|
###################################################################################
|
|
|
|
# 安装 Redis 脚本
|
2019-05-07 14:29:16 +08:00
|
|
|
# @system: 适用于 CentOS
|
|
|
|
# @author: Zhang Peng
|
2018-02-24 14:16:40 +08:00
|
|
|
###################################################################################
|
2019-05-07 12:07:59 +08:00
|
|
|
|
2019-05-07 11:08:13 +08:00
|
|
|
EOF
|
2019-08-19 19:19:57 +08:00
|
|
|
printf "${RESET}"
|
2018-02-22 09:48:39 +08:00
|
|
|
|
2019-07-12 10:17:32 +08:00
|
|
|
command -v yum >/dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n"; exit 1; }
|
2019-05-09 11:26:06 +08:00
|
|
|
|
|
|
|
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]] || [[ $# -lt 3 ]] || [[ $# -lt 4 ]];then
|
|
|
|
echo "Usage: sh redis-install.sh [version] [path] [port] [password]"
|
|
|
|
echo -e "Example: sh redis-install.sh 5.0.4 /opt/redis 6379 123456\n"
|
|
|
|
fi
|
|
|
|
|
2019-05-07 11:08:13 +08:00
|
|
|
version=5.0.4
|
|
|
|
if [[ -n $1 ]]; then
|
|
|
|
version=$1
|
|
|
|
fi
|
2018-02-22 09:48:39 +08:00
|
|
|
|
2018-09-29 10:16:24 +08:00
|
|
|
root=/opt/redis
|
2019-05-07 11:08:13 +08:00
|
|
|
if [[ -n $2 ]]; then
|
|
|
|
root=$2
|
|
|
|
fi
|
|
|
|
|
2019-05-08 17:13:04 +08:00
|
|
|
port=6379
|
|
|
|
if [[ -n $3 ]]; then
|
|
|
|
port=$3
|
|
|
|
fi
|
|
|
|
|
2019-07-11 16:52:03 +08:00
|
|
|
password=
|
2019-05-08 17:13:04 +08:00
|
|
|
if [[ -n $4 ]]; then
|
2019-07-11 16:52:03 +08:00
|
|
|
password=$4
|
2019-05-08 17:13:04 +08:00
|
|
|
fi
|
|
|
|
|
2019-08-19 19:19:57 +08:00
|
|
|
printf "${GREEN}>>>>>>>> install redis begin.${RESET}\n"
|
|
|
|
|
|
|
|
printf "\t${GREEN}Current execution: install redis ${version} to ${root}, service port = ${port}, password = ${password}${RESET}\n"
|
2019-05-07 11:08:13 +08:00
|
|
|
yum install -y zlib zlib-devel gcc-c++ libtool openssl openssl-devel tcl
|
|
|
|
|
2018-02-24 14:16:40 +08:00
|
|
|
mkdir -p ${root}
|
2019-07-12 15:53:54 +08:00
|
|
|
curl -o ${root}/redis-${version}.tar.gz http://download.redis.io/releases/redis-${version}.tar.gz
|
2018-02-24 14:16:40 +08:00
|
|
|
|
2019-05-08 17:13:04 +08:00
|
|
|
path=${root}/redis-${version}
|
2019-05-07 15:04:19 +08:00
|
|
|
tar zxf ${root}/redis-${version}.tar.gz -C ${root}
|
2019-05-08 17:13:04 +08:00
|
|
|
cd ${path}
|
2019-05-07 15:39:11 +08:00
|
|
|
make && make install
|
2019-05-07 11:08:13 +08:00
|
|
|
cd -
|
2019-05-08 17:13:04 +08:00
|
|
|
|
2019-08-19 19:19:57 +08:00
|
|
|
printf "\n${CYAN}>>>>>>>>> config redis${RESET}\n"
|
2019-05-08 17:13:04 +08:00
|
|
|
cp ${path}/redis.conf ${path}/redis.conf.default
|
2019-05-16 11:49:19 +08:00
|
|
|
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/config/redis-remote-access.conf -O ${path}/redis.conf
|
2019-08-19 19:19:57 +08:00
|
|
|
mkdir -p /etc/redis
|
2019-05-08 17:13:04 +08:00
|
|
|
cp ${path}/redis.conf /etc/redis/${port}.conf
|
|
|
|
sed -i "s/^port 6379/port ${port}/g" /etc/redis/${port}.conf
|
2019-08-19 19:19:57 +08:00
|
|
|
if [[ -n ${password} ]]; then
|
|
|
|
sed -i "s/^# requirepass/requirepass ${password}/g" /etc/redis/${port}.conf
|
|
|
|
fi
|
2019-05-08 17:13:04 +08:00
|
|
|
|
2019-08-19 19:19:57 +08:00
|
|
|
printf "\n${CYAN}>>>>>>>>> add firewall port${RESET}\n"
|
2019-05-08 17:13:04 +08:00
|
|
|
firewall-cmd --zone=public --add-port=${port}/tcp --permanent
|
|
|
|
firewall-cmd --reload
|
|
|
|
|
2019-08-19 19:19:57 +08:00
|
|
|
printf "\n${CYAN}>>>>>>>>> add redis service${RESET}\n"
|
2019-05-08 17:13:04 +08:00
|
|
|
# 注册 redis 服务,并设置开机自启动
|
2019-08-19 19:19:57 +08:00
|
|
|
cp ${path}/utils/redis_init_script /etc/init.d/
|
|
|
|
mv /etc/init.d/redis_init_script /etc/init.d/redis_${port}
|
2019-05-08 17:13:04 +08:00
|
|
|
sed -i "s/^REDISPORT=.*/REDISPORT=${port}/g" /etc/init.d/redis_${port}
|
|
|
|
chmod +x /etc/init.d/redis_${port}
|
|
|
|
chkconfig --add redis_${port}
|
|
|
|
service redis_${port} start
|
|
|
|
|
2019-08-19 19:19:57 +08:00
|
|
|
printf "\n${GREEN}<<<<<<<< install redis end.${RESET}\n"
|