1 Star 0 Fork 0

yll1024335892 / shell_script

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
redis.sh 7.66 KB
一键复制 编辑 原始数据 按行查看 历史
yll1024335892 提交于 2023-09-06 11:17 . chore: 添加关闭reids的服务
#/bin/bash
#设置dir ${INSTALL_DIR}/redis/data/
#bind 127.0.0.1只接收本机访问 为bind 0.0.0.0可以远程访问
#protected-mode为no是允许远程访问,yes禁止远程访问
#logfile "${INSTALL_DIR}/redis/log/redis-6379.log"
#daemonize为yes为守护进程使用,no非守护进程运行
#dbfilename redis6379.rdb设置持久话名称
#requirepass是配置远程连接的密码 requirepass和masterauth密码配置一样
#masterauth是配置主同步的密码
#####################################################
#AOF的配置相关
#1、appendonly:配置是否开启AOF,yes表示开启,no表示关闭。默认是no。
#2、appendfilename:AOF保存文件名
#3、appendfsync:AOF异步持久化策略
#always:同步持久化,每次发生数据变化会立刻写入到磁盘中。性能较差但数据完整性比较好(慢,安全)
#everysec:出厂默认推荐,每秒异步记录一次(默认值)
#no:不即时同步,由操作系统决定何时同步。
#4、no-appendfsync-on-rewrite:重写时是否可以运用appendsync,默认no,可以保证数据的安全性。
#5、auto-aof-rewrite-percentage:设置重写的基准百分比
#6、auto-aof-rewrite-min-size:设置重写的基准值
#####################################################
#####################################################
#其它配置
#timeout 一个空闲的客户端维持多少秒会关闭,0表示关闭该功能。即永不关闭
#tcp-keepalive 对访问客户端的一种心跳检测,每个n秒检测一次。单位为秒,如果设置为0,则不会进行Keepalive检测,建议设置成60
#pidfile存放pid文件的位置,每个实例会产生一个不同的pid文件
#loglevel 指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为notice四个级别根据使用阶段来选择,生产环境选择notice 或者warning
#logfile日志文件名称
#databases设定库的数量 默认16,默认数据库为0,可以使用SELECT 命令在连接上指定数据库id
#maxclients设置redis同时可以与多少个客户端进行连接默认情况下为10000个客户端。如果达到了此限制,redis则会拒绝新的连接请求,并且向这些连接请求方发出"max number of clients reached"以作回应。
#maxmemory建议必须设置,否则,将内存占满,造成服务器宕机设置redis可以使用的内存量。一旦到达内存使用上限,redis将会试图移除内部数据,移除规则可以通过maxmemory-policy来指定。如果redis无法根据移除规则来移除内存中的数据,或者设置了“不允许移除”,那么redis则会针对那些需要申请内存的指令返回错误信息,比如SET、LPUSH等。但是对于无内存申请的指令,仍然会正常响应,比如GET等。如果你的redis是主redis(说明你的redis有从redis),那么在设置内存使用上限时,需要在系统中留出一些内存空间给同步队列缓存,只有在你设置的是“不移除”的情况下,才不用考虑这个因素
#####
#maxmemory-policy
#volatile-lru:使用LRU算法移除key,只对设置了过期时间的键;(最近最少使用)
#allkeys-lru:在所有集合key中,使用LRU算法移除key
#volatile-random:在过期集合中移除随机的key,只对设置了过期时间的键
#allkeys-random:在所有集合key中,移除随机的key
#volatile-ttl:移除那些TTL值最小的key,即那些最近要过期的key
#noeviction:不进行移除。针对写操作,只是返回错误信息
#####
#maxmemory-samples设置样本数量,LRU算法和最小TTL算法都并非是精确的算法,而是估算值,所以你可以设置样本的大小,redis默认会检查这么多个key并选择其中LRU的那个。一般设置3到7的数字,数值越小样本越不准确,但性能消耗越小
#####################################################
#######################
# 设置环境变量
# vi /etc/profile
# export PATH=$PATH:/usr/local/redis/bin #redis安装目录以实际为主
# source /etc/profile
# 启动
# redis-server /usr/local/redis/redis.conf #redis安装目录以实际为主
# 配置了开机自动启动
# service redisd start | stop
# 配置了密码后service stop报需要密码的问题 redis-cli -a 配置的redis密码值 -p 端口号 shutdown
#######################
VER="7.2.0"
# https://github.com/redis/redis/archive/${VER}.tar.gz
RESOURCE_URL="https://mirrors.huaweicloud.com/redis/redis-${VER}.tar.gz"
DOWNLOAD_DIR="/home/tools"
INSTALL_DIR="/usr/local"
PORT=6377
function installRedis() {
rpm -q gcc &>/dev/null
if [ $? -ne 0 ]; then
yum install gcc -y
fi
rpm -q wget &>/dev/null
if [ $? -ne 0 ]; then
yum install wget -y
fi
if [ ! -d "${DOWNLOAD_DIR}" ]; then
mkdir -p ${DOWNLOAD_DIR}
fi
cd ${DOWNLOAD_DIR}
if [ ! -f "${DOWNLOAD_DIR}/redis-${VER}.tar.gz" ]; then
wget -c $RESOURCE_URL
fi
if [ -f "${DOWNLOAD_DIR}/redis-${VER}.tar.gz" ]; then
if [ ! -d "redis-${VER}" ]; then
cd ${DOWNLOAD_DIR}
tar -zxvf redis-${VER}.tar.gz
fi
fi
if [ -d "${DOWNLOAD_DIR}/redis-${VER}" ]; then
cd ${DOWNLOAD_DIR}/redis-${VER}
make && make install PREFIX=${INSTALL_DIR}/redis
if [ $? -eq 0 ]; then
echo "安装成功"
else
echo "安装失败"
fi
fi
}
function setConfig() {
if [ ! -d "${INSTALL_DIR}/redis" ]; then
mkdir -p ${INSTALL_DIR}/redis
fi
cp ${DOWNLOAD_DIR}/redis-${VER}/redis.conf ${INSTALL_DIR}/redis
sed -i "s/^port 6379/port ${PORT}/g" ${INSTALL_DIR}/redis/redis.conf
sed -i "s/redis_6379.pid/redis_${PORT}.pid/g" ${INSTALL_DIR}/redis/redis.conf
sed -i "s/redis-6379.log/redis-${PORT}.log/g" ${INSTALL_DIR}/redis/redis.conf
if [ ! -d "${INSTALL_DIR}/redis/log" ]; then
mkdir -p ${INSTALL_DIR}/redis/log
fi
if [ ! -d "${INSTALL_DIR}/redis/data" ]; then
mkdir -p ${INSTALL_DIR}/redis/data
fi
cd ${INSTALL_DIR}/redis
}
##### 自动启动
function autoStart(){
if [ ! -f "/etc/rc.d/init.d/redisd" ]; then
cat > /etc/rc.d/init.d/redisd <<END
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
REDISPORT=${PORT}
EXEC=${INSTALL_DIR}/redis/bin/redis-server
CLIEXEC=${INSTALL_DIR}/redis/bin/redis-cli
PIDFILE=/var/run/redis_\${REDISPORT}.pid
CONF="${INSTALL_DIR}/redis/redis.conf"
case "\$1" in
start)
if [ -f \$PIDFILE ]
then
echo "\$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
\$EXEC \$CONF
fi
;;
stop)
if [ ! -f \$PIDFILE ]
then
echo "\$PIDFILE does not exist, process is not running"
else
PID=\$(cat \$PIDFILE)
echo "Stopping ..."
\$CLIEXEC -p \$REDISPORT shutdown
while [ -x /proc/\${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
END
chmod +x /etc/rc.d/init.d/redisd
chkconfig redisd on
fi
}
if [[ "$1" = "installRedis" ]]; then
installRedis
fi
if [[ "$1" = "config" ]]; then
setConfig
fi
if [[ "$1" = "autoStart" ]]; then
autoStart
fi
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/1024335892/shell_script.git
git@gitee.com:1024335892/shell_script.git
1024335892
shell_script
shell_script
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891