#!/bin/sh #mkdir /root/mysql #tar -xvf mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar -C /root/mysql #cd /root/mysql #yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes #rpm -Uvh mysql-community-*.rpm #systemctl start mysqld language(){ echo $LANG |grep -q zh if [ $? -eq 0 ]; the then return 0 the else return . 1 Fi } MENU () { Clear Language IF [$ -eq? 0 ]; the then echo " ############## ---- Menu-- - ############## " echo " # 1. installation MySQL " echo " # 2. modify database initial password " echo " # 3. configuration database " echo " # 4. uninstall database " echo " # 5. quit " echo " ########################################" else echo " ##############----Menu----##############" echo "# 1. Install mysql" echo "# 2. change mysql first password" echo "# 3. configure mysql" echo "# 4. remove MySQL" echo "# 5. exit" echo " ########################################" fi } choice(){ language if [ $? -eq 0 ];then read -p "Please select a menu [1-5]: " SELECT the else Read -p " Please Choice A MENU [1-5]: " SELECT Fi } install_mysql () { Language echo " Please upload installation package Database " Rz - E mkdir / the root / MySQL the tar -xvf MySQL- 5.7 . 25 - . 1 . .el7.x86_64.rpm the bundle- the tar -C / the root / MySQL CD / the root / MySQL RM -rf MySQL-Community-devel- 5.7 .25-1.el7.x86_64.rpm mysql-community-embedded-*.rpm mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm mysql-community-test-5.7.25-1.el7.x86_64.rpm yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes net-tools rpm -Uvh mysql-community-*.rpm systemctl start mysqld if [ $? -eq 0 ] then echo "安装成功" else echo $? exit fi } change_password(){ language PORT="3306" USERNAME="root" PASSWORD=$(cat /var/log/mysqld.log |grep 'temporary password' | awk -F: '{print $4}' | sed s/[[:space:]]//g) echo $PASSWORD mysql_conn="mysql -P${PORT} -u${USERNAME} --connect-expired-password --password=${PASSWORD}" echo $mysql_conn NEWPASS="1234" # sql="alter mysql.user 'root'@'localhost' identified by \"${NEWPASS}\";flush privileges;" # echo $sql $mysql_conn -e "set global validate_password_policy=0;" $mysql_conn -e "set global validate_password_length=4;" $mysql_conn -e "set password for 'root'@'localhost'=password('1234');flush privileges;" if [ $? -eq 0 ] then echo "修改密码成功" else echo $? exit fi } configure_mysql(){ cp /etc/my.cnf /etc/my.cnf.default sed -i '3a\[client]\t\ndefault-character-set=utf8 ' /etc/my.cnf sed -i '/\[mysqld\]/a\character-set-server=utf8\nvalidate_password_length=4\nvalidate_password_policy=0 ' /etc/my.cnf mysqld restart systemctl IF [? $ -eq 0 ] the then echo " modify the default encoding is utf8 success " SLEEP 3 the else echo $? Exit fi the User = " root " passwd = " 1234 " SQL = " Show the Variables like% char%; " A = $ MySQL -u -p User password -e $ " $ SQL " echo $ A } remove_mysql () { Language systemctl stop mysqld mysql_pack=$(rpm -qa|grep mysql) for i in $mysql_pack do rpm -e ${i} --nodeps done rm -rf /var/lib/mysql rm -rf /usr/bin/mysql rm -rf /usr/lib64/mysql rm -rf /usr/share/mysql rm -rf /usr/include/mysql rm -rf /var/log/mysqld.log if [ $? -eq 0 ] then echo "卸载成功" else echo $? exit fi } while true do menu choice case $select in 1) install_mysql ;; 2) change_password ;; 3) configure_mysql ;; 4) remove_mysql ;; 5) quit;break ;; esac done