Redis自动部署脚本编写

#!/bin/bash

ck_ok()

{

if [ $? -ne 0 ]

then

echo "$1 error."

exit 1

fi

}

download_redis()

{

cd /usr/local/src

if [ -f redis-7.0.4.tar.gz ]

then

echo "当前目录已经存在redis-7.0.4.tar.gz"

echo "检测MD5"

file_md5=`md5sum redis-7.0.4.tar.gz | awk '{print $1}'`

if [ ${file_md5} == '3a2ce76ef8f5ca3cc6463c487f2d532c' ]

then

return 0

else

echo "file redis-7.0.4.tar.gz md5 check failed"

/bin/mv redis-7.0.4.tar.gz redis-7.0.4.tar.gz.old

fi

fi

sudo curl -o redis-7.0.4.tar.gz https://codeload.github.com/redis/redis/tar.gz/refs/tags/7.0.4

ck_ok " 下载redis"

}

install_redis()

{

cd /usr/local/src

if [ -d redis-7.0.4/ ]

then

rm -rf redis-7.0.4/

fi

sudo tar zxf redis-7.0.4.tar.gz

ck_ok "解压redis源码包"

cd redis-7.0.4/

sudo make && sudo make PREFIX=/usr/local/redis install

ck_ok "编译和安装redis"

sudo mkdir -p /usr/local/redis/{conf,log}

sudo mkdir -p /data/redis

if id redis &>/dev/null

then

echo "系统已经创建了redis用户,跳过创建"

else

echo "创建redis用户"

sudo useradd -s /sbin/nologin redis

fi

ck_ok "创建redis用户"

sudo chown -R redis /data/redis /usr/local/redis/log

}

config_redis()

{

echo "配置redis.conf"

sudo /bin/cp /usr/local/src/redis-7.0.4/redis.conf /usr/local/redis/conf/redis.conf

sudo sed -i 's/daemonize no/daemonize yes/' /usr/local/redis/conf/redis.conf

sudo sed -i 's@logfile ""@logfile "/usr/local/redis/log/redis.log"@' /usr/local/redis/conf/redis.conf

sudo sed -i 's@dir ./@dir /data/redis@' /usr/local/redis/conf/redis.conf

sudo sed -i 's@pidfile /var/run/redis_6379.pid@pidfile /usr/local/redis/log/redis_6379.pid@' /usr/local/redis/conf/redis.conf

sudo sed -i '/# requirepass foobared/a requirepass aminglinux.Com' /usr/local/redis/conf/redis.conf

echo "配置systemd service"

cat >/lib/systemd/system/redis.service <<EOF

Unit

Description=redis

After=network.target

Service

User=redis

Type=forking

TimeoutSec=0

PIDFile=/usr/local/redis/log/redis_6379.pid

ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

Install

WantedBy=multi-user.target

EOF

sudo mv /tmp/redis.service /lib/systemd/system/redis.service

echo "更新内核"

grep -q 'net.core.somaxconn = 2048' /etc/sysctl.conf || echo "net.core.somaxconn = 2048" || sudo tee -a /etc/sysctl.conf

grep -q 'vm.overcommit_memory = 1' /etc/sysctl.conf || echo "vm.overcommit_memory = 1" || sudo tee -a /etc/sysctl.conf

sudo sysctl -p

sudo systemctl daemon-reload

sudo systemctl enable redis

echo "启动redis服务"

sudo systemctl start redis

ck_ok "启动redis服务"

}

download_redis

install_redis

config_redis

相关推荐
闲人编程2 分钟前
Django测试框架深度使用:Factory Boy与Fixture对比
数据库·python·django·sqlite·钩子·fixture·codecapsule
以明志、4 分钟前
并行与并发
前端·数据库·c#
5***V9337 分钟前
SQL 基础 BETWEEN 的常见用法
数据库·sql·mybatis
万变不离其宗_81 小时前
centos 手动安装redis
linux·redis·centos
麦聪聊数据1 小时前
IT 的“控”与业务的“放”:构建基于 Web 原生架构的安全数据共享平台
数据库·sql·安全
rchmin2 小时前
MySQL分库分表适用场景与依据
数据库·mysql
MaisieKim_2 小时前
2025年企业文档管理系统全面评测报告
运维·数据库
f***6512 小时前
sql中COALESCE函数详解
数据库·sql
b***59432 小时前
LangChain-08 Query SQL DB 通过GPT自动查询SQL
数据库·sql·langchain
u***32432 小时前
【MySQL】数据库和表的操作
数据库·mysql·oracle