mysql 安装卸载自动化脚本
#!/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 ];then return 0 else return 1 fi } menu(){ clear language if [ $? -eq 0 ];then echo " ##############----Menu----##############" echo "# 1. 安装mysql" echo "# 2. 修改数据库的初始密码" echo "# 3. 配置数据库" echo "# 4. 卸载数据库" echo "# 5. 退出" 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 "请选择一个菜单[1-5]:" select else read -p "Please choice a menu[1-5]:" select fi } install_mysql(){ language echo "请上传数据库安装包" rz -E mkdir /root/mysql tar -xvf mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar -C /root/mysql cd /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 systemctl restart mysqld if [ $? -eq 0 ] then echo "修改默认编码为utf8成功" sleep 3 else echo $? exit fi } 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