mysql5.x安装脚本

  直接贴出来:

#!/bin/bash
#linux安装mysql服务分两种安装方法:
#①源码安装,优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错;
#②使用官方编译好的二进制文件安装,优点是安装速度快,安装步骤简单,缺点是安装包很大,300M左右
MYSQL_PACKET="/home/tools"
MYSQL_REPO="5.6"
MYSQL_VERSION="5.6.34"
MYSQL_BASEDIR="/usr/local/mysql"
MYSQL_DATADIR="/home/sqldata/mysql"
MYSQL_SOCKET="/tmp/mysql.sock"
YELLOW_COLOR='\E[1;33m'
RED_COLOR='\E[1;31m'
RES='\E[0m'
function yellow(){
          [ $# -ne 1 ] && exit 1
          echo -e  "${YELLOW_COLOR}$1${RES}" 
}
function red(){
          [ $# -ne 1 ] && exit 1
          echo -e  "${RED_COLOR}$1${RES}" 
}
function BAR(){
        i=0
         str=""
          arry=("\\" "|" "/" "-")
           while [ $i -le 100 ]
              do
               let index=i%4
                if [ $i -le 20 ];then
                    let color=44
                    let bg=34
                elif [ $i -le 45 ];then
                     let color=43
                     let bg=33
                 elif [ $i -le 75 ];then
                     let color=41
                     let bg=31
                else
                    let color=42
                    let bg=32
                fi
            printf "      \033[${color};${bg}m%-s\033[0m %d %c\r" "$str" "$i" "${arry[$index]}"
          usleep 3000
         let i=i+1
       str+="#"
         done
     printf "\n"
}
#Check if user is root 
function check_user(){
if [ $UID != 0 ];then
        echo -e "\033[31m      Error: You must be root to run this script, please use root to install\033[0m"
    exit 1
fi
clear
cat <<EOF
             ---------------------------------------------------------------------------------------------------
            |***************A script to auto-compile & install `yellow mysql-${MYSQL_VERSION}` on Redhat/CentOS Linux***************|
             ---------------------------------------------------------------------------------------------------

EOF
}
function check_env(){
           Distrib="${MYSQL_VERSION}"
           status1="`mysql --version |awk -F '[ ,]' '{print $6}'`"
          if [ "$status1" = "$Distrib" ];then
             echo -e "\033[32m      Warning: mysql-${MYSQL_VERSION} is already installed! \033[0m"
             sleep 1 ;
             BAR
             echo -e "\033[31m      [退出] \033[0m"
             exit 1
          else
             echo -e "\033[32m      Warning: mysql-${MYSQL_VERSION} is not install \033[0m"
             echo -e "\033[34m      Install mysql-${MYSQL_VERSION},Please input y \033[0m"
             read -p "(Please input `yellow y`|`red n`): " installmysql
             case "$installmysql" in
                 y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
                    echo -e "\033[34m      You will install mysql-${MYSQL_VERSION} \033[0m"
                    installmysql
                    ;;
                 n|N)
                   echo -e "\033[31m      [退出] \033[0m"
                   exit 0
                   ;;
                  *)
                    echo -e "\033[31m      INPUT error,You will exit install mysql-${MYSQL_VERSION} \033[0m"
                    exit 1
              esac
fi
}
function installmysql(){            
                [ ! -d ${MYSQL_PACKET} ] && mkdir -p ${MYSQL_PACKET}
                [ ! -d ${MYSQL_BASEDIR} ] && mkdir -p ${MYSQL_BASEDIR}
                [ ! -d ${MYSQL_DATADIR} ] && mkdir -p ${MYSQL_DATADIR}
                if [ ! -f ${MYSQL_PACKET}/mysql-${MYSQL_VERSION}.tar.gz ] ;then
                   echo -e "\033[31m      There is without ${MYSQL_PACKET}/mysql-${MYSQL_VERSION}.tar.gz ! \033[0m"
                    sleep 1;
                      echo -e "\033[32m      mysql-${MYSQL_VERSION}.tar.gz is downloading...... \033[0m"
                      cd ${MYSQL_PACKET} &&\ 
                      [ -f mysql-${MYSQL_VERSION} ] || wget http://downloads.mysql.com/archives/mysql-${MYSQL_REPO}/mysql-${MYSQL_VERSION}.tar.gz
                     #wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22.tar.gz
                    #wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
                    sleep 1;
                   echo -e "\033[32m      Installing mysql-${MYSQL_VERSION},please waiting...... \033[0m"
                  start_install
               else
                  echo -e "\033[32m      Installing mysql-${MYSQL_VERSION},please waiting...... \033[0m"
                  start_install
               fi
}

function start_install(){
        # 补充部分 Install base tools & packages & library
         if [ ! -f epel-release-6-8.noarch.rpm ] ;then
         #    wget -c http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm
              wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
             rpm -ivh epel-release-6-8.noarch.rpm
         fi
        for i in $(rpm -q glibc glibc-static glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel libaio readline-devel curl curl-devel libidn libidn-devel autoconf libjpeg libjpeg_devel libpng libpng_devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel  gcc gcc-c++ lrzsz ntpdate nmap.x86_64 lsof.x86_64 sysstat.x86_64 glances kernel-devel openssl openssl-devel  popt-devel popt-static libnl-devel libicu-devel libevent-devel tree nc make cmake jemalloc pcre pcre-devel pcre-static libzip.x86_64 libzip-devel.x86_64 libevent-devel libool libool-ltdl gd-devel bison vim-enhanced zip unzip patch expect|awk '/not install/ {print $2}') 
        do 
            yum -y install $i >/dev/null 2>&1
       done
         id mysql >/dev/null
         [ $? -eq 0 ] && usermod -s /sbin/nologin mysql &>/dev/null || useradd -s /sbin/nologin -M mysql >/dev/null 
            cd ${MYSQL_PACKET} &&\
            tar xf mysql-${MYSQL_VERSION}.tar.gz
            cd mysql-${MYSQL_VERSION} 
            cmake . -DCMAKE_INSTALL_PREFIX=${MYSQL_BASEDIR} \
            -DMYSQL_DATADIR=${MYSQL_DATADIR} \
            -DMYSQL_UNIX_ADDR=${MYSQL_SOCKET} \
            -DDEFAULT_CHARSET=utf8 \
            -DDEFAULT_COLLATION=utf8_general_ci \
            -DWITH_EXTRA_CHARSETS=all \
            -DWITH_INNOBASE_STORAGE_ENGINE=1 \
            -DWITH_FEDERATED_STORAGE_ENGINE=1 \
            -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
            -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
            -DWITH_ZLIB=bundled \
            -DWITH_SSL=bundled \
            -DENABLED_LOCAL_INFILE=1 \
            -DWITH_EMBEDDED_SERVER=1 \
            -DENABLE_DOWNLOADS=1 \
            -DWITH_DEBUG=0
            sleep 2;
            make && make install
            if [ $? -eq 0 ];then
                echo -e "\033[32m      mysql-${MYSQL_VERSION} is making successful! \033[0m"
            else
                echo -e "\033[31m      mysql-${MYSQL_VERSION} is making of failure! \033[0m"
               exit 1     
           fi     
           #ln -s /usr/local/mysql-5.6.34/ /usr/local/mysql
           \cp support-files/my*.cnf /etc/my.cnf            
           #初始化和配置数据库
           ${MYSQL_BASEDIR}/scripts/mysql_install_db --basedir=${MYSQL_BASEDIR} --datadir=${MYSQL_DATADIR} --user=mysql
           chown -R mysql.mysql ${MYSQL_BASEDIR}/
           \cp support-files/mysql.server /etc/init.d/mysqld  
           chmod 700 /etc/init.d/mysqld
           chkconfig mysqld on  && /etc/init.d/mysqld start
           grep "/usr/local/mysql/bin/" /etc/profile >/dev/null
           if [ $? -ne 0 ];then
              echo 'PATH=/usr/local/mysql/bin/:$PATH' >>/etc/profile && source /etc/profile
           else
              exit 0
           fi
           PID="`netstat -lnpt|grep 330|awk  -F '[ /]+' '{print $7}'`"
           if [ -z $PID ];then
                echo -e "\033[31m      mysql-${MYSQL_VERSION} is start failure! \033[0m"         
           else
                BAR
                echo -e "\033[32m      mysql-${MYSQL_VERSION} is start successful! \033[0m"
           fi          
 
}
check_user
check_env
View Code

 

posted @ 2019-04-02 14:53  淺景尘  阅读(164)  评论(0编辑  收藏  举报
TOP