mysql 安装卸载自动化脚本

Posted liucsxiaoxiaobai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
    user="root"
        passwd="1234"
        sql="show variables like %char%;"
        a=mysql -u$user -p$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

 

以上是关于mysql 安装卸载自动化脚本的主要内容,如果未能解决你的问题,请参考以下文章

安装mysql8 -shell脚本自动安装

Saltstack+Shell自动化分发脚本

Windows IIS 使用批处理脚本自动安装与卸载

linux (二十五)编写自动化脚本安装DNS单机服务主从服务和卸载服务,以及网卡配置

自动化运维——一键安装MySQL

如何让安卓手机开机自动执行某一脚本