• 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
  • 相关阅读:
    java中的HMAC-SHA1加密
    java拦截处理System.exit(0)
    使用canal分析binlog(二) canal源码分析
    JS的异步世界
    socket.io的用户认证
    一个补零小函数
    使用gulp在开发过程中合理导出zip文件
    使用r.js进行前端repuirejs的合并压缩
    使用r2d3的注意事项
    三列自适应布局的实现方式(兼容IE6+)
  • 原文地址:https://www.cnblogs.com/Carr/p/13219119.html
Copyright © 2020-2023  润新知