MySQL 如何快速复制用户权限到其他环境

标题: MySQL 如何快速复制用户权限到其他环境

作者:lōττéry©版权所有[文章允许转载,但必须以链接方式注明源地址,否则追究法律责任.]


脚本用途:
 1、mysql主从搭建,从库用户权限的复制(可直接在主库执行脚本,将输出结果在从库执行)
 2、实现 pt-show-grants【打印权限信息】(线上可选择不用安装pt软件..通过脚本实现pt-show-grants功能)
 3、用户权限统计

脚本:

hostname=192.168.1.12
username=lottery
password=lottery
touch CREATE_user.txt
for port in `ps -ef | grep mysql| grep socket| grep datadir| awk -F= '{print $NF}'`
  do
   sock=`ps -ef | grep mysql| grep socket| grep datadir|awk -F".pid" '{print $NF}'| grep $port`
   echo "-------"$port"-create_user.sql:-------" >>CREATE_user.txt
     echo "select concat('show grants for ''',user,'''@''',host, ''';') from mysql.user where user <>'root' and user <>'' " | \
     mysql --host=$hostname --user=$username --password=$password $sock -N | \
     mysql --host=$hostname --user=$username --password=$password $sock -N | \
     sed "s/$/;/"  >>CREATE_user.txt
done
cat CREATE_user.txt
rm -rf CREATE_user.txt



脚本打印信息如下:
-------3306-create_user.sql:-------
GRANT ALL PRIVILEGES ON *.* TO 'lottery'@'192.168.%' IDENTIFIED BY PASSWORD '*6E9EF176ABE11111146AF84111111171111111';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'192.168.1.%' IDENTIFIED BY PASSWORD '*AC111111FDDC8943AB31CBD111111E79F7953EA';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'192.168.2%' IDENTIFIED BY PASSWORD '*AC2111111FDDC8943A11111147D758E79F7953EA';
GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329101111118DDA7DC61111112AD9';
-------3308-create_user.sql:-------
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY PASSWORD '*AC241830FFDDC11111131CBD47D758E79111111A';
GRANT ALL PRIVILEGES ON *.* TO 'lottery'@'192.168.%' IDENTIFIED BY PASSWORD '*6E9EF111111590A33746AF84DB6348D171111111';
GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'mycat'@'192.168.1.%' IDENTIFIED BY PASSWORD '*6EA111111950B4A6BFC111111925FD11111147A42';
GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'mycat_user'@'192.168.1%' IDENTIFIED BY PASSWORD '*6EAF111111B4A6BFC92BA04111111086111111112';
GRANT SHUTDOWN ON *.* TO 'admin'@'localhost' IDENTIFIED BY PASSWORD '*4E74B1831111113952F4C163CB961DFBC5111111';
[root@MIU-MYSQL-002 ~]# 


pt-show-grants 打印权限介绍

l 功能介绍:
规范化和打印 mysql权限,让你在复制、比较 mysql权限以及进行版本控制的时候更有效率!
l 用法介绍:
pt-show-grants [OPTION...] [DSN] 选项自行用help 查看,DSN 选项也请查看help,选项区分大小写。
l 使用示例:
查看指定mysql的所有用户权限: pt-show-grants --host='localhost' --user='root' --password='zhang@123' 
查看执行数据库的权限: pt-show-grants --host='localhost' --user='root' --password='zhang@123' --database='hostsops' 
查看每个用户权限生成 revoke收回权限的语句: pt-show-grants --host='localhost' --user='root' --password='zhang@123' --revoke

【源于本人笔记】 若有书写错误,表达错误,请指正... 
上一篇:算法题-Length of Last Word


下一篇:限制MySQL Binlog的传输速率