#!/bin/bash # by newrain # time 2019-11-28 # lv 2.0 password="QianFeng@123" remote_user="remote" remote_passwd="QianFeng@123" unset c color(){ declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34) #echo -e "\033[${c[$1]}m[`date +%T`]($1) $2\033[0m" printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2" sleep 0.5 } color Info "此脚本用于yum 安装mysql" color Warning "此脚本将删除所有mysql数据,如需备份请ctrl+c终止脚本,程序将在3秒后执行" sleep 3 if [[ $UID -ne 0 ]];then color Warning "使用root 执行此脚本" exit 1 fi color Warning "清理环境" systemctl stop mysqld mariadb &>/dev/null yum erase -y `rpm -qa |grep mariadb` 2>/dev/null yum erase -y `rpm -qa |grep mysql` 2>/dev/null rm -rvf /etc/my.cnf /var/lib/mysql /var/log/mysql* userdel -rf mysql &>/dev/null ping -c1 -w1 www.baidu.com &>/dev/null if [[ $? -eq 0 ]];then yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm else color Error "网络错误" exit 22 fi if [ -z "$level" ];then echo -e "使用此脚本:curl https://gitea.beyourself.org.cn/newrain001/shell-project/raw/branch/master/os/get-mysql.sh | level=5.7 sh\n版本号可选 5.6 5.7 8.0" exit elif [ "$level" == "5.7" ];then yum -y install mysql-community-server mysql-community-devel --disablerepo mysql80-community --enablerepo mysql57-community --nogpgcheck elif [ "$level" == "8.0" ];then yum install -y mysql-community-server --nogpgcheck elif [ "$level" == "5.6" ];then yum -y install mysql-community-server mysql-community-devel --disablerepo mysql80-community --enablerepo mysql56-community --nogpgcheck fi systemctl start mysqld color Warning "启动成功,初始密码如下(mysql5.7前版本没有初始密码)" passwd=$(grep -o 'root@localhost.*' /var/log/mysqld.log | awk 'END{print $NF}') mysqladmin -uroot -p$passwd password $password && \ mysql -uroot -p$password -e "create user \"$remote_user\"@'%' identified by \"$remote_passwd\";"; mysql -uroot -p$password -e "grant all on *.* to \"$remote_user\"@'%'; flush privileges;" color Success "安装完成 初始密码为$password"