首页 > 数据库 >一键部署redis脚本

一键部署redis脚本

时间:2022-10-28 21:31:50浏览次数:50  
标签:脚本 redis 一键 echo etc INSTALL REDIS DIR

#!/bin/bash
#

#REDIS_VERSION=redis-7.0.5
#REDIS_VERSION=redis-7.0.3
#REDIS_VERSION=redis-6.2.6
REDIS_VERSION=redis-5.0.14
#REDIS_VERSION=redis-4.0.14
REDIS_URL=http://download.redis.io/releases
PASSWORD=123456
INSTALL_DIR=/apps/redis


CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`
. /etc/os-release

color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}


prepare(){
if [ $ID = "centos" -o $ID = "rocky" ];then
yum -y install gcc make jemalloc-devel systemd-devel
else
apt update
apt -y install gcc make libjemalloc-dev libsystemd-dev
fi
if [ $? -eq 0 ];then
color "安装软件包成功" 0
else
color "安装软件包失败,请检查网络配置" 1
exit
fi
}
install() {
if [ ! -f ${REDIS_VERSION}.tar.gz ];then
wget ${REDIS_URL}/${REDIS_VERSION}.tar.gz || { color "Redis 源码下载失败" 1 ; exit; }
fi
tar xf ${REDIS_VERSION}.tar.gz -C /usr/local/src
cd /usr/local/src/${REDIS_VERSION}
make -j $CUPS USE_SYSTEMD=yes PREFIX=${INSTALL_DIR} install && color "Redis 编译安装完成" 0 || { color "Redis 编译安装失败" 1 ;exit ; }

ln -s ${INSTALL_DIR}/bin/redis-* /usr/bin/

mkdir -p ${INSTALL_DIR}/{etc,log,data,run}

cp redis.conf ${INSTALL_DIR}/etc/

sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir ${INSTALL_DIR}/data/" -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log" -e "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf


if id redis &> /dev/null ;then
color "Redis 用户已存在" 1
else
useradd -r -s /sbin/nologin redis
color "Redis 用户创建成功" 0
fi

chown -R redis.redis ${INSTALL_DIR}

cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
sysctl -p
if [ $ID = "centos" -o $ID = "rocky" ];then
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
/etc/rc.d/rc.local
else
echo -e '#!/bin/bash\necho never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
chmod +x /etc/rc.local
/etc/rc.local
fi


cat > /lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target

EOF
systemctl daemon-reload
systemctl enable --now redis &> /dev/null
if [ $? -eq 0 ];then
color "Redis 服务启动成功,Redis信息如下:" 0
else
color "Redis 启动失败" 1
exit
fi
sleep 2
redis-cli -a $PASSWORD INFO Server 2> /dev/null
}

prepare

install

标签:脚本,redis,一键,echo,etc,INSTALL,REDIS,DIR
From: https://blog.51cto.com/dayu/5805274

相关文章

  • IP 地址切换脚本
    @echooffcolor00modeconcols=80lines=30titleIP模式切换脚本:startclsecho=echo============================================================================ec......
  • Token+Redis登录认证
    首先需要了解一下大概的步骤登录生成一个Token存入Redis有效期为30分钟,返回到前端之后前端每次请求,带上登录时返回的Token服务器判断前端带来的Token是否在Redis服务......
  • redis常见问题(三)
    redis安装之后导致linux无法正常启动问题现象:在linux安装redis后,将redis设置为开机启动项,导致系统开机后停在redis启动界面,无法登陆系统,shell命令行也无法显示,如下图:问题原......
  • js一键切换dark模式 -
    js一键切换dark模式//初始化$(function(){lettheme=$.cookie('theme');if(theme=="dark"){$("body").addClass('dark');$("#theme").add......
  • 一键部署LNMP平台脚本
    分享一个一键部署LNMP平台脚本,设置通过systemd管理Nginx服务,好用!#!/bin/bashyum-yinstallgccopenssl-develpcre-develtarxflnmp_soft.tar.gzcdlnmp_soft/tarxfngi......
  • python 网页登录了之后拿着cookie直接用于脚本程序获取api接口数据
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#python3.7cookieurllib3,requests##pipinstallrequests#分析网站数据源接口规律#当前......
  • 问题定位记录1:Djano-redis库报错No connection available
    一、问题现象MANO(网络管理与编排软件)原子层创建资源后写redis缓存时报错Noconnectionavaiable堆栈打印:Traceback(mostrecentcalllast): File"/home/oes/.loca......
  • Linux系统下如何设置开机自动运行脚本?以Redis开机自启为例
    参考链接:https://baijiahao.baidu.com/s?id=1722174560616569543&wfr=spider&for=pc前言:最近在安装Redis的时候,想找下Redis开机自启的功能,在网上找了很多的教程,貌似很多......
  • Token需要缓存到Redis的原因
    1、实现单点登录假设:有设备AB、无单点登录限制的某APP、一个账号(用户)设备A在APP第1次登录用户,服务器会给该用户创建1个Token并返回到设备A,设备A会将该Token保存到本地......
  • redis 持久化之RDB和AOF的区别
    持久化之RDB定义:在指定的时间间隔内生成数据集的时间点快照RDB的优点:1.RDB是一个非常紧凑的文件它保存了Redis在某个时间点上的数据集。这种文件非常适合用于进......