1.項(xiàng)目背景
因監(jiān)控需要,我們需要在既有的每個(gè)MySQL實(shí)例上創(chuàng)建一個(gè)賬號(hào)。公司有數(shù)百臺(tái) MySQL 實(shí)例,如果手動(dòng)登入來(lái)創(chuàng)建賬號(hào)很麻煩,也不現(xiàn)實(shí)。所以,我們寫(xiě)了一個(gè)簡(jiǎn)單的shell腳本,用來(lái)創(chuàng)建批量服務(wù)器的mysql 賬號(hào)。
2.執(zhí)行腳本內(nèi)容;
#!/bin/bash
## 此段shell 腳本的主要功能是實(shí)現(xiàn)在多個(gè)SQL Server IP實(shí)例上,創(chuàng)建賬號(hào)。輸入?yún)?shù)是兩個(gè),第一個(gè)是數(shù)據(jù)庫(kù)所在的IPs,即多個(gè)Server IP構(gòu)成的字符串,IP間用逗號(hào)隔開(kāi)。第二個(gè)參數(shù)是 端口(3306 或 3307)
##MySQL程序所在路徑--mysql bin 文件所在路徑;如果由建立軟連接,可直接是mysql
command_linebin="/data/mysql5720/bin/mysql"
##用來(lái)連接MySQ的賬號(hào)和密碼
username="DBA_MYSQLACC"
password="DBAACCTEST109211706DF"
## 新創(chuàng)建的賬號(hào)和密碼
createuid="testuid"
createpwd="testpwd"
##指定新創(chuàng)建的用戶(hù)在那個(gè)主機(jī)上可以登錄,如果是本地用戶(hù)可用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)建賬號(hào)前,先檢查需要?jiǎng)?chuàng)建的賬號(hào)是否已經(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)建后,再次檢查賬號(hào)看否已將存在。如果不存在,則說(shuō)明創(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 請(qǐng)對(duì)此文件授予可執(zhí)行權(quán)限,否則,提示以下錯(cuò)誤。

Step 3 執(zhí)行的具體命令(參數(shù)格式),例如 在 177.177.XXX.128,177.177.XXX.144 兩個(gè) 3306的實(shí)例上創(chuàng)建賬號(hào)
./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ǔ)充說(shuō)明
如果創(chuàng)建一個(gè)服務(wù)器上的MySQL賬號(hào),可按照以下格式
./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
總結(jié)
以上所述是小編給大家介紹的通過(guò)Shell腳本批量創(chuàng)建服務(wù)器上的MySQL數(shù)據(jù)庫(kù)賬號(hào) ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
您可能感興趣的文章:- 監(jiān)控MySQL主從狀態(tài)的shell腳本
- shell腳本一鍵安裝MySQL5.7.29的方法
- mysql常用備份命令和shell備份腳本分享
- shell腳本定時(shí)備份MySQL數(shù)據(jù)庫(kù)數(shù)據(jù)并保留指定時(shí)間
- shell腳本自動(dòng)化創(chuàng)建虛擬機(jī)的基本配置之tomcat--mysql--jdk--maven
- shell腳本實(shí)現(xiàn)mysql定時(shí)備份、刪除、恢復(fù)功能
- 一個(gè)Shell小腳本精準(zhǔn)統(tǒng)計(jì)Mysql每張表的行數(shù)實(shí)現(xiàn)
- 使用shell腳本來(lái)給mysql加索引的方法
- 干掉一堆mysql數(shù)據(jù)庫(kù),僅需這樣一個(gè)shell腳本(推薦)
- 使用shell腳本每天對(duì)MySQL多個(gè)數(shù)據(jù)庫(kù)自動(dòng)備份的講解
- MySQL Shell的介紹以及安裝