Preface
 
    User privileges regulation is pretty important in DBAs routine job.As we all know,it's the less the better to reduce risks when someone who has no privileges manipulates data by malevolence.On the other hand,when we implement HA,replication or the other tools,it's significant and efficient to grant proper privileges to specific users.
 
Introduce
 
    pt-show-grants is the right tool which can directly provide an overview of grants to all users on connected MySQL db server what is rather convenient for us.There're also several advartages of using it.At first,you can dump user grants from one server to another one simply. Secondly,you can organize version control by placing the user grants with it since it will sort the grant statements in order what "show grants;" may not do.Thirdly,now that it provide a normalized format than "show grants;" does,you can distinguish the different user grants between two servers efficiently.
 
Procedure
 
Usage
 pt-show-grants [OPTIONS] [DSN]
Parameters
 //Regular parameters.
--drop //Add "DROP USER" before each user of output,which can be used to get a ddl of droping user.
--flush //Add "FLUSH PRIVILEGES" after output(version ahead 4.1.1 need).
--ignore //Speicify the ignore user.
--only //on the contrary of "--ignore" does,specify the only user you want.
--include-unused-roles //This options merely for MySQL 8.0 + version which support roles.
--seperate //List the grant and revoke statement respectively. //Dump hearder relevant.
--no-header //Don't print head information of dump.
--no-timestamp //Don't add timestam to the head of dump.
Examples
 
Execute without  any parameters.(it will read connection options in defatul my.cnf )
 [root@zlm1 :: ~]
#pt-show-grants
-- Grants dumped by pt-show-grants
-- Dumped from server Localhost via UNIX socket, MySQL 5.7.-log at -- ::
-- Grants for 'bkuser'@'localhost'
CREATE USER IF NOT EXISTS 'bkuser'@'localhost';
ALTER USER 'bkuser'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*3DE5D9E4FBC1E464DA1B1172D6333CE89FDE5C61' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT LOCK TABLES, PROCESS, RELOAD, REPLICATION CLIENT ON *.* TO 'bkuser'@'localhost';
-- Grants for 'mysql.session'@'localhost'
CREATE USER IF NOT EXISTS 'mysql.session'@'localhost';
ALTER USER 'mysql.session'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT LOCK;
GRANT SELECT ON `mysql`.`user` TO 'mysql.session'@'localhost';
GRANT SELECT ON `performance_schema`.* TO 'mysql.session'@'localhost';
GRANT SUPER ON *.* TO 'mysql.session'@'localhost';
-- Grants for 'mysql.sys'@'localhost'
CREATE USER IF NOT EXISTS 'mysql.sys'@'localhost';
ALTER USER 'mysql.sys'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT LOCK;
GRANT SELECT ON `sys`.`sys_config` TO 'mysql.sys'@'localhost';
GRANT TRIGGER ON `sys`.* TO 'mysql.sys'@'localhost';
GRANT USAGE ON *.* TO 'mysql.sys'@'localhost';
-- Grants for 'repl'@'192.168.56.%'
CREATE USER IF NOT EXISTS 'repl'@'192.168.56.%';
ALTER USER 'repl'@'192.168.56.%' IDENTIFIED WITH 'mysql_native_password' AS '*872ECE72A7EBAC6A183C90D7043D5F359BD85A9E' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.56.%';
-- Grants for 'root'@'localhost'
CREATE USER IF NOT EXISTS 'root'@'localhost';
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*DDFB542AA0BD1D251995D81AEBEB96DEEAD1132F' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION;
Specify the only user "root".
 [root@zlm1 :: ~]
#pt-show-grants -hlocalhost -P3306 -uroot -pPassw0rd --only=root
-- Grants dumped by pt-show-grants
-- Dumped from server Localhost via UNIX socket, MySQL 5.7.-log at -- ::
-- Grants for 'root'@'localhost'
CREATE USER IF NOT EXISTS 'root'@'localhost';
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*DDFB542AA0BD1D251995D81AEBEB96DEEAD1132F' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION;
Specify the only user "root" with revoke statement separated with grant statement.
 [root@zlm1 :: ~]
#pt-show-grants -hlocalhost -P3306 -uroot -pPassw0rd --only=root --separate --revoke
-- Grants dumped by pt-show-grants
-- Dumped from server Localhost via UNIX socket, MySQL 5.7.-log at -- ::
-- Revoke statements for 'root'@'localhost'
REVOKE ALL PRIVILEGES ON *.* FROM 'root'@'localhost';
REVOKE GRANT OPTION ON *.* FROM 'root'@'localhost';
REVOKE PROXY ON ''@'' FROM 'root'@'localhost';
REVOKE GRANT OPTION ON *.* FROM 'root'@'localhost';
-- Grants for 'root'@'localhost'
CREATE USER IF NOT EXISTS 'root'@'localhost';
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*DDFB542AA0BD1D251995D81AEBEB96DEEAD1132F' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION;
Specify the only user "root" with drop statement.
 [root@zlm1 :: ~]
# pt-show-grants -hlocalhost -P3306 -uroot -pPassw0rd --only=root --drop
-- Grants dumped by pt-show-grants
-- Dumped from server Localhost via UNIX socket, MySQL 5.7.-log at -- ::
DROP USER 'root'@'localhost';
DELETE FROM `mysql`.`user` WHERE `User`='root' AND `Host`='localhost';
-- Grants for 'root'@'localhost'
CREATE USER IF NOT EXISTS 'root'@'localhost';
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*DDFB542AA0BD1D251995D81AEBEB96DEEAD1132F' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION;
Specify the only user "repl" with no timestamp.
 [root@zlm1 :: ~]
#pt-show-grants -h192.168.56. -P3306 -urepl -prepl4slave --only repl --no-timestamp
-- Grants dumped by pt-show-grants
-- Dumped from server 192.168.56.100 via TCP/IP, MySQL 5.7.-log
-- Grants for 'repl'@'192.168.56.%'
CREATE USER IF NOT EXISTS 'repl'@'192.168.56.%';
ALTER USER 'repl'@'192.168.56.%' IDENTIFIED WITH 'mysql_native_password' AS '*872ECE72A7EBAC6A183C90D7043D5F359BD85A9E' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.56.%';
Specify the only user "repl" with no header.
 [root@zlm1 :: ~]
#pt-show-grants -h192.168.56. -P3306 -urepl -prepl4slave --only repl --no-header
-- Grants for 'repl'@'192.168.56.%' //Only message of annotation this time.
CREATE USER IF NOT EXISTS 'repl'@'192.168.56.%';
ALTER USER 'repl'@'192.168.56.%' IDENTIFIED WITH 'mysql_native_password' AS '*872ECE72A7EBAC6A183C90D7043D5F359BD85A9E' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;
GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.56.%';

Distinguish the difference of user privileges between zlm1 & zlm2 and make it be same.

 //Dump the user grans into a file on server zlm1
[root@zlm1 :: ~]
#pt-show-grants -h192.168.56. -P3306 -urepl -prepl4slave --only repl > repl_grants.sql //Copy the file to zlm2.
[root@zlm1 :: ~]
#scp repl_grants.sql 192.168.56.101:~
repl_grants.sql % .4KB/s : [root@zlm1 :: ~] //Show user infomation on zlm2.
root@localhost:mysql.sock [(none)]>select user,host from mysql.user;
+---------------+--------------+
| user | host |
+---------------+--------------+
| repl | 192.168..% |
| bkuser | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+--------------+
rows in set (0.00 sec) //Show user grants infomation.
root@localhost:mysql.sock [(none)]>show grants for repl@'192.168.56.%';
+---------------------------------------------------------+
| Grants for repl@192.168..% |
+---------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.56.%' |
+---------------------------------------------------------+
row in set (0.00 sec) //Revoke the privileges from repl user to mimic difference.
root@localhost:mysql.sock [(none)]>revoke all on *.* from repl@'192.168.56.%';
Query OK, rows affected (0.00 sec) root@localhost:mysql.sock [(none)]>show grants for repl@'192.168.56.%';
+---------------------------------------------+
| Grants for repl@192.168..% |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'repl'@'192.168.56.%' |
+---------------------------------------------+
row in set (0.00 sec) //Check difference with zlm1.
[root@zlm2 :: ~]
#pt-show-grants -hlocalhost -P3306 -uroot -pPassw0rd --only repl | diff repl_grants.sql -
2c2
< -- Dumped from server 192.168.56.100 via TCP/IP, MySQL 5.7.-log at -- ::
---
> -- Dumped from server Localhost via UNIX socket, MySQL 5.7.-log at -- ::
6c6
< GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.56.%'; //User privileges in dump file.
---
> GRANT USAGE ON *.* TO 'repl'@'192.168.56.%'; //User privileges in local server. [root@zlm2 :: ~]
#mysql -hlocalhost -S /var/lib/mysql/mysql.sock -uroot -pPassw0rd < repl_grants.sql >/dev/null //Import the user grants from dump file. //Check the privileges of user repl again.
[root@zlm2 :: ~]
#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. root@localhost:mysql.sock [(none)]>show grants for repl@'192.168.56.%';
+------------------------------------------------------+
| Grants for repl@192.168..% |
+------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.56.%' | //The privileges are same with zlm1 now.
+------------------------------------------------------+
row in set (0.00 sec)
Summary
  • pt-show-grants helps us dba to manager user privileges better than MySQL command line "show grant for ... ;" does.
  • pt-show-grants is convenient to use even with any options specified.
  • pt-show-grants can be used to check the difference of user privileges between the servers.
  • pt-show-grants is not a intrusive tool like pt-pmp,you can run it at any time you need.

Percona-Tookit工具包之pt-show-grants的更多相关文章

  1. Linux后台开发工具箱

    https://files-cdn.cnblogs.com/files/aquester/Linux后台开发工具箱.pdf 目录 目录 1 1. 前言 3 2. 脚本类工具 3 2.1. sed命令- ...

  2. Mysql: pt-table-checksum 和 pt-table-sync 检查主从一致性,实验过程

    一.安装 percona 包 1.安装仓库的包 https://www.percona.com/doc/percona-repo-config/yum-repo.html sudo yum insta ...

  3. Linux后台开发工具箱-葵花宝典

    Linux后台开发工具箱-葵花宝典 一见 2016/11/4 目录 目录 1 1. 前言 4 2. 脚本类工具 4 2.1. 双引号和单引号 4 2.2. 取脚本完整文件路径 5 2.3. 环境变量和 ...

  4. 推荐几款MySQL相关工具

    前言: 随着互联网技术的不断发展, MySQL 相关生态也越来越完善,越来越多的工具涌现出来.一些公司或个人纷纷开源出一些不错的工具,本篇文章主要介绍几款 MySQL 相关实用工具.提醒下,这里并不介 ...

  5. [知识库分享系列] 二、.NET(ASP.NET)

    最近时间又有了新的想法,当我用新的眼光在整理一些很老的知识库时,发现很多东西都已经过时,或者是很基础很零碎的知识点.如果分享出去大家不看倒好,更担心的是会误人子弟,但为了保证此系列的完整,还是选择分享 ...

  6. SQL慢查询安装过程

    SQL慢查询 基本操作 打开防火墙 firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload 安 ...

  7. 快速安装Percona pt工具

    yum install perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-Time-HiRes perl-IO-Socket-SSLwget http://pk ...

  8. Percona 工具包 pt-online-schema-change 简介

    mysql的在线表结构修改,因为低效和阻塞读写.一直被诟病.至于ALTER TABLE 的原理,参看我上一篇文章.MySQL在线修改大表结构.看完后,发现的问题是还是会锁的,且对于在线更新的这块也是不 ...

  9. 安装percona工具包

    1.安装percona源 sudo yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona- ...

  10. percona pt toolkit 总结

    ##=====================================================##pt-osc之工作流程:1.检查更改表是否有主键或唯一索引,是否有触发器2.检查修改表 ...

随机推荐

  1. vue发布后的一些问题

    1.发布后把config/index.js 的productionSourceMap: true,改成productionSourceMap: false webpack成功隐藏

  2. 《ArcGIS Runtime SDK for Android开发笔记》——(11)、ArcGIS Runtime SDK常见空间数据加载

    ArcGIS Runtime SDK for Android 支持多种类型空间数据源.每一种都提供了相应的图层来直接加载,图层Layer是空间数据的载体,其主要继承关系及类型说明如下图所示: 转载请注 ...

  3. 【邀请函】小投入 大产出—微软智能云(Azure)之CDN 专题

    会议时间 2016 年 11 月 24 日 14:00-16:00 会议介绍 尊敬的客户: 您是否还在为如何提高网站的响应速度以及用户增长造成的源站压力烦恼?是否还在担心源站 IP 暴露存在安全隐患? ...

  4. 初涉node.js做微信测试公众号一路填坑顺便发现个有趣的其他漏洞

    [微信测试公众号] 半年前耍着玩搭起来的“微信简历”,是LAMP版的,很皮毛. 微信的官方文档在这 http://mp.weixin.qq.com/wiki/index.php 1.获取access ...

  5. day2 数据结构和一些基础知识

    请查看我的云笔记链接: http://note.youdao.com/noteshare?id=4171342601326695ec87866e1cc3e410&sub=20CFB149543 ...

  6. nodejs理解

    一.nodejs介绍 nodejs主要体现在事件机制和异步IO,nodejs是事件驱动的: nodejs作用:js的运行环境.操作文件.链接数据库: nodejs在执行js是单线程的,但不是nodej ...

  7. IOS VLC (第三方音频)的使用

    使用注意 ● 注意点 ● 存放VLC的文件夹名不要有空格 ● 一旦执行过编译脚本sh,就别再修改存放VLC的文件夹名   编译 ● 打开最外层的工作空间 选择真机运行 编译 ● 一些简单的示例程序 集 ...

  8. (转)写的非常好的一篇HTTP协议详解

    原文地址:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html#3931004(推荐看原文,因为原文文章末有更详细链接) 当今 ...

  9. note01-计算机网络

    1.基础概述 三网: 电信网络.有线电视网络.计算机网络(连通&共享) 终端->z本地ISP->x地区IXP->y主干ISP 通信方式: C/S .P2P(即是client也 ...

  10. hdu-2136 Largest prime factor---巧用素数筛法

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2136 题目大意: 每个素数在素数表中都有一个序号,设1的序号为0,则2的序号为1,3的序号为2,5的 ...