Script sharing for exporting user permission settings in MySQL _ MySQL

Source: Internet
Author: User
This article mainly introduces the script for exporting user permission settings in MySQL. to set the export permission for data in the user table, you can refer to the user and permission in the source database when migrating the MySQL 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

The code is as follows:


[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

The code is as follows:


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

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

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.