pt-show-grants

时间:2023-03-09 18:05:36
pt-show-grants

用法:

pt-show-grants [OPTION ... ] [DSN]
例子:
pt-show-grants
pt-show-grants --separate --revoke | diff othergrants.sql -
风险:
只读工具和写工具
pt-show-grants默认是只读,风险低。如果使用--flush,将执行flush privileges。
至今没有已知bug导致用户的损失。
描述:
pt-show-grants提取、排序和打印MySQL 相关账户
存在的原因:
1、从一台服务器上将权限复制到另一台机器上;可以很简单地从第一台服务器上抽取相关权限,然后直接导入第二台机器上;
2、将权限信息放在版本控制中。使用自动grant dump到版本控制中,可能会获得一些没有变化的授权。原因在于mysql按照看似随机的顺序打印授权信息。
例如:
一天打印如下:
pt-show-grants
另一天可能打印:
pt-show-grants
授权没变,但是顺序变了
该脚本可以解决该问题,在GRANT和ON之间做排序。如果show grants结果存在多行的话,也会这些行进行排序。
3、比较服务器之间的权限,没有规范化的,这样的操作会比较困难。输出结果完全是可对比的。
执行pt-show-grans的过程:
命令:
pt-show-grants -u pt -p 213456 -S ./tmp/mysql.sock
 全日志:
pt-show-grants
从全日志中可以看到:
1、先查找所有用户和Host
2、然后逐个执行show grants
输出:
pt-show-grants
两种不同的方式进行连接
选项:
--ask-pass            Prompt for a password when connecting to MySQL
--ask-pass 作为密码的提示 在系统中运行出错
出错信息:Enter password: Cannot read response; is Term::ReadKey installed? Can't locate Term/ReadKey.pm in @INC
缺少Term/ReadKey.pm这个模块——在自己测试机上继续练习
--charset=s       -A  Default character set
--charset=s -A 连接使用的字符集
例子:
pt-show-grants -upt -hlocalhost -S ./tmp/mysql.sock -p 213456 -A utf8
对应的全日志中多了,37 Query     /*!40101 SET NAMES utf8*/
--config=A            Read this comma-separated list of config files; if  specified, this must be the first option on the command   line
--config 
类型为数组
从特定的文件中读取配置,如果设置,这个选项需要设置在第一行
--database=s      -D  The database to use for the connection
--database=s -D 连接数据库使用到的DB
--defaults-file=s -F  Only read mysql options from the given file
pt-show-grants --defaults-file=./etc/my.cnf -upt -p213456
--drop                Add DROP USER before each user in the output
pt-show-grants
添加了两行:
DROP USER 'pt'@'%';
DELETE FROM `mysql`.`user` WHERE `User`='pt' AND `Host`='%';
--flush               Add FLUSH PRIVILEGES after output
在输出最后一行加了如下一行命令:
FLUSH PRIVILEGES;
--[no]header          Print dump header (default yes)
pt-show-grants
--help                Show help and exit
 
--host=s          -h  Connect to host
--ignore=a            Ignore this comma-separated list of users 【不起作用??】
参数必须是'root'@'localhost',而不是root
pt-show-grants
--only=a              Only show grants for this comma-separated list of users
只显示的用户
--password=s      -p  Password to use when connecting
--pid=s               Create the given PID file
--port=i          -P  Port number to use for connection
--revoke              Add REVOKE statements for each GRANT statement
pt-show-grants
是REVOKE形式,与drop不同,其是delete
--separate            List each GRANT or REVOKE separately
--set-vars=s          Set these MySQL variables (default wait_timeout=10000)
用法:--set-vars            wait_timeout=100
 
--socket=s        -S  Socket file to use for connection
--[no]timestamp       Add timestamp to the dump header (default yes)是否添加时间戳
--user=s          -u  User for login if not current user
--version             Show version and exit
选项类型:
Option types: s=string, i=integer, f=float, h/H/a/A=comma-separated list, d=DSN, z=size, m=time
s表示string
i表示整数
f表示浮点数
h/H/a/A 表是逗号分隔型取值格式
d表示DSN
z表示大小
m表示time
pt-show-grants
参数的默认值:
pt-show-grants
参数的默认值
环境:
使用PTDEBUG可以将debug信息打印到STDERR,也可以重定向到文件中
PTDEBUG=1 pt-show-grants ... > FILE 2>&1
环境准备:
Perl,DBI,DBD::mysql 以及新版的perl所需要的一些软件包
研究方法:
1、看帮助
2、开启全日志,查找对应的命令发送哪些SQL语句
3、使用PTDEBUG信息,查找更细节的过程
4、研究代码