mysql-utilities1.6

mysql-utilities1.6

mysql-utilities是一个用python编写的mysql工具集

mysql-utilities是Oracle专门开发的


一共有28个工具

/usr/bin/mysqlauditadmin
/usr/bin/mysqlauditgrep
/usr/bin/mysqlbinlogmove
/usr/bin/mysqlbinlogpurge
/usr/bin/mysqlbinlogrotate
/usr/bin/mysqldbcompare 重要
/usr/bin/mysqldbcopy
/usr/bin/mysqldbexport
/usr/bin/mysqldbimport
/usr/bin/mysqldiff
/usr/bin/mysqldiskusage
/usr/bin/mysqlfailover
/usr/bin/mysqlfrm  重要
/usr/bin/mysqlgrants
/usr/bin/mysqlindexcheck  重要
/usr/bin/mysqlmetagrep  重要
/usr/bin/mysqlprocgrep  重要
/usr/bin/mysqlreplicate
/usr/bin/mysqlrpladmin
/usr/bin/mysqlrplcheck
/usr/bin/mysqlrplms
/usr/bin/mysqlrplshow
/usr/bin/mysqlrplsync
/usr/bin/mysqlserverclone
/usr/bin/mysqlserverinfo
/usr/bin/mysqlslavetrx
/usr/bin/mysqluc
/usr/bin/mysqluserclone






不要用yum安装,要下载rpm包安装最新版本 mysql-utilities-1.6,yum安装的是1.3版本有bug
yum install   -y   mysql-utilities

下载地址:https://dev.mysql.com/downloads/utilities/
yum localinstall -y   mysql-connector-python-2.1.6-1.el6.x86_64.rpm
yum localinstall -y   mysql-utilities-1.6.5-1.el6.noarch.rpm




mysql-utilities系列工具的连接数据库方式
<user>:<passwd>@<host>:<port>:<socket>

例如:/usr/bin/mysqldbcompare --server1='root':'963'@127.0.0.1:3306 --server2='root':'963'@127.0.0.1:3306  --changes-for=server1  --show-reverse  --difftype=sql school:mysql




1. mysqldbcompare
http://www.ttlsa.com/mysql/mysqldbcompare-compare-two-databases/
比较两个服务器或同个服务器上的数据库
比较定义文件和数据
产生差异报告
生成差异性的转换SQL语句

注意:比较的两个表要有主键!!

mysqldbcompare和redgate的mysqlcompare都可以比较字段的大小写
比如
ABC varchar(10) default null
Abc  varchar(10) default null

可以比较出来
生成的差异脚本都是

ALTER TABLE `school`.`tt`
  DROP COLUMN `ABC`
  , ADD COLUMN `Abc` varchar(10) NULL;



选项
--skip-object-compare
跳过数据库对象缺失的检测

--skip-diff
跳过比较对象的定义(CREATE语句)

--skip-row-count
跳过检测表的行数

--skip-data-check 
跳过检查表数据的一致性,skip data consistency check.

--skip-table-options  
跳过所有的表选项,例如AUTO_INCREMENT,ENGINE,CHARSET



--skip-checksum-table  1.6版本新增选项
跳过检查CHECKSUM TABLE,skip CHECKSUM TABLE step in data consistency check.




--run-all-tests   
首次发现差异时不中止,继续检测




--all             
检测所有库,1.4.0版本引入


--exclude=EXCLUDE
排除一个或多个特定的数据库。1.4.0版本引入
    
    
--changes-for=server1
默认server1
--changes-for=server1: 针对server1,以server2为参照物(默认)
--changes-for=server2: 针对server2,以server1为参照物

db1:db2
要比较的数据库,db1和db2这个两个数据库进行比较


--show-reverse
输出参照物的sql语句



--disable-binary-logging
不记录binlog,set sql_log_bin=0



例子
对比本机的两个库的差异
/usr/bin/mysqldbcompare --server1='root':'963'@127.0.0.1:3306 --server2='root':'963'@127.0.0.1:3306  --changes-for=server1   --disable-binary-logging  --difftype=sql school:school2



对比两台机器的两个库的差异
/usr/bin/mysqldbcompare --server1='root':'963'@10.105.9.115:3306 --server2='root':'963'@127.0.0.1:3306  --changes-for=server1 --run-all-tests   --disable-binary-logging   --difftype=sql school:school




对比两台机器的两个库的表结构差异
/usr/bin/mysqldbcompare --server1='root':'963'@10.105.9.115:3306 --server2='root':'963'@127.0.0.1:3306  --changes-for=server1  --skip-row-count --skip-checksum-table   --skip-data-check  --skip-table-options    --disable-binary-logging  --run-all-tests   --difftype=sql   school:school
          



2. mysqldbcopy
服务器之间复制数据库
在同一台服务器上克隆数据库
支持重命名



3. mysqldbexport
从一个或多个数据库导出元数据和或数据
支持的格式: SQL, CSV, TAB, Grid, Vertical




4. mysqldbimport
从一个或多个文件导入元数据和数据
支持mysqldbexport各种格式



5. mysqldiff
比较对象的定义
产生差异的报告


6. mysqldiskusage
显示数据库磁盘使用情况
生产的报表有: SQL, CSV, TAB, Grid, Vertical




7. mysqlfrm

http://www.ttlsa.com/mysql/mysql-utilities-mysqlfrm/
读.frm 文件, 可以以byte-by-byte 诊断模式
表生成 CREATE 语句

mysqlfrm 是一个恢复性质的工具,用来读取.frm文件并从该文件中找到表定义数据生成CREATE语句。
在大多数情况下,生成的CREATE语句用于在另一个服务器上创建表或进行诊断等。
然而,有些功能是不保存在.frm文件中的,因此这些功能将被忽略的。如:
外键约束
自增长序列
字符集


mysqlfrm 有两种操作模式
再生实例模式:默认的模式,使用--basedir选项或指定--server选项来连接到已经安装的实例。这种过程不会改变原始的.frm文件。该模式也需要指定--port选项来给再生的实例使用,该端口不能与现有的实例冲突。在读取.frm文件后,再生的实例将被关闭,所有的临时文件将被删除的。 某些引擎表在默认模式下不可读取的。如PARTITION, PERFORMANCE_SCHEMA,必需在诊断模式下可读。


诊断模式:需要指定 --diagnostic 选项。byte-by-byte读取.frm文件 尽可能多的恢复信息。该模式有更多的局限性,不能校验字符集Cannot generate character set or collation names without the --server option.。
当使用默认模式无法读取文件或者该服务器上没有安装MySQL实例就使用诊断模式,支持分区表,P_S库,sys库
需要指定.frm文件的路径,也可以指定一个目录,该目录下的所有.frm文件将被读取。


如果省略了数据库名称和路径,最后的一个文件夹将作为数据库名称的。如 /home/me/data1/t1.frm,data1将作为数据库名。
如果不想最后的文件夹作为数据库名,只需指定冒号,如 /home/me/data1/:t1.frm,在这种情况下,CREATE语句将忽略数据库,如果是读取目录下所有的frm文件则无法忽略数据库。

选项
--diagnostic
诊断模式



例子
读取目录下所有的frm文件
mysqlfrm --diagnostic /data/mysql/mysql3306/data/school/
输出

# WARNING: Cannot generate character set or collation names without the --server option.
# CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
# Reading .frm file for /data/mysql/mysql3306/data/school/tpartition.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement:

CREATE TABLE `school`.`tpartition` (
  `id` int(11) DEFAULT NULL
) ENGINE=INNODB  
PARTITION BY RANGE (id)
(PARTITION p0 VALUES LESS THAN (10) ENGINE = InnoDB,  
PARTITION p1 VALUES LESS THAN (20) ENGINE = InnoDB);

# Reading .frm file for /data/mysql/mysql3306/data/school/tt.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement:

CREATE TABLE `school`.`tt` (
  `id` int(11) NOT NULL,
  `name` varchar(40) DEFAULT NULL,
PRIMARY KEY `PRIMARY` (`id`)
) ENGINE=InnoDB;

# Reading .frm file for /data/mysql/mysql3306/data/school/ttt.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement:

CREATE TABLE `school`.`ttt` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(400) DEFAULT NULL,
PRIMARY KEY `PRIMARY` (`id`)
) ENGINE=InnoDB;

#...done.


实际表结构,只有自增列和字符集没有指定,其他正常
CREATE TABLE `ttt` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4

CREATE TABLE `tt` (
  `id` INT(11) NOT NULL,
  `name` VARCHAR(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4

CREATE TABLE `tpartition` (
  `id` INT(11) DEFAULT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (id)
(PARTITION p0 VALUES LESS THAN (10) ENGINE = InnoDB,
 PARTITION p1 VALUES LESS THAN (20) ENGINE = InnoDB) */




读取P_S库和sys库的单个frm文件
mysqlfrm --diagnostic /data/mysql/mysql3306/data/performance_schema/users.frm
mysqlfrm --diagnostic /data/mysql/mysql3306/data/sys/waits_global_by_latency.frm   
sys库的表定义其实是一个视图定义,mysql里面视图就是表,表就是视图





8. mysqlindexcheck

http://www.ttlsa.com/mysql/mysql-utilities-mysqlindexcheck/
读取一个或多个表的索引
检查冗余和重复的索引
生成的报表有: SQL, CSV, TAB, Grid, Vertical


选项
--show-drops
打印drop index语句对于要drop的索引


--show-indexes
显示每个表的索引


--stats
显示索引的性能统计信息,估计读取mysql库`innodb_index_stats`


--report-indexes
打印没有主键或唯一索引的表


--best
限制打印最好索引的数量

--worst
限制打印最差索引的数量

-vvv
显示工具的工作过程


例子
针对school库进行检查
mysqlindexcheck --server='root':'963'@127.0.0.1:3306  school   --show-drops   --stats  --format=sql --report-indexes  
输出

WARNING: Using a password on the command line interface can be insecure.
# Source on 127.0.0.1: ... connected.
# Table `school`.`tpartition` is not indexed.
# WARNING: Not enough data to calculate best/worst indexes.
# The following index is a duplicate or redundant for table school.tt:
#
CREATE INDEX `idx_tt_name` ON `school`.`tt` (`name`) USING BTREE
#     may be redundant or duplicate of:
CREATE INDEX `idx_tt_nameid` ON `school`.`tt` (`name`, `id`) USING BTREE
#
# DROP statement:
#
ALTER TABLE `school`.`tt` DROP INDEX `idx_tt_name`;
#
# The following index for table school.tt contains the clustered index and might be redundant:
#
CREATE INDEX `idx_tt_nameid` ON `school`.`tt` (`name`, `id`) USING BTREE
#
# DROP/ADD statement:
#
ALTER TABLE `school`.`tt` DROP INDEX `idx_tt_nameid`, ADD INDEX `idx_tt_nameid` (name);
#
# WARNING: Not enough data to calculate best/worst indexes.
# Table `school`.`tt2` is not indexed.
# WARNING: Not enough data to calculate best/worst indexes.
# The following index is a duplicate or redundant for table school.ttt:
#
CREATE INDEX `idx_ttt_id` ON `school`.`ttt` (`id`) USING BTREE
#     may be redundant or duplicate of:
ALTER TABLE `school`.`ttt` ADD PRIMARY KEY (`id`)
#
# DROP statement:
#
ALTER TABLE `school`.`ttt` DROP INDEX `idx_ttt_id`;
#
# The following indexes for table school.ttt contain the clustered index and might be redundant:
#
CREATE INDEX `idx_ttt_id` ON `school`.`ttt` (`id`) USING BTREE
#
CREATE INDEX `idx_ttt_nameid` ON `school`.`ttt` (`name`, `id`) USING BTREE
#
# DROP/ADD statements:
#
ALTER TABLE `school`.`ttt` DROP INDEX `idx_ttt_id`;
ALTER TABLE `school`.`ttt` DROP INDEX `idx_ttt_nameid`, ADD INDEX `idx_ttt_nameid` (name);
#
# WARNING: Not enough data to calculate best/worst indexes.



针对school库的tt表进行检查
mysqlindexcheck --server='root':'963'@127.0.0.1:3306  school.tt   --show-drops  --stats   --format=sql --report-indexes






9. mysqlmetagrep

http://www.ttlsa.com/mysql/mysql-utilities-mysqlmetagrep/
搜索元数据
正则表达式搜索数据库
搜索查询生成SQL语句
'database', 'trigger', 'user', 'routine', 'column', 'table', 'partition', 'event', 'view'。默认搜索所有类型。

在'database', 'trigger', 'user', 'routine', 'column', 'table', 'partition', 'event', 'view'对象中搜索包含bbs的对象

选项
--body
搜索存储过程或函数里面的内容,默认只搜索对象名称





例子
通配符
mysqlmetagrep --server='root':'963'@localhost --body --pattern='%cost%'


正则
mysqlmetagrep --server='root':'963'@localhost --body --pattern='^.*cost.*' --basic-regex



10. mysqlprocgrep

http://www.ttlsa.com/mysql/mysql-utilities-mysqlprocgrep/
搜索进程信息
搜索生成SQL语句
kill掉匹配到的进程

类似pt-kill

选项
--match-state
匹配状态


--match-user
匹配用户


--match-command
匹配命令


--match-xxx 选项如同INFORMATION_SCHEMA.PROCESSLIST 表列名。
执行该命令需要 PROCESS 和 SUPER 权限。没有PROCESS权限,没法查看其他用户的权限。没有SUPER权限,不能对其他用户的进程执行某些动作。

例子
不显示CREATE PROCEDURE kill_processes
mysqlprocgrep  --match-user=root  --kill-connection --match-state=sleep  --sql-body

显示CREATE PROCEDURE kill_processes
mysqlprocgrep  --match-user=root  --kill-connection --match-state=sleep  --print-sql  

杀死用户是nobody的 并且在1分钟内创建的所有进程
mysqlprocgrep --server='root':'963'@127.0.0.1:3306  --match-user=nobody --age=1m --print --kill-query

杀死所有超过1小时的空闲进程
mysqlprocgrep --server='root':'963'@127.0.0.1:3306  --match-command=sleep --age=1h --kill-connection



11. mysqluserclone
克隆用户
显示用户权限



12. mysqluc
MySQL Utilities 命令行客户端
允许长连接到 MySQL 服务器
可以使用Tab键来完成工具名称和选项
允许使用命令的短名称,如 serverinfo 代替 mysqlserverinfo


13. mysqlfailover
提供对复制结构故障自动转移
使用全局事务标识符 (GTID, MySQL Server 5.6.5+)

没有vip,keepalived+mysqlfailover,但是vip跟mysqlfailover不是集成的依然有风险,而且貌似有bug,主库挂了,failover mode为fail无法转移

MHA是percona公司推荐的,而mysql-utilities还没见到有人用

《mysql管理之道 P290》



14. mysqlreplicate
设置复制
从一开始,当前,特定的binlog,pos复制


15. mysqlrplms
提供round-robin multi-source 复制(a slave server continually cycles through multiple masters in order to store a consolidated data set)
使用全局事务标识符 (GTID, MySQL Server 5.6.5+)



16. mysqlrpladmin
管理复制拓扑
允许恢复主
命令包括 elect, failover, gtid, health, start, stop, and switchover



17. mysqlrplcheck
检查复制配置
在主上测试二进制日志


18. mysqlrplshow
查看从连接到的主
可以递归搜索
显示复制拓扑图形或列表



19. mysqlrplsync
检查服务器之间的数据一致性
使用全局事务标识符(GTID)
需要MySQL Server 5.6.14 和更高版本




原文地址:https://www.cnblogs.com/MYSQLZOUQI/p/7055312.html