#!/usr/bin/bash source ./color.sh # 检查执行用户 if [ $UID -ne 0 ];then red "请使用过ROOT用户执行脚本" exit 1 fi # mysql 安装检查 [[ -f /etc/my.cnf ]] || [[ -d /var/lib/mysql ]] || [[ -f /usr/bin/mysql ]] && \ yellow "经检查发现,当前系统曾安装过mysql数据库,是否进行备份后卸载操作[Y|N]" read modify modify=${modify:="N"} if [ $modify = 'Y' ];then systemctl stop mysqld mariadb &>/dev/null yum remove `rpm -qa |grep mysql` yum remove `rpm -qa |grep maraidb` mkdir -p /db-save/{conf,log,data} mv /etc/my.cnf /db-save/conf mv /var/log/mysql* /db-save/log mv /var/log/mariadb* /db-save/log mv /var/lib/mysql/* /db-save/data userdel -r -f mysql yellow "我们为您将数据备份到了/db-save目录下" else green "为了您的数据安全,我们并未进行任何操作" exit 0 fi # 网络检查 ping -w1 -c1 www.baidu.com &>/dev/null if [ $? -ne 0 ] ;then red "网络异常" exit 2 fi # yum源检查 cd /etc/yum.repos.d/ && \ rename .repo .repo.bak *.repo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo yum install -y epel-release yum-utils && \ green "yum源成功安装" green "开始安装mysql yum源" yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm green "请输入安装的版本[80|57|56|55](default:57):" read level green "请输入初始密码:" read -s passwd level=${level:=57} case $level in 80) yum -y install mysql-community-server ;; *) yum-config-manager --disable mysql80-community yum-config-manager --enable mysql${level}-community yum -y install mysql-community-server ;; esac systemctl enable mysqld && systemctl start mysqld && green "mysql安装完成并且成功启动" || red "mysql 安装失败请检查" yellow "mysql 5.6及以下版本没有初始密码" [[ -f /var/log/mysqld.log ]] && mysql_passwd=$(grep password /var/log/mysqld.log| awk '{print $NF}') || red "数据库日志异常,请手动更新初始密码" [[ ! -z mysql_passwd ]] && mysqladmin -p"$mysql_passwd" password "$passwd" && green "密码修改成功:原密码:$mysql_passwd 新密码: $passwd" || red "密码修改出错,请手动修改"