redis-3.2.7 安装脚本 for CentOS 6.5
#!/bin/bash
#Filename: inst_for_redis.sh
#Version: 1.1
#Lastdate: 2017/07/25
#Author: wangshenjin<wangshenjin233@foxmail.com>
#Description: auto install redis
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
VERSION="3.2.7"
PORT="6001"
BASEDIR="/usr/local/redis"
DATADIR="/data/database/redis${PORT}"
LOGDIR="/data/logs/redis"
PIDDIR="/var/run/redis"
REQUIREPASS=`cat /dev/urandom | head -10 | md5sum | head -c8`
REDISUSER="redis"
Init_Env() {
yum -y install tcl-8.5* make gcc gcc-c++
[ "$?" -ne 0 ] && echo -e "\n [`date +%Y-%m-%d" "%H:%m:%S`] Install depend soft error.\n" && exit
[ -d "${DISTDIR}" ] || mkdir -p ${DISTDIR}
[ -d "${DATADIR}" ] || mkdir -p ${DATADIR}
[ -d "${LOGDIR}" ] || mkdir -p ${LOGDIR}
[ -d "${PIDDIR}" ] || mkdir -p ${PIDDIR}
}
Install_Redis() {
groupadd ${REDISUSER}
useradd -g ${REDISUSER} -s /sbin/nologin ${REDISUSER}
cd /dist/src
if [ ! -d "redis-${VERSION}" ] ; then
[ -f "/dist/dist/redis-${VERSION}.tar.gz" ] || wget http://download.redis.io/releases/redis-${VERSION}.tar.gz -O /dist/dist/redis-${VERSION}.tar.gz
tar xf /dist/dist/redis-${VERSION}.tar.gz
fi
cd redis-${VERSION}
make MALLOC=libc
[ "$?" -ne 0 ] && echo -e "\n [`date +%Y-%m-%d" "%H:%m:%S`] Make redis error.\n" && exit
mkdir ${BASEDIR}/{bin,etc} -p
mkdir ${DATADIR} -p
cp redis.conf ${BASEDIR}/etc/redis_default.conf
cd src
cp redis-server redis-cli redis-benchmark ${BASEDIR}/bin/
ln -s ${BASEDIR}/bin/* /usr/local/bin/
chown ${REDISUSER}:${REDISUSER} -R ${BASEDIR}
chown ${REDISUSER}:${REDISUSER} -R ${DATADIR}
chown ${REDISUSER}:${REDISUSER} -R ${LOGDIR}
chown ${REDISUSER}:${REDISUSER} -R ${PIDDIR}
cat > $BASEDIR/etc/redis${PORT}.conf << EOF
daemonize yes
pidfile ${PIDDIR}/redis${PORT}.pid
port ${PORT}
bind 127.0.0.1
timeout 300
loglevel notice
logfile ${LOGDIR}/redis${PORT}.log
syslog-enabled no
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename redis${PORT}.rdb
appendfilename redis${PORT}.aof
dir ${DATADIR}
appendonly yes
appendfsync everysec
requirepass $REQUIREPASS
no-appendfsync-on-rewrite no
set-max-intset-entries 512
activerehashing yes
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
EOF
}
Create_Scripts() {
cat > /root/redis_start << EOF
chown redis:redis -R ${LOGDIR}
su - ${REDISUSER} -s /bin/bash -c "${BASEDIR}/bin/redis-server ${BASEDIR}/etc/redis${PORT}.conf"
EOF
cat > /root/redis_stop << EOF
${BASEDIR}/bin/redis-cli -h 127.0.0.1 -p ${PORT} -a ${REQUIREPASS} save
sleep 5s
${BASEDIR}/bin/redis-cli -h 127.0.0.1 -p ${PORT} -a ${REQUIREPASS} shutdown
EOF
cat > /root/redis_login << EOF
${BASEDIR}/bin/redis-cli -h 127.0.0.1 -p ${PORT} -a ${REQUIREPASS}
EOF
chmod 700 /root/redis_*
if /root/redis_start ; then
echo -e "\n Help:"
echo -e "\n Base dir: ${BASEDIR}."
echo -e "\n Data dir: ${DATADIR}."
echo
else
echo -e "\n [`date +%Y-%m-%d" "%H:%m:%S`] Redis start error. \n"
fi
}
##################### Main ##########################
Init_Env
Install_Redis
Create_Scripts