[root@db upload]# more exp_grant.sh #!/bin/bash #Function export user privileges
pwd=123456 expgrants() { mysql -B -u'root' -p${pwd} -N $@ -e "SELECT CONCAT( 'SHOW GRANTS FOR ''', user, '''@''', host, ''';' ) AS query FROM mysql.user" | / mysql -u'root' -p${pwd} $@ | / sed 's//(GRANT .*/)//1;/;s/^/(Grants for .*/)/-- /1 /;/--/{x;p;x;}' } expgrants > ./grants.sql
2、生成权限SQL脚本
[root@db upload]# ./exp_grant.sh
[root@db upload]# cat 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';
...... 3、在目标服务器上执行脚本
将生成的脚本在目标服务器上执行即可。 mysql -uname -ppwd <grants.sql
需要注意:
a、目标服务上为非空服务器,已经存在一些账户及权限应考虑会覆盖的问题。
b、如果仅仅需要迁移非root用户,可以在原脚本中添加过滤条件,即 where user<>'root' 。