說明:使用mysql generic tar.gz包快速安裝mysqlpython
三個文件installation_of_single_mysql.sh、template_install-my.cnf、mysql-5.7.18-linux-glibc2.5-x86_64.tar.gzmysql
修改腳本的配置信息指定template_install-my.cnf和mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz位置linux
默認root帳號是iforogtc++
#!/bin/bash # line: V1.8 # mail: gczheng@139.com # data: 2018-09-04 # script_name: installation_of_single_mysql.sh # function: Install mysql5.7.18 #======================================================================= #配置信息 #======================================================================= MYSQL_DATADIR=/data/mysqldata MYCNF=template_install-my.cnf MYSQL_SOURCE_PACKAGES=/software/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz MYSQL_SOURCE_PACKAGES_NAMES=`echo $MYSQL_SOURCE_PACKAGES |awk -F '/' '{print $NF}' |awk -F ".tar.gz" '{printf $1}'` MYSQL_DOWNLOAD_LINK='http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz' MYSQL=/usr/local/mysql/bin/mysql #======================================================================= # echo添加顏色 #======================================================================= echo_color(){ color=${1} && shift case ${color} in black) echo -e "\e[0;30m${@}\e[0m" ;; red) echo -e "\e[0;31m${@}\e[0m" ;; green) echo -e "\e[0;32m${@}\e[0m" ;; yellow) echo -e "\e[0;33m${@}\e[0m" ;; blue) echo -e "\e[0;34m${@}\e[0m" ;; purple) echo -e "\e[0;35m${@}\e[0m" ;; cyan) echo -e "\e[0;36m${@}\e[0m" ;; *) echo -e "\e[0;37m${@}\e[0m" ;; esac # --- end of case --- } #======================================================================= #檢查安裝包、腳本、my.cnf是否齊全 #======================================================================= function chk_install_resource() { #判斷 template_install-my.cnf 是否存在 if [ ! -f "$MYCNF" ];then echo_color red "$(date +'%Y-%m-%d %H:%M:%S') $MYCNF file is not exits!$(echo_warning)" exit 1 fi #判斷 MySQL Community Server 5.7.18 tar包是否存在 if [ ! -f "$MYSQL_SOURCE_PACKAGES" ];then echo_color red "$(date +'%Y-%m-%d %H:%M:%S') $MYSQL_SOURCE_PACKAGES is not exits, please download it from $MYSQL_DOWNLOAD_LINK" exit 1 fi } #======================================================================= # 添加賬號和目錄 #======================================================================= function create_sys_user() { #添加mysql用戶信息 if id mysql &> /dev/null;then echo_color red "$(date +'%Y-%m-%d %H:%M:%S') MySQL user is exits." else useradd -r -s /bin/false mysql && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') The system user is added to success .." fi if [ ! -d "${MYSQL_DATADIR}" ];then mkdir -p ${MYSQL_DATADIR} && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') MySQL data directory is created .." chown -R mysql:mysql ${MYSQL_DATADIR} chmod 750 ${MYSQL_DATADIR} elif [ "$(ls -A ${MYSQL_DATADIR})" = "" ];then echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') MySQL data directory is exits." else echo_color red "$(date +'%Y-%m-%d %H:%M:%S') MySQL data directory is not empty. Please check it." exit 1 fi } #======================================================================= #檢查是否有舊的mysql/mariadb版本存在 #======================================================================= function chk_old_mysql_version() { mysqlNum=$(rpm -qa | grep -Ei 'mysql|mariadb'|wc -l) if [ "${mysqlNum}" -gt "0" ];then echo_color red "$(date +'%Y-%m-%d %H:%M:%S') The system has MySQL other version. There may be a conflict in the version!If it continues, the original database will be uninstall." read -p "Do you continue to install it(y/n):" cn case $cn in y|Y) rpm -qa | grep -Ei 'mysql|mariadb' | xargs yum remove -y &> /dev/null source /etc/profile tar_install ;; n|N) exit 1 ;; *) echo_color red "$(date +'%Y-%m-%d %H:%M:%S') Input ERROR." esac else echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') No old version was found." source /etc/profile tar_install fi } #======================================================================= # 解壓安裝 #======================================================================= function installPackage() { count=0 package=(gcc gcc-c++ bzip2 bzip2-devel bzip2-libs python-devel libaio libaio-devel ncurses ncurses-devel cmake numactl-libs) nums01=${#package[@]} for((i=0;i<nums01;i++)); do char=${package[$i]} rpm -qa | grep "^$char" if [ $? != 0 ] ; then error[$count]=${package[$i]} count=$(($count+1)) echo_color red "$(date +'%Y-%m-%d %H:%M:%S') The ${package[$i]} is not installed.Please check it.." fi done if [ $count -gt "0" ];then echo "You have $count patchs are not installed." echo "the not installed patch is:" nums02=${#error[@]} for((ii=0;ii<nums02;ii++)); do echo "${error[$ii]}^" done echo -e "Are you sure to install the patch[yes or no]:\c" read select if [ $select == "yes" ]; then for((is=0;is<nums02;is++)); do var=${error[$is]} echo $var yum install -y $var done fi else echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Check pass!.." fi count=0 rpm -q gcc gcc-c++ bzip2 bzip2-devel bzip2-libs python-devel libaio libaio-devel ncurses ncurses-devel cmake numactl-libs | grep "not installed" } function tar_install() { installPackage echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Starting unzip $MYSQL_SOURCE_PACKAGES .." tar zxvf $MYSQL_SOURCE_PACKAGES -C /usr/local/ echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Unzip $MYSQL_SOURCE_PACKAGES SUCCESS .." if [ ! -d "/usr/local/mysql" ];then ln -s /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} /usr/local/mysql chown -R mysql:mysql /usr/local/mysql chown -R mysql:mysql /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} chmod 750 /usr/local/mysql chmod 750 /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') MySQL package has been placed in the right position .." cp -f $MYCNF /etc/my.cnf else read -p "/usr/local/mysql install directory already exists, delete it, and continue(y/n):" dn case $dn in y|Y) rm -rf /usr/local/mysql ln -s /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} /usr/local/mysql chown -R mysql:mysql /usr/local/mysql chown -R mysql:mysql /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} chmod 750 /usr/local/mysql chmod 750 /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} echo_color cyan "