Export MySQL user permissions
When migrating a MySQL database, you sometimes need to migrate the users and permissions in the source database. For this migration, we can obtain the user's related permissions from the mysql. user table to generate corresponding SQL statements, and then execute the generated SQL statements on the target server. This article provides a script for extracting User Permissions and a demonstration.
1. Generate a script for User Permissions
[Root @ HKBO ~] # More exp_grant.sh
#! /Bin/bash
# Function export user privileges
Pwd = 1, 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. * \)/-- \ 1/;/--/{x; p; x ;}'
}
Expgrants>./grants. SQL
2. Generate a permission SQL script
[Root @ HKBO ~] #./Exp_grant.sh
[Root @ HKBO ~] # Head grants. SQL
-- Grants for root@127.0.0.1
Grant all privileges on *. * TO 'root' @ '192. 0.0.1 'identified by password' * eb3ea446c759c9da93f84fcb561_dbef051a9dd' with grant option;
Grant all privileges on 'cnbo0815'. * TO 'root' @ '2017. 0.0.1 'with grant option;
-- Grants for root@172.16.10. %
Grant all privileges on *. * TO 'root' @ '192. 172.% 'identified by password' * 6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 ';
-- Grants for CNBO@192.168.1. %
Grant usage on *. * TO 'cnbo' @ '192. 192.% 'identified by password' * ABD91BAD4A3448428563952E281015B237310EA8 ';
...........................
-- Author: Leshami
-- Blog:
MySQL authoritative guide (original book version 2nd) Clear Chinese scan PDF
Ubuntu 14.04 LTS install LNMP Nginx \ PHP5 (PHP-FPM) \ MySQL
Build a MySQL Master/Slave server in Ubuntu 14.04
Build a highly available distributed MySQL cluster using Ubuntu 12.04 LTS
Install MySQL5.6 and Python-MySQLdb in the source code of Ubuntu 12.04
MySQL-5.5.38 universal binary Installation
-------------------------------------- Split line --------------------------------------
This article permanently updates the link address: