Redis编译安装脚本


Redis简单安装脚本

#!/bin/bash

# 检查系统类型
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS_NAME=$NAME
    OS_VERSION=$VERSION_ID
else
    echo "无法识别操作系统"
    exit 1
fi

echo "当前操作系统: $OS_NAME $OS_VERSION"

# 根据操作系统选择安装命令
if [[ "$OS_NAME" == "Ubuntu" ]]; then
    # 更新包列表并安装依赖
    apt update
    apt -y install build-essential tcl wget

elif [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
    # 清理和更新包缓存
    yum clean all
    yum makecache fast
    yum -y install gcc gcc-c++ make tcl wget

    # 编译Redis所需要的gcc
    yum -y install centos-release-scl
    yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
    source /opt/rh/devtoolset-9/enable
    echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
    gcc --version

else
    echo "不支持的操作系统"
    exit 1
fi

# 下载Redis源码包
if [ -f redis-6.2.14.tar.gz ]; then
    echo "redis-6.2.14.tar.gz已存在不需要下载!"
else
    # wget http://download.redis.io/releases/redis-6.2.14.tar.gz
    wget http://192.168.3.200/Software/redis-6.2.14.tar.gz
fi

# 解压源码包
tar -xvf redis-6.2.14.tar.gz

# 进入解压后的目录
cd redis-6.2.14

# Redis编译安装
make
make PREFIX=/usr/local/redis install

# Redis基础配置
mkdir -p /usr/local/redis/{etc,logs,data}
egrep -v "^$|^#" redis.conf > /usr/local/redis/etc/redis.conf

sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /usr/local/redis/etc/redis.conf
sed -i "s/protected-mode yes/protected-mode no/g" /usr/local/redis/etc/redis.conf
sed -i "s/daemonize no/daemonize yes/g" /usr/local/redis/etc/redis.conf
sed -i "s/pidfile \/var\/run\/redis_6379.pid/pidfile \/usr\/local\/redis\/redis.pid/g" /usr/local/redis/etc/redis.conf
sed -i "s/dir \.\//dir \/usr\/local\/redis\/data/g" /usr/local/redis/etc/redis.conf
sed -i "s/logfile \"\"/logfile \"\/usr\/local\/redis\/logs\/redis.log\"/g" /usr/local/redis/etc/redis.conf
sed -i "s/dbfilename dump.rdb/dbfilename dump.rdb/g" /usr/local/redis/etc/redis.conf
sed -i "s/appendfilename \"appendonly.aof\"/appendfilename \"appendonly.aof\"/g" /usr/local/redis/etc/redis.conf

# 将配置文件复制到/etc下
mkdir -p /etc/redis/
cp /usr/local/redis/etc/redis.conf /etc/redis/

# 启动redis
/usr/local/redis/bin/redis-server /etc/redis/redis.conf

# 更新PATH环境变量
echo 'export PATH=$PATH:/usr/local/redis/bin/' >> /etc/profile
source /etc/profile

# 查看redis监听端口
netstat -tanp | grep redis

编写一个Redis启动、关闭和重启的Shell脚本。以下是一个示例脚本:

#!/bin/bash

REDIS_PATH="/usr/local/redis"
REDIS_CONF="/etc/redis/redis.conf"
PID_FILE="$REDIS_PATH/redis.pid"

start() {
    if [ -f $PID_FILE ]; then
        echo "Redis is already running."
        exit 1
    fi

    echo "Starting Redis..."
    $REDIS_PATH/bin/redis-server $REDIS_CONF &
    echo $! > $PID_FILE
    echo "Redis started."
}

stop() {
    if [ ! -f $PID_FILE ]; then
        echo "Redis is not running."
        exit 1
    fi

    echo "Stopping Redis..."
    kill $(cat $PID_FILE)
    rm $PID_FILE
    echo "Redis stopped."
}

restart() {
    stop
    sleep 1
    start
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

将此脚本保存为redis.sh,并确保它具有可执行权限(使用chmod +x redis.sh)。然后,您可以使用以下命令来启动、停止和重启Redis服务器:

chmod +x redis.sh
./redis.sh start             # 启动Redis服务器
./redis.sh stop              # 停止Redis服务器
./redis.sh restart           # 重启Redis服务器

# 习惯上会把启动脚本放置在/etc/init.d/目录下,方便使用
cp redis.sh /etc/init.d/redis