环境,Centos7+redis6.2.16,比较新,这个版本修改了一个严重的安全问题。默认下载包路径/tmp/soft,删除的目录默认在、/tmp/data/目录下。
指定三个参数,第一个port端口号,第二个redis的密码,为安全必须设置,默认为xxxxx,第三个是redis版本。
脚本自动检查环境,用户,安装目录,当前端口是否占用,安装包。依赖包等,并安装配置。
#redis 6.0 开始依赖gcc 5+ ,因此如果linux系统是4.8则,需要升级gcc .
#https://mirrors.aliyun.com/gnu/gcc/, 参考文档:https://blog.csdn.net/qq_41054313/article/details/119453611
#configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
#gcc的依赖:(这里版本固定10.5.0) 脚本自动判断gcc版本,并编译安装,这个过程很 漫长。需耐心等待。
编译完成,配置systemd 管理服务。方便管理。使用systemctl daemon-reload 加载配置。
启动:systemctl start redis$PORT
停止:systemctl stop redis$PORT
查看状态,检查错误信息:systemctl status redis$PORT redis自己的错误日志位于/data/redis$PORT目录下/redis.log
#!/bin/bash
#Author:lzj
#Date:2024-08-15
#Description:redis 6.0、7.0 自动化部署脚本(6.2.8,6.2.10,6.2.12,6.2.14,7.0.14,),注意linux命令可能需要完整路径。
#yum源
#wget https://github.com/redis/redis/archive/refs/tags/6.2.16.tar.gz
#防火墙
#systemctl stop firewalld
#systemctl disable firewalld
#文件描述符号
#cat >> /etc/security/limits.conf << EOF
#redis soft nproc 16384
#redis hard nproc 16384
#redis soft nofile 65536
#redis hard nofile 65536
#EOF
PORT=$1
PASSWD=$2
VERSION=$3
VERSION=${VERSION:='6.2.16'}
PORT=${PORT:=6379}
#默认需要密码,设置密码。默认xxxxx
PASSWD=${PASSWD:='xxxxx'}
URL="https://github.com/redis/redis/archive/refs/tags/"
FILE_NAME="redis-${VERSION}"
TMP="/tmp/soft"
REDIS_BASE=/data/redis$PORT
#日志输出,output打印输出,log输出日志文件,all两种都输出
LOG_TYPE=all
MESSAGE_LOG=$TMP/redis_message.log
function log_info()
{
local msg=$1
local dt=`/usr/bin/date '+%Y%m%d %H:%M:%S'`
case $LOG_TYPE in
output)
echo "$dt [info] $msg" ;;
log)
echo "$dt [info] $msg" &>> $MESSAGE_LOG ;;
all)
echo "$dt [info] $msg"
echo "$dt [info] $msg" &>> $MESSAGE_LOG ;;
*)
esac
}
function log_error()
{
local msg=$1
local dt=`/usr/bin/date '+%Y%m%d %H:%M:%S'`
case $LOG_TYPE in
output)
echo "$dt [error] $msg" ;;
log)
echo "$dt [error] $msg" &>> $MESSAGE_LOG ;;
all)
echo "$dt [error] $msg"
echo "$dt [error] $msg" &>> $MESSAGE_LOG ;;
*)
esac
}
function check_env()
{
#目录
[ ! -d "$TMP" ] && mkdir -p $TMP
#检查系统
log_info " "
grep CentOS /etc/redhat-release &>/dev/null
[ "$?" -ne 0 ] && { log_error 'Only suppert Centos.';exit 1;}
#执行本脚本必须是root用户
[ $UID -ne 0 ] && { log_error "Please switch root user and try again."; exit 10;}
#检查用户
/usr/sbin/groupadd redis &> /dev/null
/usr/bin/id redis &> /dev/null
if [ "$?" -eq 0 ];then
/usr/sbin/usermod -s /sbin/nologin redis &> /dev/null && log_info "Redis user already exists."
else
/usr/sbin/useradd -g redis redis -s /sbin/nologin &> /dev/null && log_info "Redis user created successfully."
fi
#校验redis实例
redis_status=`netstat -nltp | grep 'redis' | grep -w $PORT | wc -l`
[ "$redis_status" = 1 ] && { log_error "Redis instance exists. please check it."; exit 18;}
#检查数据库目录
if [ -d ${REDIS_BASE} -a "`ls -A ${REDIS_BASE}`" != "" ];then
read -r -p "${REDIS_BASE} exists,do you want to delete them? [Y/N]:" input
case $input in
[yY][eE][sS]|[yY])
mkdir -p /tmp/data &> /dev/null
mv ${REDIS_BASE} /tmp${REDIS_BASE}-`date '+%Y%m%d%H%M%S'` && log_info "delete ${REDIS_BASE} succeed." ;;
[nN][oO]|[nN])
log_error "Please delete ${REDIS_BASE} manually." && exit 11 ;;
*)
log_error "Input error. " && exit 12 ;;
esac
fi
#检查安装包
[ -e "${TMP}/${FILE_NAME}.tar.gz" ] && log_info "REDIS package exists.:`ls ${TMP}/${FILE_NAME}*`" || DOWNLOAD=1
#selinux 和 防火墙
sed -i -e "s:SELINUX=.*:SELINUX=permissive:g" /etc/selinux/config
setenforce 0 &> /dev/null && log_info "SELinux is disabled"
#安装依赖包
yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel make libstdc++-devel tcl openssl openssl-devel bzip2 &> /dev/null
if [ "$?" -eq 0 ];then
log_info "The redis dependency package is successfully installed."
else
log_error "The redis dependency package install failed."
read -r -p "Whether to continue? [Y/N]:" input
case $input in
[yY][eE][sS]|[yY])
log_info "continue check env." ;;
[nN][oO]|[nN])
log_error "check exit." && exit 16 ;;
*)
log_error "Input error. " && exit 17 ;;
esac
fi
#切换为阿里yum源
#
#wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#yum makecache
}
function upgrade_gcc()
{
#redis 6.0 开始依赖gcc 5+ ,因此如果linux系统是4.8则,需要升级gcc .
#https://mirrors.aliyun.com/gnu/gcc/, 参考文档:https://blog.csdn.net/qq_41054313/article/details/119453611
#configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
#gcc的依赖:(这里版本固定)
DEST_GCC_VER="10.5.0"
#判断是否已经安装gcc 5+以上。
gcc_ver=`/usr/bin/gcc -dumpversion`
if [ `echo ${gcc_ver} | cut -d '.' -f 1` -gt 5 ];then
log_info "gcc version:${gcc_ver} ."
else
yum -y install bzip2 glibc-headers gcc-c++
log_info "Download source code..."
if [ ! -e "${TMP}/gcc-${DEST_GCC_VER}.tar.gz" ];then
wget -O ${TMP}/gcc-${DEST_GCC_VER}.tar.gz https://mirrors.aliyun.com/gnu/gcc/gcc-${DEST_GCC_VER}/gcc-${DEST_GCC_VER}.tar.gz
[ "$?" -eq 0 ] && { log_info "Download source code gcc-${DEST_GCC_VER} completed.";} || { log_error "Download source code gcc-${DEST_GCC_VER} failed."; exit 50;}
fi
#配置GCC
log_info "GCC compilation and installation in progress..."
cd ${TMP}
tar -xf gcc-${DEST_GCC_VER}.tar.gz && cd gcc-${DEST_GCC_VER}
./contrib/download_prerequisites
[ "$?" -eq 0 ] && { log_info "GCC dependency installation completed.";} || { log_error "GCC dependency installation failed."; exit 57;}
mkdir build && cd build/
../configure -prefix=/usr/local/ -enable-checking=release -enable-languages=c,c++ -disable-multilib
make && log_info "make......The compilation time is very long, please wait."
yum -y remove gcc g++
log_info "make install......"
make install
[ "$?" -eq 0 ] && { log_info "GCC ${DEST_GCC_VER} installation completed.";} || { log_error "GCC ${DEST_GCC_VER} installation failed."; exit 58;}
#检查版本
install_gcc=`/usr/local/bin/gcc -dumpversion`
log_info "gcc install version is ${install_gcc}."
#环境配置
rm -rf /usr/bin/{gcc,g++,c++,cc}
ln -s /usr/local/bin/gcc /usr/bin/gcc
ln -s /usr/local/bin/g++ /usr/bin/g++
ln -s /usr/local/bin/c++ /usr/bin/c++
ln -s /usr/local/bin/gcc /usr/bin/cc
#检查动态库:strings /usr/lib64/libstdc++.so.6 | grep CXXABI
rm -rf /usr/lib64/libstdc++.so.6
ln -s /usr/local/lib64/libstdc++.so.6.0.28 /usr/lib64/libstdc++.so.6
#环境版本检查
gcc_version=`/usr/local/bin/gcc -dumpversion`
[ "$gcc_version" -eq "$DEST_GCC_VER" ] && log_info "The current system gcc version is $gcc_version." || { log_info "The current system gcc version is $gcc_version." ;exit 59;}
fi
}
function redis_conf()
{
listen_ip=`ifconfig eth0 | awk '/inet / {print $2}'`
/usr/bin/cat <<EOF > ${REDIS_BASE}/redis.conf
bind $listen_ip 127.0.0.1
port ${PORT}
protected-mode no
daemonize yes
requirepass ${PASSWD}
pidfile ${REDIS_BASE}/redis_${PORT}.pid
logfile "${REDIS_BASE}/redis.log"
save 900 1
save 300 10
save 60 10000
dir ${REDIS_BASE}
dbfilename dump.rdb
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
hz 10
#危险命令禁用
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""
rename-command KEYS ""
rename-command SHUTDOWN ""
#rename-command DEL ""
rename-command EVAL ""
EOF
}
function redis_install()
{
#判断下载安装包
if [ $DOWNLOAD ]; then
log_info "Download REDIS package."
wget -O ${TMP}/${FILE_NAME}.tar.gz "${URL}${VERSION}.tar.gz" && log_info "Successfully downloaded REDIS package ." || { log_error "Cannot connect to the target address.";exit 20;}
fi
#判断是否存在解压文件
log_info "Extract and deploy the installation files."
if [ -e ${TMP}/${FILE_NAME} ];then
mv ${TMP}/${FILE_NAME} ${TMP}/${FILE_NAME}-`date '+%Y%m%d%H%M%S'` &> /dev/null
fi
tar -xf "${TMP}/${FILE_NAME}.tar.gz" -C $TMP/ &> /dev/null || { log_error "Extract redis package failed.";exit 21; }
mv "${TMP}/${FILE_NAME}" ${REDIS_BASE} &> /dev/null && log_info "deploy installation files successfully." || { log_error "deploy installation files failed."; exit 23;}
#编译
cd $REDIS_BASE/deps && log_info "REDIS compilation begins...."
/usr/bin/make distclean && /usr/bin/make lua hiredis linenoise jemalloc geohash-int
log_info " "
log_info "deps completed."
log_info " "
#SSL支持
cd $REDIS_BASE
#/usr/bin/make distclean && /usr/bin/make BUILD_TLS=yes
/usr/bin/make BUILD_TLS=yes && /usr/bin/make install
/usr/bin/make test
[ "$?" -eq 0 ] && log_info "REDIS compilation completed." || { log_error "REDIS compilation failed."; exit 24;}
#调用配置文件
/usr/bin/mv ${REDIS_BASE}/redis.conf ${REDIS_BASE}/redis.confbak
redis_conf
#命令、权限
/usr/bin/mkdir -p $REDIS_BASE/bin
ln -s $REDIS_BASE/src/redis-benchmark $REDIS_BASE/bin/redis-benchmark
ln -s $REDIS_BASE/src/redis-check-aof $REDIS_BASE/bin/redis-check-aof
ln -s $REDIS_BASE/src/redis-check-rdb $REDIS_BASE/bin/redis-check-rdb
ln -s $REDIS_BASE/src/redis-cli $REDIS_BASE/bin/redis-cli
ln -s $REDIS_BASE/src/redis-server $REDIS_BASE/bin/redis-server
/usr/bin/chown -R redis.redis $REDIS_BASE
/usr/bin/chmod 600 ${REDIS_BASE}/redis.conf
#环境变量,先过滤掉空行和注释行,再判断
is_env=`/usr/bin/grep -vE '^#|^$' /etc/profile | /usr/bin/grep "${REDIS_BASE}/bin:" | /usr/bin/wc -l` &> /dev/null
if [ $is_env -gt 0 ];then
log_info "env is exists."
else
echo "export REDIS_HOME=${REDIS_BASE}" >> /etc/profile
echo "export PATH=$REDIS_HOME/bin:\$PATH" >> /etc/profile
source /etc/profile
export "REDIS_HOME=${REDIS_BASE}"
export "PATH=$REDIS_HOME/bin:\$PATH"
log_info "env add succeed."
fi
#处理redis 相关
echo 512 > /proc/sys/net/core/somaxconn
echo never > /sys/kernel/mm/transparent_hugepage/enabled
/usr/sbin/sysctl vm.overcommit_memory=1
/usr/bin/chmod +x /etc/rc.d/rc.local
is_hugepage=`/usr/bin/grep -vE '^#|^$' /etc/rc.local | /usr/bin/grep "/sys/kernel/mm/transparent_hugepage/enabled" | /usr/bin/wc -l` &> /dev/null
[ $is_hugepage -eq 0 ] && echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
is_somaxconn=`/usr/bin/grep -vE '^#|^$' /etc/rc.local | /usr/bin/grep "/proc/sys/net/core/somaxconn" | /usr/bin/wc -l` &> /dev/null
[ $is_somaxconn -eq 0 ] && echo "echo 512 > /proc/sys/net/core/somaxconn" >> /etc/rc.local
is_overcommit=`/usr/bin/grep -vE '^#|^$' /etc/sysctl.conf | /usr/bin/grep "vm.overcommit_memory = 1" | /usr/bin/wc -l` &> /dev/null
[ $is_overcommit -eq 0 ] && echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
#添加systemed服务,非root用户启动
/usr/bin/cat <<EOF > /usr/lib/systemd/system/redis$PORT.service
[unit]
Description=Redis Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=redis
Group=redis
Type=simple
RuntimeDirectory=${REDIS_BASE}
RuntimeDirectoryMode=0755
ExecStart=${REDIS_BASE}/bin/redis-server ${REDIS_BASE}/redis.conf
ExecReload=${REDIS_BASE}/bin/redis-server -s reload
ExecStop=${REDIS_BASE}/bin/redis-server -s stop
Environment="TZ=Asia/shanghai"
LimitNOFILE=100000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=true
EOF
/usr/bin/systemctl daemon-reload
/usr/bin/systemctl start redis$PORT
/usr/bin/sleep 15s
#校验redis实例是否启动成功
redis_status=`/usr/bin/netstat -nltp | /usr/bin/grep 'redis' | /usr/bin/grep -w $PORT | /usr/bin/wc -l`
if [ "$redis_status" = 1 ];then
log_info "Redis start succeed."
#初始化设置密码
else
log_error "Redis start failed."
fi
}
function main(){
check_env
upgrade_gcc
redis_install
}
main
标签:脚本,bin,gcc,log,REDIS,Redis6,redis,usr,自动化 From: https://www.cnblogs.com/rcsy/p/18542628