您好,登錄后才能下訂單哦!
小編給大家分享一下怎么通過Shell腳本批量創(chuàng)建服務(wù)器上的MySQL數(shù)據(jù)庫賬號,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
1.項(xiàng)目背景
因監(jiān)控需要,我們需要在既有的每個MySQL實(shí)例上創(chuàng)建一個賬號。公司有數(shù)百臺 MySQL 實(shí)例,如果手動登入來創(chuàng)建賬號很麻煩,也不現(xiàn)實(shí)。所以,我們寫了一個簡單的shell腳本,用來創(chuàng)建批量服務(wù)器的mysql 賬號。
2.執(zhí)行腳本內(nèi)容;
#!/bin/bash ## 此段shell 腳本的主要功能是實(shí)現(xiàn)在多個SQL Server IP實(shí)例上,創(chuàng)建賬號。輸入?yún)?shù)是兩個,第一個是數(shù)據(jù)庫所在的IPs,即多個Server IP構(gòu)成的字符串,IP間用逗號隔開。第二個參數(shù)是 端口(3306 或 3307) ##MySQL程序所在路徑--mysql bin 文件所在路徑;如果由建立軟連接,可直接是mysql command_linebin="/data/mysql5720/bin/mysql" ##用來連接MySQ的賬號和密碼 username="DBA_MYSQLACC" password="DBAACCTEST109211706DF" ## 新創(chuàng)建的賬號和密碼 createuid="testuid" createpwd="testpwd" ##指定新創(chuàng)建的用戶在那個主機(jī)上可以登錄,如果是本地用戶可用localhost;如果指定規(guī)則的可以使用通配符% phost="177.177.%" mysqlserverIPs=$1 echo $mysqlserverIPs ## 按“,”分割,將字符串轉(zhuǎn)換為數(shù)組。 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 ##創(chuàng)建賬號前,先檢查需要創(chuàng)建的賬號是否已經(jīng)存在,如果已經(jīng)存在了,則直接退出。 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 ## 以下幾行代碼是創(chuàng)建的關(guān)鍵 ${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 ##創(chuàng)建后,再次檢查賬號看否已將存在。如果不存在,則說明創(chuàng)建失敗,直接退出。 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. 執(zhí)行舉例
Step 1 將代碼放置到執(zhí)行文件中,可執(zhí)行文件命名為 mysql_CreateUIDMulti.sh
Step 2 請對此文件授予可執(zhí)行權(quán)限,否則,提示以下錯誤。
Step 3 執(zhí)行的具體命令(參數(shù)格式),例如 在 177.177.XXX.128,177.177.XXX.144 兩個 3306的實(shí)例上創(chuàng)建賬號
./mysql_CreateUIDMulti.sh 177.177.XXX.128,177.177.XXX.144 3306
Step 4 打印的執(zhí)行結(jié)果如下
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.補(bǔ)充說明
如果創(chuàng)建一個服務(wù)器上的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腳本批量創(chuàng)建服務(wù)器上的MySQL數(shù)據(jù)庫賬號”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。