Script sharing for exporting User permission settings in MySQL, mysql user permissions

Source: Internet
Author: User
Tags mysql command line

Script sharing for exporting User permission settings in MySQL, 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

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

Copy codeThe 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 <grants. SQL

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


How does MYSQL set user permissions?

Grant all privileges on *. * to root @ '%' identified by '000000' with grant option;
Implement this. If it is normal, there will be no errors.
The basic knowledge of Mysql can be viewed here

Mysql permission settings

Use the grant statement to create a user in the mysql command line.

Grant permission on database name. database table to username @ '%' identified by 'userpwd '.

You can
Grant all on dbname. table0 to 'username' @ '%' identified by 'pwd ';
Grant all on dbname. table1 to 'username' @ '%' identified by 'pwd ';
......
......

In some databases
Grant all on dbname, table0, dbname. table1 to 'username' @ '% 'identified by 'pwd ';
Or
Grant all on dbname, table0 dbname. table1 to 'username' @ '% 'identified by 'pwd ';

However, mysql does not seem to work. At least in 5.0.41, I have never understood how to do this before I can grant many specified tables to an account in a single statement, so I had to separate them.

Related Article

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.