redis自动化安装

redis 自动化安装

#/bin/bin/ksh

LOGFILE=/var/log/redis.log
REDISSOURCE=/mnt/hgfs/Redis/
INSTALLDIR=/data
REDISDIR=/data/redis
VERISON=6.0.9
PORT=8000
RC=8

redislog()
{
    printf `date --date=today +"%Y-%M-%d %H:%m:%S"` >>${LOGFILE}  2 >&1
}

checkredis()
{
if [ `ps -ef|grep -i redis|grep -v grep|wc -l` -eq 1 ];then
   printf "redis already installed"
   exit
fi
}


version_select()
{
redislog
cd /tmp
printf "you can select following version:\n" 
printf "please input verson like 5.0.6\n\n"
printf "===============================================================================================\n"
echo -e "\e[1;32m 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 \e[0m"
echo -e "\e[1;32m 5.0.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.0.10 \e[0m"
echo -e "\e[1;32m 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 6.0.8 6.0.9 6.0.10 \e[0m"
printf "===============================================================================================\n"
read -p "Which version do you want to insatll:  " version
printf "your are going to download redis-${version}.tar.gz \n\n"
redisdownload
}

redisdownload()
{   
    cd /tmp
    wget http://download.redis.io/releases/redis-${version}.tar.gz      
    if [ $? -eq 0 ];then
        printf "download redis-${version}.tar.gz successfully"  
    else
        printf "download redis-${version}.tar.gz failed"        
        printf "wait 5 seconds and try again.."                 
        sleep 5
        redisdownload
    fi
}

#创建用户
craete_user()
{
redisid=$(cat /etc/passwd|grep -i redis|wc -l)
if [ $redisid -eq 1 ];then
  printf "user redis already exist" |tee -a >> ${LOGFILE}
else
  groupadd redis                  >> ${LOGFILE}
  useradd -g redis redis          >> ${LOGFILE}
  if [ $? -eq 0 ];then
  printf "user redis is created"   |tee -a   >> ${LOGFILE}
  fi
fi
}

#安装环境
prepqreenv()
{

##redis path
if ! [ -d ${REDISDIR} ] ; then
    rm -rf ${REDISDIR}
    mkdir -p ${REDISDIR}
    printf " folder /data/redis is created" |tee -a >> ${LOGFILE}
fi

#sysctl
if ! [ -e /etc/sysctl.d/98-redis.conf ]; then
cat >> /etc/sysctl.d/98-redis.conf <<EOF
net.core.somaxconn = 2048
vm.overcommit_memory = 1
EOF
sysctl -p                                 |tee -a  >> ${LOGFILE}
fi

#disable Transparent Huge Pages (THP) support 
thg=$(cat  /sys/kernel/mm/transparent_hugepage/enabled | grep -i enver |wc -l)
if [ ${thg} -ne 1 ] ; then
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
    cat /sys/kernel/mm/transparent_hugepage/enabled|grep -i 'never' >>/etc/rc.local
fi
        
if ! [ $( cat /etc/security/limits.conf  | grep "redis" | wc -l )  -lt 1 ] ;then
cat >>/etc/security/limits.conf << EOF
redis   soft    nproc   65535
redis   hard    nproc   65535
redis   soft    nofile  65535
redis   hard    nofile  65535
EOF
fi
}

installredis()
{
#解压安装包
tar -zxvf /tmp/redis-${VERISON}.tar.gz    -C /data |tee -a >> ${LOGFILE}
if [ $? -eq 0 ]; then
  printf "Redis version ${VERISON} extract in to ${INSTALLDIR} done! "  |tee -a  >> ${LOGFILE}
fi
#ln -s  ${INSTALLDIR}/redis-${VERISON} ${INSTALLDIR}/redis

#编译
cd ${INSTALLDIR}/redis-${VERISON}
printf "start make"                                   >> ${LOGFILE}
make                                                >> ${LOGFILE}
if [ $? -eq 0 ];then
  printf "make done! "                                >> ${LOGFILE}
else
  printf "make failed with $RC,pls check reasib! "    >> ${LOGFILE}
  exit
fi

cd ${INSTALLDIR}/redis-${VERISON}/src
make test              >> ${LOGFILE}
if [ $? -lt 4 ];then
  printf "make test done! "    >> ${LOGFILE}
else
  printf "make test failed with ${RC},pls check reasib! "    >> ${LOGFILE}
  exit
fi

make install PREFIX=${INSTALLDIR}/redis            >> ${LOGFILE}
if [ $? -eq 0 ];then
  printf "make install done!\n "    >> ${LOGFILE}
else
  printf "make install failed with $?,pls check reason! "    >> ${LOGFILE}
  exit
fi

if ! [ -d ${INSTALLDIR}/redis/8000 ]
then
    mkdir -p  ${INSTALLDIR}/redis/8000/{data,tmp,logs}
fi

if ! [ -d ${INSTALLDIR}/redis/etc ]
then
    mkdir -p  ${INSTALLDIR}/redis/etc/
fi
}

#修改配置文件
redis_inist()
{
cat >> ${INSTALLDIR}/redis/etc/redis.conf <<EOF
bind 0.0.0.0
port 8000
daemonize yes
supervised systemd 
pidfile "/data/redis/8000/tmp/redis-8000.pid"
logfile "/data/redis/8000/logs/redis-8000.log"
dir "/data/redis/8000/data"
requirepass "123456"
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
loglevel notice
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass foobared
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
oom-score-adj no
oom-score-adj-values 0 200 800
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
EOF

chown -R  redis:redis ${REDISDIR}
chmod -R  755 ${REDISDIR}

if [ -f /etc/profile.d/redis ] ; then
    rm -rf /etc/profile.d/redis
    echo "PATH=$PATH:${INSTALLDIR}/redis/bin" >> /etc/profile.d/redis
    chmod +x /etc/profile.d/redis
    source /etc/profile.d/redis
fi
#redis-server /data/redis/etc/redis.conf &
}


redis_start()
{
REDISDIR=/data/redis
cp -p ${REDISDIR}/bin/redis-server /usr/bin
cp -p ${REDISDIR}/bin/redis-cli /usr/bin

if [ -f /usr/lib/systemd/system/redis.service ] ;then
    rm -rf /usr/lib/systemd/system/redis.service
fi


cat >> /usr/lib/systemd/system/redis.service << EOF [Unit] Description=Redis persistent key-value database Documentation=https://redis.io/documentation After=network.target [Service] #ExecStart=/usr/bin/redis-server /data/redis/etc/redis.conf --supervised systemd ExecStart=/usr/bin/redis-server /data/redis/etc/redis.conf ExecStop=/usr/bin/redis-cli shutdown Type=forking Restart=always LimitNOFILE=10032 User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 TimeoutSec=900 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable redis sleep 5 systemctl start redis if [ $? -eq 0 ];then redislog printf "redis is startup\n" >>${LOGFILE} 2 >&1 fi } main() { checkredis version_select craete_user prepqreenv installredis redis_inist redis_start } main

 

posted on 2021-01-20 23:38  InnoLeo  阅读(191)  评论(0编辑  收藏  举报