Centos使用yum安装Mysql各个版本

实测脚本如下:

(写的不好勿喷!!)

#!/bin/bash
Mysql_repo () {
echo -e "
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql56-community-el7/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools]
name=Mysql Tools Community -el7
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
" > /etc/yum.repos.d/mysql.repo
}
while :; do
    read -r -p "Whether Install Mysql? Y/N (Default y Enter):" YN
    YN=${YN:-y}
    case $YN in
        y|Y)
        Mysql_repo
        echo -e "\n********请选择需要安装的Mysql版本:********"
        echo -e "(1) Install Mysql-5.6.50"
        echo -e "(2) Install Mysql-5.7.32"
        echo -e "(3) Install Mysql-8.0.22"
        break
        ;;
        n|N)
        exit 1
        ;;
        *)
        echo -e "\033[31m [Error] Please Input y/n\033[0m"
        ;;
    esac
done
while :; do
    read -r -p "请输入要安装的Mysqk版本:" MV
    case $MV in
        1)
        sed -i "s/enabled=0/enabled=1/g" /etc/yum.repos.d/mysql.repo
        sed -i "1,/enabled=1/s/enabled=1/enabled=0/" /etc/yum.repos.d/mysql.repo
        yum install mysql-community-server -y
        systemctl start mysqld && systemctl enable mysqld
        read -r -p "Please input Mysql Password:" PaD
        mysqladmin password "${PaD}"
        DB_V=$(mysql -V |awk '{print $5}'|tr -d ',')
        echo -e "\033[32m DB_Version:${DB_V}\n DB_User:root\n DB_Passwd:${PaD}\033[0m"
        break  
        ;;
        2)
        sed -i "s/enabled=0/enabled=1/g" /etc/yum.repos.d/mysql.repo
        sed -i "0,/enabled=1/s/enabled=1/enabled=0/" /etc/yum.repos.d/mysql.repo        
        yum install mysql-community-server -y
        systemctl start mysqld && systemctl enable mysqld
        Password=$(echo |grep "temporary password" /var/log/mysqld.log | cut -f 4 -d ":"|cut -f 2 -d " ")
        mysql --connect-expired-password -uroot -p"${Password}" -e "set password=password('${Password}');"
        DB_V=$(mysql -V |awk '{print $5}'|tr -d ',')
        echo -e "\033[32m DB_Version:${DB_V}\n DB_User:root\n DB_Passwd:${Password}\033[0m"
        break
        ;;
        3)
        sed -i "s/enabled=0/enabled=1/g" /etc/yum.repos.d/mysql.repo
        yum install mysql-community-server -y
        systemctl start mysqld && systemctl enable mysqld
        Password=$(grep "temporary password" /var/log/mysqld.log | cut -f 4 -d ":"|cut -f 2 -d " ")
        mysql --connect-expired-password -uroot -p"${Password}" -e "set password=password('${Password}');" ||
        mysqladmin -uroot -p"${Password}" password "${Password}" ||
        mysql --connect-expired-password -uroot -p"${Password}" -e "alter user 'root'@'localhost' identified by '${Password}';"
        DB_V=$(mysql -V |awk '{print $3}')
        echo -e "\033[32m DB_Version:${DB_V}\n DB_User:root\n DB_Passwd:${Password}\033[0m"
        break
        ;;
    esac
done

  

posted @ 2021-01-13 16:20  青柠-青柚  阅读(118)  评论(0)    收藏  举报