前言:在某些特殊的狀況下,咱們但願能拿到全量的mysql受權表信息,手動操做比較麻煩,這時候就須要一個腳本mysql
一、備份用戶權限的腳本sql
mysql version<5.7:bash
[mysql@gz-mysql-offline02 ~]$ cat /home/mysql/scripts/mysql_exp_grant.sh #/bin/bash #Function export user privileges pwd=123456 port=3306 expgrants() { /usr/bin/mysql -B -u'root' -p${pwd} -h'127.0.0.1' -P${port} -N $@ -e "SELECT CONCAT('SHOW GRANTS FOR ''', user, '''@''', host, ''';') AS query FROM mysql.user" | \ /usr/bin/mysql -u'root' -p${pwd} -h'127.0.0.1' -P${port} $@ | \ sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 /;/--/{x;p;x;}' } expgrants > ./grants.sql
mysql version>=5.7:服務器
[mysql@gz-mysql-offline00 scripts]$ cat mysql_exp_grant.sh #/bin/bash #Function export user privileges pwd=123456 port=3306 expgrants() { mysql -B -u'root' -p${pwd} -h'127.0.0.1' -P${port} -N $@ -e "SELECT CONCAT( 'SHOW CREATE USER ''', user, '''@''', host, ''';' ) AS query FROM mysql.user" | \ mysql -u'root' -p${pwd} -h'127.0.0.1' -P${port} -f $@ | \ sed 's#$#;#g;s/^\(CREATE USER for .*\)/-- \1 /;/--/{x;p;x;}' mysql -B -u'root' -p${pwd} -h'127.0.0.1' -P${port} -N $@ -e "SELECT CONCAT( 'SHOW GRANTS FOR ''', user, '''@''', host, ''';' ) AS query FROM mysql.user" | \ mysql -u'root' -p${pwd} -h'127.0.0.1' -P${port} -f $@ | \ sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 /;/--/{x;p;x;}' } expgrants > ./grants.sql
二、生成的sql語句(摘自參考)spa
[root@HKBO ~]# head grants.sql -- Grants for root@127.0.0.1 GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY PASSWORD '*EB3EA446C759C9DA93F84FCB56430DBEF051A9DD' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON `CNBO0815`.* TO 'root'@'127.0.0.1' WITH GRANT OPTION; -- Grants for root@172.16.10.% GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.10.%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'; -- Grants for CNBO@192.168.1.% GRANT USAGE ON *.* TO 'CNBO'@'192.168.1.%' IDENTIFIED BY PASSWORD '*ABD91BAD4A3448428563952E281015B237310EA8';
三、恢復用戶權限.net
將生成的腳本在目標服務器上執行便可。 mysql -uname -ppwd <grants.sql
須要注意:
a、目標服務上爲非空服務器,已經存在一些帳戶及權限應考慮會覆蓋的問題。
b、若是僅僅須要遷移非root用戶,能夠在原腳本中添加過濾條件,即 where user<>'root' 。code
原文:https://blog.csdn.net/leshami/article/details/40106401blog
#/bin/bash #auth: tom #time: 2018-04-16 #function: count mysql connections while true do netstat -an|grep -w 10.0.0.8:3306|awk -F '[ ]+' '{print $5}'|awk -F '[:]' '{print $1}'|sort|uniq >>./netstat.log sleep 1 done
#/bin/bash #auth: tom #time: 2018-04-28 #function: count mysql processlist while true do echo "+--------+------------------+-------------------+--------------------+---------+--------+--------------------------------------------------------+------------------+" >>./process_list.log mysqladmin -utest -p'test123' -h10.0.0.8 -P3306 processlist|grep confluencedb >>./process_list.log sleep 1 done