陈佳浩
陈佳浩
发布于 2024-06-29 / 23 阅读
0
0

Redis安装

#!/bin/bash
#
#********************************************************************
#Author:            chenjiahao
#QQ:                1938191576
#Date:              2022-08-22
#FileName:          install_redis.sh
#URL:               https://www.placjh.com
#Description:       The deploy script
#Copyright (C):     2022 All rights reserved
#*******************************************************************

RD=redis-7.2.3
DIR=/apps
PASSWORD=1syhLqT

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 
}

. /etc/os-release

download(){
	if [ $ID = "rocky" -o $ID = "centos" ];then
		yum -y install gcc make jemalloc-devel systemd-devel &>/dev/null
	elif [ $ID = "ubuntu" ];then
		echo "更新apt源"
		apt update &>/dev/null
		apt -y install gcc make libjemalloc-dev libsystemd-dev &>/dev/null
	else
		echo OS不支持
		exit
	fi
	color 安装依赖包完毕 0
	echo "下载redis源码包"
	if ! [ -f redis-7.2.3.tar.gz ];then
		wget https://download.redis.io/releases/$RD.tar.gz &>/dev/null
		if [ $? -eq 0 ];then
			color 下载成功 0
		else
			color下载失败 1
			exit
		fi
	fi
	tar xf redis-7.2.3.tar.gz
}

install(){
	color 开始编译 0
	mkdir -p $DIR/redis
	cd $RD
	make -j 2 USE_SYSTEMD=yes PREFIX=$DIR/redis install &>/dev/null
	if [ $? -eq 0 ];then
		color 编译完毕 0
	else
		color 编译失败 1
		exit
	fi
	ln -s $DIR/redis/bin/redis-* /usr/bin/; 
	mkdir -p $DIR/redis/{etc,log,data,run}
	cp redis.conf $DIR/redis/etc/
}
modify_conf(){
	sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir $DIR/redis/data/" -e "/logfile .*/c logfile $DIR/redis/log/redis-6379.log" -e "/^pidfile .*/c pidfile $DIR/redis/run/redis_6379.pid" -e "s/# maxclients 10000/maxclients 100000/" $DIR/redis/etc/redis.conf
	useradd -r -s /sbin/nologin redis
	chown -R redis.redis $DIR/redis
	cat >> /etc/sysctl.conf <<-EOF
	net.core.somaxconn = 1024
	vm.overcommit_memory = 1
	EOF
	sysctl -p &>/dev/null
	color 更改配置完毕 0
}

ready_start(){
	if [ $ID = "rocky" -o $ID = "centos" ];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.loacl
		chmod +x /etc/rc.loacl
		/etc/rc.loacl
	fi

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

	[Service]
	ExecStart=$DIR/redis/bin/redis-server $DIR/redis/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
	color 启动前准备 0
}

run(){
	echo 'nofile - max 1000000' >> /etc/security/limits.conf
	systemctl daemon-reload
	systemctl enable --now redis &>/dev/null
	if [ $? -eq 0 ];then
		color 启动完毕 0
	else
		color 启动失败 1
	fi
}

download
install
modify_conf
ready_start
run


评论