39.第33章 企业级NoSQL数据库Redis

一.Redis一键编译安装脚本

root@ubuntu2004:~# cat install_redis.sh 
#!/bin/bash
#
#******************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-04-22
#FileName:      install_redis.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_redis for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#******************************************************************************
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}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${REDIS_FILE} ];then
        ${COLOR}"缺少${REDIS_FILE}文件"${END}
        ${COLOR}'开始下载REDIS源码包'${END}
        wget ${URL}${REDIS_FILE} || { ${COLOR}"Redis 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${REDIS_FILE}文件已准备好"${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
    id redis &> /dev/null || { useradd -s /sbin/nologin -r  redis ; ${COLOR}"创建redis用户"${END}; }
    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

二.Redis Cluster的ruby一键编译脚本

[root@centos7 ~]# cat install_ruby.sh
#!/bin/bash
#
#******************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-04-29
#FileName:      install_ruby.sh
#URL:           www.cnblogs.com/neteagles
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#******************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

URL='https://cache.ruby-lang.org/pub/ruby/2.5/'
RUBY_FILE=ruby-2.5.5.tar.gz

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}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${RUBY_FILE} ];then
        ${COLOR}"缺少${RUBY_FILE}文件"${END}
        ${COLOR}'开始下载RUBY源码包'${END}
        wget ${URL}${RUBY_FILE} || { ${COLOR}"Ruby 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${RUBY_FILE}文件已准备好"${END}
    fi
}

install(){
    ${COLOR}"开始安装RUBY"${END}
    ${COLOR}"开始安装RUBY依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc make openssl openssl-devel zlib-devel &> /dev/null || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
    else
        apt -y install gcc make openssl libssl-dev zlib1g-dev &> /dev/null  || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
    fi
    cd ${SRC_DIR}
    tar xf ${RUBY_FILE}
    RUBY_DIR=`echo ${RUBY_FILE} | sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${RUBY_DIR}
    ./configure
    make -j ${CPUS} && make install 
    [ $? -eq 0 ] && $COLOR"RUBY编译安装成功"$END ||  { $COLOR"RUBY编译安装失败,退出!"$END;exit; }
    gem install redis
    ${COLOR}"RUBY安装完成"${END}
}

main(){
    os
    check_file
    install
}

main

三.一键编译安装memcache

[root@centos7 ~]# cat install_memcache.sh 
#!/bin/bash
#
#******************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-04-29
#FileName:      install_memcache.sh
#URL:           www.neteagles.cn
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#******************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

URL='http://memcached.org/files/'
MEMCACHE_FILE=memcached-1.6.9.tar.gz
INSTALL_DIR=/apps/memcache

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}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${MEMCACHE_FILE} ];then
        ${COLOR}"缺少${MEMCACHE_FILE}文件"${END}
        ${COLOR}'开始下载MEMCACHE源码包'${END}
        wget ${URL}${MEMCACHE_FILE} || { ${COLOR}"Memcache 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${MEMCACHE_FILE}文件已准备好"${END}
    fi
}

install(){
    [ -d ${INSTALL_DIR} ] && { ${COLOR}"Memcache已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装MEMCACHE"${END}
    ${COLOR}"开始安装MEMCACHE依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc libevent libevent-devel &> /dev/null || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
    else
        apt -y install make gcc libevent-dev  &> /dev/null  || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
    fi
    cd ${SRC_DIR}
    tar xf ${MEMCACHE_FILE}
    MEMCACHE_DIR=`echo ${MEMCACHE_FILE} | sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${MEMCACHE_DIR}
    ./configure --prefix=${INSTALL_DIR}
    make -j ${CPUS} && make install && ${COLOR}"Memcache 编译安装完成"${END} || { ${COLOR}"Memcache 编译安装失败"${END};exit ; }
    ln -s ${INSTALL_DIR}/bin/*  /usr/bin/
    if id memcached &> /dev/null ;then
        ${COLOR}"Memcached 用户已存在"${END}
    else
        useradd -r -s /sbin/nologin memcached
        ${COLOR}"Memcached 用户创建成功"${END}
    fi
    [ -d /etc/sysconfig ] || mkdir /etc/sysconfig
    cat > /etc/sysconfig/memcached <<-EOF
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
EOF
    cp scripts/memcached.service /lib/systemd/system/
    systemctl daemon-reload
    systemctl enable --now  memcached &> /dev/null 
    systemctl is-active memcached &> /dev/null && ${COLOR}"Memcache 服务启动成功"${END} || { ${COLOR}"Memcache 启动失败"${END};exit; }
    ${COLOR}"MEMCACHE安装完成"${END}
}

main(){
    os
    check_file
    install
}

main

四.memcache高可用repcached一键编译安装

[root@centos7 ~]# cat install_repcached.sh
#!/bin/bash
#
#******************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-04-29
#FileName:      install_repcached.sh
#URL:           www.neteagles.cn
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#******************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

URL='https://sourceforge.net/projects/repcached/files/repcached/2.2.1-1.2.8/'
REPCACHED_FILE=memcached-1.2.8-repcached-2.2.1
TAR=.tar.gz
INSTALL_DIR=/apps/repcached

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}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${REPCACHED_FILE}${TAR} ];then
        ${COLOR}"缺少${REPCACHED_FILE}${TAR}文件"${END}
        ${COLOR}'开始下载REPCACHED源码包'${END}
        wget ${URL}${REPCACHED_FILE}${TAR} || { ${COLOR}"Repcached 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${REPCACHED_FILE}${TAR}文件已准备好"${END}
    fi
}

install(){
    [ -d ${INSTALL_DIR} ] && { ${COLOR}"Repcached已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装REPCACHED"${END}
    ${COLOR}"开始安装REPCACHED依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc libevent libevent-devel &> /dev/null || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
    else
        apt -y install make gcc libevent-dev  &> /dev/null  || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
    fi
    cd ${SRC_DIR}
    tar xf ${REPCACHED_FILE}${TAR}
    cd ${REPCACHED_FILE}
     ./configure --prefix=${INSTALL_DIR} --enable-replication
    sed -i -e '57d' -e '59d' memcached.c
    make -j ${CPUS} && make install && ${COLOR}"Repcached 编译安装完成"${END} || { ${COLOR}"Repcached 编译安装失败"${END};exit ; }
    ln -s ${INSTALL_DIR}/bin/*  /usr/bin/
    if id memcached &> /dev/null ;then
        ${COLOR}"Memcached 用户已存在"${END}
    else
        useradd -r -s /sbin/nologin memcached
        ${COLOR}"Memcached 用户创建成功"${END}
    fi
    ${COLOR}"Repcached安装完成"${END}
}

main(){
    os
    check_file
    install
}

main
posted @ 2021-04-21 20:46  网络之鹰  阅读(789)  评论(0编辑  收藏  举报