通过Shell脚本批量创建服务器上的MySQL数据库账号
程序员文章站
2023-02-17 17:34:10
1.项目背景
因监控需要,我们需要在既有的每个mysql实例上创建一个账号。公司有数百台 mysql 实例,如果手动登入来创建账号很麻烦,也不现实。所以,我们写了一个简单...
1.项目背景
因监控需要,我们需要在既有的每个mysql实例上创建一个账号。公司有数百台 mysql 实例,如果手动登入来创建账号很麻烦,也不现实。所以,我们写了一个简单的shell脚本,用来创建批量服务器的mysql 账号。
2.执行脚本内容;
#!/bin/bash ## 此段shell 脚本的主要功能是实现在多个sql server ip实例上,创建账号。输入参数是两个,第一个是数据库所在的ips,即多个server ip构成的字符串,ip间用逗号隔开。第二个参数是 端口(3306 或 3307) ##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
总结
以上所述是小编给大家介绍的通过shell脚本批量创建服务器上的mysql数据库账号 ,希望对大家有所帮助
上一篇: 如何让vsCode显示中文界面