在对mysql数据库进行迁移的时候,有时候也需要迁移源数据库内的用户与权限。对于这个迁移我们可以从mysql.user表来获取用户的相关权限来生成相应的sql语句,然后在目标服务器上来执行生成的sql语句即可。本文提供了生成提取用户权限的脚本并给出演示。 1、生
在对mysql数据库进行迁移的时候,有时候也需要迁移源数据库内的用户与权限。对于这个迁移我们可以从mysql.user表来获取用户的相关权限来生成相应的sql语句,然后在目标服务器上来执行生成的sql语句即可。本文提供了生成提取用户权限的脚本并给出演示。
1、生成用户权限的脚本
[root@hkbo ~]# 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@hkbo ~]# ./exp_grant.sh
[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';
...........................
3、在目标服务器上执行脚本
将生成的脚本在目标服务器上执行即可。 mysql -uname -ppwd
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!