一键安装redis脚本

Posted y_zilong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一键安装redis脚本相关的知识,希望对你有一定的参考价值。

脚本1
[root@cent8_yzl_10 ~]# cat install_redis.sh 
#!/bin/bash
SRC_DIR=/usr/local/src
COLOR="echo -e \\\\033[01;31m"
END='\\033[0m'
CPUS=`lscpu |awk '/^CPU\\(s\\)/{print $2}'`

URL='http://download.redis.io/releases/'
VERSION=redis-6.2.2
PASSWORD=123456
INSTALL_DIR=/apps/redis

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
}

install(){
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum  -y install gcc jemalloc-devel || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    else
        apt -y install make gcc libjemalloc-dev  || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
    fi

    cd ${SRC_DIR}
    wget ${URL}${VERSION}.tar.gz || { ${COLOR}"Redis 源码下载失败"${END}; exit; }
    tar xf ${VERSION}.tar.gz
    cd ${VERSION}
    make -j ${CPUS} PREFIX=${INSTALL_DIR} install && ${COLOR}"Redis 编译安装完成"${END} || { ${COLOR}"Redis 编译安装失败"${END};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 用户已存在"${END}
    else
        useradd -r -s /sbin/nologin redis
        ${COLOR}"Redis 用户创建成功"${END}
    fi

    chown -R redis.redis ${INSTALL_DIR}
    cat >> /etc/sysctl.conf <<-EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
    sysctl -p
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
        chmod +x /etc/rc.d/rc.local
    else
        cat >> /lib/systemd/system/rc-local.service <<-EOF

[Install]
WantedBy=multi-user.target
EOF
        echo '#!/bin/bash' > /etc/rc.local
        echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
        chmod +x /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=redisRuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now  redis &> /dev/null && ${COLOR}"Redis 服务启动成功,Redis信息如下:"${END} || { ${COLOR}"Redis 启动失败"${END};exit; }
    sleep 1
    redis-cli -a ${PASSWORD} INFO Server 2> /dev/null
}

main(){
    os
    install
}

main
[root@cent8_yzl_10 ~]# 
脚本2
[root@cen7_17 ~]# cat install_redis_v2.sh 
#!/bin/bash
SRC_DIR=/usr/local/src
COLOR="echo -e \\\\033[01;31m"
END='\\033[0m'
CPUS=`lscpu |awk '/^CPU\\(s\\)/{print $2}'`

URL='https://download.redis.io/releases/'
REDIS_FILE=redis-6.2.2.tar.gz
VERSION=`echo $REDIS_FILE |awk -F"[-.]" '{print $2}'`
PASSWORD=123456
INSTALL_DIR=/apps/redis

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
}

check_file (){
    cd ${SRC_DIR}
    rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    if [ ! -e ${REDIS_FILE} ];then
        ${COLOR}"缺少${REDIS_FILE}文件"${END}
        ${COLOR}'开始下载REDIS源码包'${END}
        wget ${URL}${REDIS_FILE} || { ${COLOR}"Redis 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install(){
    [ -d ${INSTALL_DIR} ] && { ${COLOR}"Redis已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装REDIS"${END}
    ${COLOR}"开始安装REDIS依赖包"${END}
    if [ ${VERSION} == "6" ] ;then
        if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
            yum  -y install gcc jemalloc-devel systemd-devel &> /dev/null || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
        else
            apt -y install make gcc libjemalloc-dev libsystemd-dev &> /dev/null  || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
        fi
    else
         if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
            yum  -y install gcc jemalloc-devel &> /dev/null || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
        else
            apt -y install make gcc libjemalloc-dev &> /dev/null  || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
        fi
    fi
    cd ${SRC_DIR}
    tar xf ${REDIS_FILE}
    REDIS_DIR=`echo ${REDIS_FILE} | sed -nr 's/^(.*[0-9]).*/\\1/p'`
    cd ${REDIS_DIR}
    if [ ${VERSION} == "6" ] ;then
        make -j ${CPUS} USE_SYSTEMD=yes PREFIX=${INSTALL_DIR} install && ${COLOR}"Redis 编译安装完成"${END} || { ${COLOR}"Redis 编译安装失败"${END};exit ; }
    else
        make -j ${CPUS} PREFIX=${INSTALL_DIR} install && ${COLOR}"Redis 编译安装完成"${END} || { ${COLOR}"Redis 编译安装失败"${END};exit ; }
    fi
    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 用户已存在"${END}
    else
        useradd -r -s /sbin/nologin redis
        ${COLOR}"Redis 用户创建成功"${END}
    fi

    chown -R redis.redis ${INSTALL_DIR}
    cat >> /etc/sysctl.conf <<-EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
    sysctl -p
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
        chmod +x /etc/rc.d/rc.local
    else
        cat >> /lib/systemd/system/rc-local.service <<-EOF

[Install]
WantedBy=multi-user.target
EOF
        echo '#!/bin/bash' > /etc/rc.local
        echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
        chmod +x /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
ExecReload=/bin/kill -s HUP \\$MAINPID
ExecStop=/bin/kill -s QUIT \\$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now  redis &> /dev/null 
    systemctl is-active redis &> /dev/null && ${COLOR}"Redis 服务启动成功,Redis信息如下:"${END} || { ${COLOR}"Redis 启动失败"${END};exit; }
    redis-cli -a ${PASSWORD} info server 2> /dev/null
    ${COLOR}"REDIS安装完成"${END}
}

main(){
    os
    check_file
    install
}

main
[root@cen7_17 ~]# 
#shell脚本写入数据到redis
[root@cen7_17 ~]# cat redis_test.sh 
#!/bin/bash
NUM=100
PASS=
for i in `seq $NUM`;do
    redis-cli -h 127.0.0.1 -a "$PASS"  --no-auth-warning  set key${i} value${i}
    echo "key${i} value${i} 写入完成"
done
echo "$NUM个key写入到Redis完成"  
[root@cen7_17 ~]# 
#python脚本写入数据到redis
[root@cen7_17 ~]# yum install -y python3
[root@cen7_17 ~]# yum install -y python-redis
#注意文件名不要为redis,会和redis模块名称冲突

[root@cen7_17 ~]# cat redis_test.py 
#!/bin/env python3 
import redis
pool = redis.ConnectionPool(host="127.0.0.1",port=6379,password="$PASS",decode_responses=True)
r = redis.Redis(connection_pool=pool)
for i in range(100):
    r.set("k%d" % i,"v%d" % i)
    data=r.get("k%d" % i)
    print(data)
[root@cen7_17 ~]# chmod +x redis_test.py 
[root@cen7_17 ~]# ./redis_test.py 

以上是关于一键安装redis脚本的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本一键部署——Redis安装部署

shell脚本一键部署——Redis安装部署

Shell脚本——一键安装Redis

Shell脚本——一键安装Redis

脚本一键安装redis实现主从复制

shell脚本一键在线安装redis数据库(使用函数实现)