多例項MySQL批量新增使用者和密碼並授權

haoge0205發表於2014-09-01
OS:Cent OS 6.3

DB:5.5.14

如果一臺伺服器上有100個MySQL資料庫例項,此時需要給不同埠的幾個資料庫新增使用者名稱、密碼並授權應用IP,一個個授權沒問題,反正累的不是我,哇咔咔。


建立add_user.sh


#/bin/bash
#add user to mysql db


passpwd="yoon"


cat user.txt | while read user
do
  port=`echo $user |awk '{print $1 }'`
  username=`echo $user |awk '{print $2 }'`
  pwd=`echo $user |awk '{print $3 }'`
  db=`echo $user |awk '{print $4 }'`
#  echo 'port='$port';username='$username';pwd='$pwd';db='$db


  /export/servers/mysql/bin/mysql -S /export/data/mysql/tmp/mysql$port.sock -uroot -p$passpwd <


use mysql;


GRANT  SELECT,INSERT,UPDATE,DELETE ON $db.* TO '$username'@'192.17.12.2' IDENTIFIED BY '$pwd';
GRANT  SELECT,INSERT,UPDATE,DELETE ON $db.* TO '$username'@'192.17.12.3' IDENTIFIED BY '$pwd';
GRANT  SELECT,INSERT,UPDATE,DELETE ON $db.* TO '$username'@'192.17.12.4' IDENTIFIED BY '$pwd';


flush privileges;


EOF



done



建立user.txt

2013     yoon_rw     WY811JnJTzcjjfZO..Ui_8Q8i8k09kX  yoon
2014     yoon_rw     WY811JnJTzcjjfZO..Ui_8Q8i8k10kX  yoon
2015     yoon_rw     WY811JnJTzcjjfZO..Ui_8Q8i8k11kX  yoon
2016     yoon_rw     WY811JnJTzcjjfZO..Ui_8Q8i8k12kX  yoon
2017     yoon_rw     WY811JnJTzcjjfZO..Ui_8Q8i8k13kX  yoon
2018     yoon_rw     WY811JnJTzcjjfZO..Ui_8Q8i8k14kX  yoon
2020     yoon_rw     WY811JnJTzcjjfZO..Ui_8Q8i8k15kX  yoon


[root@yoon ~]# sh  add_user.sh

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-1261471/,如需轉載,請註明出處,否則將追究法律責任。

相關文章