Export MySQL user permissions

Source: Internet
Author: User
When migrating a MySQL database, you sometimes need to migrate the users and permissions in the source database. For this migration, we can obtain user-related information from the mysql. user table.

When migrating a MySQL database, you sometimes need to migrate the users and permissions in the source database. For this migration, we can obtain user-related information from the mysql. user table.

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:

3. Execute the script on the target server.

Run the generated script on the target server. Mysql-uname-ppwd

Note:

A. The target service is a non-empty server. Some accounts and permissions should be covered.

B. If you only need to migrate non-root users, you can add filter conditions in the original script, that is, where user <> 'root '.

Refer:

-------------------------------------- Split line --------------------------------------

Install MySQL in Ubuntu 14.04

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 --------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.