MySQL通过shell脚本工具自动添加用户

MySQL通过脚本工具自动添加用户

#!/bin/bash
export admin_user=''
export admin_passwd=''
export host_ip=''
export add_privilege=''
export add_user=''
export add_user_passwd=''
export dbename=''

getAdmin_user() {
    read -p "MySQL Manager user : " admin_user
    export admin_user
}

getAdmin_passwd() {
    read -s -p "admin_passwd: " admin_passwd
    export admin_passwd
}

getHost_IP() {
    echo
    echo " 测试环境:192.168.1.101 "
    echo " 生产环境:192.168.2.102 "
    read -p "host_ip : " host_ip
    export host_ip
}

getAdd_privilege() {
    read -p "add_privilege like select,insert,update,delete: " add_privilege
    export add_privilege
}

getAdd_user() {
    read -p "add_user: " add_user
    export add_user
}

getAdd_user_passwd() {
    read -p "add_user_passwd: " add_user_passwd
    export add_user_passwd
}

getDBname() {
    read -p "dbname: " dbname
    export dbname
}

getDBname() {
    read -p "dbname: " dbname
    export dbname
}

getAdmin_user
getAdmin_passwd
getHost_IP
getAdd_privilege
getDBname
getAdd_user
getAdd_user_passwd

mysql  -h$host_ip -u$admin_user -p$admin_passwd -e "grant $add_privilege privileges on $dbname.* to $add_user@'%' Identified by '$add_user_passwd' "

if [ $? -eq 0 ];then
    echo -e "\033[32m Create Database $dbname Success! \033[0m"
else
    echo -e "\033[31m Create Database $dbname Failed! \033[0m"
fi

 

发表评论