您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 平凉分类信息网,免费分类信息发布

MySQL中导出用户权限设置的脚本分享

2024/2/21 12:29:51发布16次查看
在对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 需要注意:
a、目标服务上为非空服务器,已经存在一些账户及权限应考虑会覆盖的问题。
b、如果仅仅需要迁移非root用户,可以在原脚本中添加过滤条件,即 where user'root' 。
平凉分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录