#!/usr/bin/env bash set -e #定义下载路径: SRC='/usr/local/src' #定义redis 目录: REDISDIR='/usr/local/redis' #定义redis 密码: REDISPAS="$(openssl rand -base64 32)" && echo ${REDISPAS} > /root/.REDIS_PAS #从官网下载redis 源码包: wget -O ${SRC}/redis-4.0.1.tar.gz http://download.redis.io/releases/redis-4.0.1.tar.gz #解压redis: tar zxf ${SRC}/redis-4.0.1.tar.gz -C ${SRC} #移动解压目录并重命名: mv ${SRC}/redis-4.0.1 ${REDISDIR} #进入redis 目录及创建目录: cd ${REDISDIR} && mkdir {bin,etc,var} make -j $(awk '/processor/{i++}END{print i}' /proc/cpuinfo) #复制可执行文件到创建的 bin 目录: cp -a ${REDISDIR}/src/redis-{benchmark,check-aof,check-rdb,cli,sentinel,server} ${REDISDIR}/bin #复制redis 主配置文件到创建的 etc 目录: cp -a ${REDISDIR}/redis.conf ${REDISDIR}/etc/ #替换配置,采用守护进程的方式启动redis sed -i 's/^daemonize no/daemonize yes/' ${REDISDIR}/etc/redis.conf #替换redis 的日志目录: sed -i "s#^logfile ""#logfile "${REDISDIR}/var/redis_6379.log"#" ${REDISDIR}/etc/redis.conf #替换redis 的 pid 文件路径: sed -i "s#^pidfile /var/run/redis_6379.pid#pidfile ${REDISDIR}/var/redis_6379.pid#" ${REDISDIR}/etc/redis.conf #替换redis 最大连接数: sed -i "s@^# maxclients 10000@maxclients 100@" ${REDISDIR}/etc/redis.conf #设置redis 密码: #sed -i "s@^# requirepass foobared@requirepass ${REDISPAS}@" ${REDISDIR}/etc/redis.conf #启用redis 监听socket: sed -i "s@^# unixsocket /tmp/redis.sock@unixsocket ${REDISDIR}/var/redis_6379.sock@" ${REDISDIR}/etc/redis.conf #设置 redis socket 文件的权限: sed -i "s/^# unixsocketperm 700/unixsocketperm 777/" ${REDISDIR}/etc/redis.conf #创建redis 启动脚本文件: echo '[Unit]' > /usr/lib/systemd/system/redis.service echo 'Description=Redis persistent key-value database' >> /usr/lib/systemd/system/redis.service echo 'After=network.target' >> /usr/lib/systemd/system/redis.service echo '' >> /usr/lib/systemd/system/redis.service echo '[Service]' >> /usr/lib/systemd/system/redis.service echo 'Type=forking' >> /usr/lib/systemd/system/redis.service echo "ExecStart=${REDISDIR}/bin/redis-server ${REDISDIR}/etc/redis.conf" >> /usr/lib/systemd/system/redis.service echo 'Restart=always' >> /usr/lib/systemd/system/redis.service echo '' >> /usr/lib/systemd/system/redis.service echo '[Install]' >> /usr/lib/systemd/system/redis.service echo 'WantedBy=multi-user.target' >> /usr/lib/systemd/system/redis.service #启动redis并设置成随机启动: systemctl start redis && systemctl enable redis #删除安装脚本 rm -rf $0