zoukankan      html  css  js  c++  java
  • 通过Shell脚本来创建批量服务器上的MySQL数据库账号

     1.项目背景

    因监控需要,我们需要在既有的每个MySQL实例上创建一个账号。公司有数百台 MySQL 实例,如果手动登入来创建账号很麻烦,也不现实。所以,我们写了一个简单的shell脚本,用来创建批量服务器的mysql 账号。

     2.执行脚本内容;

    #!/bin/bash
    ## 此段shell 脚本的主要功能是实现在多个SQL Server IP实例上,创建账号。输入参数是两个,第一个是数据库所在的IPs,即多个Server IP构成的字符串,IP间用逗号隔开。第二个参数是 端口(33063307)
    ##MySQL程序所在路径--mysql bin 文件所在路径;如果由建立软连接,可直接是mysql
    command_linebin="/data/mysql5720/bin/mysql"
    ##用来连接MySQ的账号和密码
    username="DBA_MYSQLACC"
    password="DBAACCTEST109211706DF"
    ## 新创建的账号和密码
    createuid="testuid"
    createpwd="testpwd"
    ##指定新创建的用户在那个主机上可以登录,如果是本地用户可用localhost;如果指定规则的可以使用通配符%
    phost="177.177.%"
    
    mysqlserverIPs=$1
    echo $mysqlserverIPs
    ## 按“,”分割,将字符串转换为数组。
    IParr=(${mysqlserverIPs//,/ })
    echo $IParr
    
    for ((i=0;i<${#IParr[@]};i++))
    do
       IP=${IParr[$i]}
       echo "${IP}"
    select_sql="select * from user where user="$createuid""
    msg=$(${command_linebin} -h ${IP}  -P $2 -u$username -p$password -s mysql -e "${select_sql}")
    
    echo $msg
    
    ##创建账号前,先检查需要创建的账号是否已经存在,如果已经存在了,则直接退出。
    if [[ $msg = "" ]] ;then
       echo $(date -d today +"%Y%m%d%H%M%S")  $mip  "The Condition is OK,permit to create UID."
    else
        echo $(date -d today +"%Y%m%d%H%M%S")  $IP  "The UID you want create has been exited, please check it! The Act Quit!"
        exit
     fi
    
    ## 以下几行代码是创建的关键
    ${command_linebin} -h ${IP} -P $2  -u$username -p$password -s mysql <<EOF
    
    CREATE USER '$createuid'@'$phost' IDENTIFIED BY '$createpwd';
    
    
    GRANT Select,PROCESS ON *.* TO '$createuid'@'$phost';
    
    
    flush privileges;
    
    
    EOF
    
    ##创建后,再次检查账号看否已将存在。如果不存在,则说明创建失败,直接退出。
    select_sql="select * from user where user="$createuid""
    msg=$(${command_linebin} -h ${IP}  -P $2 -u$username -p$password -s mysql -e "${select_sql}")
    echo $msg
    
    
    if [[ $msg = "" ]] ;then
       echo $(date -d today +"%Y%m%d%H%M%S")  ${IP}  "The action to create UID Error,Please Check it! The Act Quit! "
       exit
    else
        echo $(date -d today +"%Y%m%d%H%M%S")  ${IP}  "Congratulation! Create UID successful"
     fi
    
    
    done

    3. 执行举例

    Step 1 将代码放置到执行文件中,可执行文件命名为 mysql_CreateUIDMulti.sh

    Step 2 请对此文件授予可执行权限,否则,提示以下错误。

    Step 3 执行的具体命令(参数格式),例如 在 177.177.XXX.128,177.177.XXX.144 两个 3306的实例上创建账号

    ./mysql_CreateUIDMulti.sh 177.177.XXX.128,177.177.XXX.144 3306

    Step 4 打印的执行结果如下

    177.177.XXX.128,177.177.XXX.144
    177.177.XXX.128
    177.177.XXX.128
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    20180529171802 The Condition is OK,permit to create UID.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    177.177.% testuid Y N N N N N N N Y N N N N N N N N N N N N N N N N N N N N 0 0 0 0 mysql_native_password *22CBF14EBDE8814586FF12332FA2B6023A7603BB N 2018-05-29 17:18:02 NULL N
    20180529171802 177.177.XXX.128 Congratulation! Create UID successful
    177.177.XXX.144
    mysql: [Warning] Using a password on the command line interface can be insecure.
    177.177.% testuid Y N N N N N N N Y N N N N N N N N N N N N N N N N N N N N 0 0 0 0 mysql_native_password *22CBF14EBDE8814586FF12332FA2B6023A7603BB N 2018-05-30 00:56:38 NULL N
    20180529171802 177.177.XXX.144 The UID you want create has been exited, please check it! The Act Quit!

    4.补充说明

    如果创建一个服务器上的MySQL账号,可按照以下格式

    ./mysql_CreateUIDMulti.sh 177.177.XXX.128 3306

    打印的Log 如下

    177.177.XXX.128
    177.177.XXX.128
    177.177.XXX.128
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    20180529173517 The Condition is OK,permit to create UID.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    177.177.% testuid Y N N N N N N N Y N N N N N N N N N N N N N N N N N N N N 0 0 0 0 mysql_native_password *22CBF14EBDE8814586FF12332FA2B6023A7603BB N 2018-07-29 17:35:17 NULL N
    20180529173517 177.177.XXX.128 Congratulation! Create UID successful

    本文版权归作者所有,未经作者同意不得转载,谢谢配合!!!

    本文版权归作者所有,未经作者同意不得转载,谢谢配合!!!

    本文版权归作者所有,未经作者同意不得转载,谢谢配合!!!

  • 相关阅读:
    [Python] 登录人人网2011版
    [WPF] 自定义窗体样式
    [Python] 控制台输入密码的方法
    [Python] 字符串加密解密
    [WPF] 模仿AMD LIVE! EXPLORER界面
    [Python] Visual Studio 2008 集成 IronPython 开发环境
    mysql表类型(存储引擎)
    logstash收集nginx访问日志
    linux设置开机启动脚本
    logstash安装log4j插件
  • 原文地址:https://www.cnblogs.com/xuliuzai/p/11265051.html
Copyright © 2011-2022 走看看