1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #!/bin/bash # #************************************ #Author: 白展躺 #Date: 2023-06-24 #FileName: installredis.sh #Description: 说明 #Copyright: 2023 #************************************ DIR=/apps/redis REDIS_VERSION=7.0.11 PORT=$1 IP=$2 function install(){ tar xf /usr/local/src/redis-${REDIS_VERSION}.tar.gz -C /usr/src/ cd /usr/src/${REDIS_VERSION}/ make PREFIX=$DIR install useradd -M -s /bin/nologin redis mkdir $DIR/{etc,log,data} -p chown redis:redis ${DIR} -R
} function installdb(){ if [ -z $PORT ];then echo '端口不能为空' exit 1 fi cp /usr/src/redis-${REDIS_VERSION}/redis.conf ${DIR}/etc/redis_${PORT}.conf cp /usr/src/redis-${REDIS_VERSION}/sentinel.conf ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak 's/^(bind).*/\1 * -::1/g' ${DIR}/etc/redis_${PORT}.conf sed -Ei.bak 's/^(protected-mode).*/\1 no/g' ${DIR}/etc/redis_${PORT}.conf sed -Ei.bak "s@^(port).*@\1 ${PORT}@g" ${DIR}/etc/redis_${PORT}.conf sed -Ei.bak "s@^(pidfile).*@\1 ${DIR}/redis_${PORT}.pid@g" ${DIR}/etc/redis_${PORT}.conf sed -Ei.bak "s@^(logfile).*@\1 ${DIR}/log/message${PORT}.log@g" ${DIR}/etc/redis_${PORT}.conf #sed -Ei.bak "s@^(dir).*@\1 /data/redis${PORT}@g" ${DIR}/etc/redis_${PORT}.conf sed -Ei.bak "s/^(dbfilename).*/\1 dump${PORT}.rdb/g" ${DIR}/etc/redis_${PORT}.conf sed -Ei.bak "s/^(appendonly).*/\1 yes/g" ${DIR}/etc/redis_${PORT}.conf sed -Ei.bak "s/^(appendfilename).*/\1 \"appendonly${PORT}.aof\"/g" ${DIR}/etc/redis_${PORT}.conf sed -Ei.bak "s@^(dir).*@\1 /data/redis@g" ${DIR}/etc/redis_${PORT}.conf sed -Ei.bak "/^protected-mode no/a\bind 0.0.0.0" ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak "s@^(port).*@\1 2${PORT}@g" ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak "s@^(daemonize).*@\1 yes@g" ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak "s@^(pidfile).*@\1 /apps/redis/redis-sentinel_${PORT}.pid@g" ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak "s@^(logfile).*@\1 /apps/redis/log/sentinel_${PORT}.log@g" ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak "s@^(sentinel monitor).*@\1 rediscluster ${IP} ${PORT} 2@g" ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak "s@^(sentinel down-after-milliseconds).*@\1 rediscluster 3000@g" ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak "s@^(sentinel parallel-syncs).*@\1 rediscluster 1@g" ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak "s@^(sentinel failover-timeout).*@\1 rediscluster 180000@g" ${DIR}/etc/sentinel_${PORT}.conf sed -Ei.bak "s@^(SENTINEL master-reboot-down-after-period).*@\1 rediscluster 0@g" ${DIR}/etc/sentinel_${PORT}.conf #mkdir /data/redis${PORT} -p #chown redis:redis /data/redis${PORT} cat > /etc/systemd/system/redis${PORT}.service <<EOF [Unit] Description=Redis In-Memory Data Store After=network.target
[Service] User=redis Group=redis ExecStart=${DIR}/bin/redis-server $DIR/etc/redis_${PORT}.conf --supervised systemd ExecStop=${DIR}/bin/redis-cli shutdown Restart=always
[Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl start redis${PORT} }
installdb
|