mysql创建用户授权shell
#!/bin/bash declare -A chengshike declare -A jinrizhuan declare -A dazui declare -A fengkuangshiwan declare -A ceshiku ceshiku=( ["host"]="地址" ["user"]="账号" ["passwd"]="密码" ) chengshike=( ["host"]="地址" ["user"]="账号" ["passwd"]="密码" ) jinrizhuan=( ["host"]="地址" ["user"]="账号" ["passwd"]="密码" ) dazui=( ["host"]="地址" ["user"]="账号" ["passwd"]="密码" ) fengkuangshiwan=( ["host"]="地址" ["user"]="账号" ["passwd"]="密码" ) #menu=(chengshike jinrizhuan dazui fengkuangshiwan) itemname=$1 user=$2 passwd=`date +%s |sha256sum |base64 |head -c 10 ;echo` command_linebin="/usr/bin/mysql" function monitor() { if [ "${itemname}" = "chengshike" ];then echo "${chengshike[host]}" hosts=${chengshike[host]} users=${chengshike[user]} passwds=${chengshike[passwd]} mysql elif [ "${itemname}" = "jinrizhuan" ];then echo "${jinrizhuan[host]}" hosts=${jinrizhuan[host]} users=${jinrizhuan[user]} passwds=${jinrizhuan[passwd]} mysql elif [ "${itemname}" = "dazui" ];then echo "${dazui[host]}" hosts=${dazui[host]} users=${dazui[user]} passwds=${dazui[passwd]} mysql elif [ "$itemname" = "fengkuangshiwan" ];then echo "${fengkuangshiwan[host]}" hosts=${fengkuangshiwan[host]} users=${fengkuangshiwan[user]} passwds=${fengkuangshiwan[passwd]} mysql elif [ "${itemname}" = "ceshiku" ];then echo "${ceshiku[host]}" hosts=${ceshiku[host]} users=${ceshiku[user]} passwds=${ceshiku[passwd]} mysql else echo "1" fi } mysql() { usertest="SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$user')" msg=$(${command_linebin} -h $hosts -u$users -p$passwds -s mysql -e "${usertest}") echo $msg #usertest=$(mysql -h $hosts -u$users -p$passwds -e "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = \"$user\")") if [ "$msq" = 1 ];then echo "$user 已存在" exit else echo "开始创建用户并授权" ${command_linebin} -h $hosts -u$users -p$passwds -s mysql <<EOF create user '$user'@'%' identified by '$passwd'; GRANT Select ON *.* TO '$user'@'%'; flush privileges; EOF check fi } check() { usertest="SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$user')" msg=$(${command_linebin} -h $hosts -u$users -p$passwds -s mysql -e "${usertest}") if [[ "$msg" = "" ]];then echo "创建失败" exit else echo "创建成功" echo "$user $passwd " >>/root/mysqluser.txt fi } monitor