Home > Database > Mysql Tutorial > 导出MySQL用户权限_MySQL

导出MySQL用户权限_MySQL

WBOY
Release: 2016-06-01 13:03:52
Original
945 people have browsed it

在对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
Copy after login

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';
         ...........................

--Author : Leshami
--Blog   : http://blog.csdn.net/leshami
Copy after login

3、在目标服务器上执行脚本
将生成的脚本在目标服务器上执行即可。 mysql -uname -ppwd 需要注意:
a、目标服务上为非空服务器,已经存在一些账户及权限应考虑会覆盖的问题。
b、如果仅仅需要迁移非root用户,可以在原脚本中添加过滤条件,即 where user'root' 。

参考:
http://serverfault.com/questions/8860/how-can-i-export-the-privileges-from-mysql-and-then-import-to-a-new-server

鹏城DBA总群

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template