一、maatkit工具集简介
maatkit包含多个MySQL的辅助工具,使用它们,可以让你的MySQL服务器使用起来更加方便也更加安全。其实它提供的大部分工具本身就应该是MySQL应该提供的,但是由于各种原因,MySQL更专注于数据库本身的性能和功能的增强,而忽略了这些方面工具的提供,好在percona和其他一些对MySQL本身功能仍然不满足公司组织发起并维护了maatkit项目,他们的贡献和使用进一步促进了maatkit的进步和发展。
maatkit的另外一个好处就是,他是开源的,是一个Free software。你可以自由的改进软件并将自己作出的改进版本向社会发行传播。它遵循GPL协议 。如何获得支持和服务是众所周知的自由软件的问题,但是对maatkit来说,也不是大问题。你可以通过google project获得免费的帮助,如果仍然不够,你甚至可以直接通过percona联系原作者,获得MySQL性能优化的专家顾问们的帮助。maatkit考虑到自身工具的数量问题,为了减轻它的复杂度,也简化使用者学习的难度,maatkit所有的工具的选项和参数都保持统一的标准(而且大部分参数和MySQL的命令行客户端保持一致),基本上,你会用了一个工具,其他工具的学习和了解也差不多。这样大大减轻了使用者的负担也加速了我们的学习过程。真是由于maatkit的这么多优势,它目前已经随Debian和CentOS等许多GNU/linux distributions一起发布。

下面我们先来简单了解一下maatkit包含的工具集为MySQL提供了哪些振奋人心的新特性和新功能。maatkit工具目前包含26个不同功能的工具。相应的工具名及其功能简介如下:
1.mk-archiver 
将MySQL表中的一些行数据库归档到另外一个表或者文件中。Archive rows from a MySQL table into another table or a file.
2.mk-audit 
对MySQL配置,架构和操作进行分析,汇总并生成报告。Analyze, summarize and report on MySQL config, schema and operation
3.mk-checksum-filter 
过滤mk-table-checksum生成的checksums。Filter checksums from mk-table-checksum.
4.mk-deadlock-logger 
抽取和记录MySQL死锁信息。Extract and log MySQL deadlock information.
5.mk-duplicate-key-checker 
查找MySQL表中重复的索引和外键索引。Find duplicate indexes and foreign keys on MySQL tables.
6.mk-fifo-split 
Split files and pipe lines to a fifo without really splitting.暂时没有被包含在fedora 11的源包中。
7.mk-find 
类似于GNU的find工具,用于查找MySQL表和执行动作。Find MySQL tables and execute actions, like GNU find.
8.mk-heartbeat 
监控MySQL replication延迟时间。Monitor MySQL replication delay.
9.mk-kill 
kill掉符合某一条件的MySQL queries。Kill MySQL queries that match certain criteria.暂时没有被包含在fedora 11的源包中。
10.mk-loadavg 
监控MySQL负载并在负载过大时采取某些动作。Watch MySQL load and take action when it gets too high. 暂时没有被包含在fedora 11的源包中。
11.mk-log-player 
分割并重现MySQL的slow logs。Split and play MySQL slow logs. 暂时没有被包含在fedora 11的源包中。
12.mk-parallel-dump 
以并行的方式dump MySQL的多个表。Dump sets of MySQL tables in parallel.
13.mk-parallel-restore 
以并行的方式将文件Load到MySQL中去。Load files into MySQL in parallel.
14.mk-profile-compact 
压缩mk-query-profiler的输出。Compact the output from mk-queryprofiler.
15.mk-query-digest 
解析日志和相关资料。分析,转换,过滤,复审queries,并生成报告。Parses logs and more. Analyze, transform, filter, review and report on queries.暂时没有被包含在fedora 11的源包中。存在另外的mk-log-parser工具。
16.mk-query-profiler 
执行SQL语句并打印统计信息,或者评估其他进程的活动。Execute SQL statements and print statistics, or measure activity caused by other processes.
17.mk-show-grants 
规范话和打印MySQL授权信息。这样你可以更有效的复制,比较他们,也可以更好的对他们进行版本控制。Canonicalize and print MySQL grants so you can effectively replicate, compare and version-control them.
18.mk-slave-delay 
控制MySQL slave端,使它滞后于其master。Make a MySQL slave server lag behind its master.
19.mk-slave-find 
找出并打印MySQL和其slave的层级关系树模型。Find and print replication hierarchy tree of MySQL slaves.
20.mk-slave-move 
在MySQL replication层级关系中移动某一个MySQL slave。Move a MySQL slave around in the replication hierarchy.
21.mk-slave-prefetch 
将relay log传给MySQL slave以便预热缓存。Pipeline relay logs on a MySQL slave to pre-warm caches.
22.mk-slave-restart 
监控MySQL replication并在其出现错误时重启replication。Watch and restart MySQL replication after errors.
23.mk-table-checksum 
利用它可以对在线的replication进行一致性检查,或者有效地计算一个或多个服务器上的MySQL表的校验和。Perform an online replication consistency check, or checksum MySQL tables efficiently on one or many servers.
24.mk-table-sync 
有效地同步MySQL的表。Synchronize MySQL tables efficiently.
25.mk-upgrade 
在两个MySQL server上提交SQL语句,并比较运行结果。Execute SQL statements against two MySQL servers and compare the results. 暂时没有被包含在fedora 11的源包中。
26.mk-visual-explain 
将EXPLAIN的输出打印成树状结构。Format EXPLAIN output as a tree.

注意:上面的各项工具及其相应的功能主要翻译自maatkit的官方文档。其中:mk-fifo-split,mk-kill,mk-loadavg,mk-log-player,mk-query-digest,mk-upgrade暂时没有包含在fedora的源中,而fedora的源中包含另外一个官方文档中不存在的工具mk-log-parser工具。

详细请参照:
http://hatemysql.com/2010/06/10/maatkit-%E7%AE%80%E4%BB%8B/
http://www.percona.com/

二、安装Maatkit
Maatkit是一个Perl写的Mysql开源管理工具,根据调查全球大约有70%多的Mysql管理员使用这个工具来管理Mysql,但国内很少有人知道这个工具,因此介绍这个工具。首先安装Maatkt,需要Perl,DBI,DBD::mysql相关模块。
1.perl
perl -v
This is perl, v5.8.5 built for i386-linux-thread-multi
Copyright 1987-2004, Larry Wall
一般linux安装的时候默认安装了perl。
2.Mysql安装(省略)
3.DBI,可以到这里下载相应的版本
tar zxf DBI-1.58.tar.gz
cd DBI-1.58
perl Makefile.pl
make
make test
make
make install
4.data-showtable包
gzip -c -d Data-ShowTable-x.xx.tar.gz | tar xvf -
cd Data-ShowTable-x.xx
perl Makefile.PL
make
不要试make test,测试套件损坏了!
make install
5.msql-mysql-modules 
gzip -c -d Msql-Mysql-modules-x.xx.tar.gz | tar xvf -
将压缩文件接压缩到一个名为Msql-Mysql-modules-x.xx的目录。
cd Msql-Mysql-modules-x.xx
perl Makefile.PL
在 "perl Makefile.PL" 期间,会向你提出一些问题。特别是你必须选择安装驱动器(MySQL, mSQL2 and/or mSQL1)。
MySQL驱动程序叫DBD::mysql,单独的mSQL驱动程序叫DBD::mSQL。如果你想支持mSQL1和mSQL2,用DBD::mSQL1。
make
make test
make install
6.DBD-mysql
安装DBD: DBD-mysql-4.005
perl Makefile.PL --mysql_config=/usr/local/mysql/bin/mysql_config
make
make test
make
make install
如果出现以下错误:
install_driver(mysql) failed: Can't load '/usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi/auto/DBD/mysql/mysql.so' for module DBD::mysql: libmysqlclient.so.12: cannot open shared object file: No such file or directory at /usr/lib/perl5/5.8.5/i386-linux-thread-multi/DynaLoader.pm line 230.
at (eval 6) line 3
Compilation failed in require at (eval 6) line 3.
Perhaps a required shared library or dll isn't installed where expected at /root/mysqlreport line 162
就做操作:
cp /usr/local/mysql/include/mysql* /usr/include/
cp /usr/local/mysql/lib/* /usr/lib/ (64位的是lib64)
然后再重新安装DBD: DBD-mysql-4.005
perl Makefile.PL --mysql_config=/usr/local/mysql/bin/mysql_config
make
make test
make install
7.安装maatkit
tar zxvf   maatkit-2325.tar.gz
cd maatkit-2325
perl Makefile.PL
make install
下面分别列出这套工具,以及官方的定义,在后续的文章中,会分别对这些工具进行介绍,以及自己的测试和心得体会。
mk-archiver Archive rows from a MySQL table into another table or a file.
mk-audit Analyze, summarize and report on MySQL config, schema and operation
mk-checksum-filter Filter checksums from mk-table-checksum.
mk-deadlock-logger Extract and log MySQL deadlock information.
mk-duplicate-key-checker Find duplicate keys and foreign keys on MySQL tables.
mk-fifo-split Split files and pipe lines to a fifo without really splitting.
mk-find Find MySQL tables and execute actions, like GNU find.
mk-heartbeat Monitor MySQL replication delay.
mk-log-player Split and play MySQL slow logs.
mk-log-server Serve MySQL binary logs.
mk-parallel-dump Dump sets of MySQL tables in parallel.
mk-parallel-restore Load files into MySQL in parallel.
mk-profile-compact Compact the output from mk-query-profiler.
mk-query-digest Parses logs and more. Analyze, transform, filter, review and report on queries.
mk-query-profiler Execute SQL statements and print statistics, or measure activity caused by other processes.
mk-show-grants Canonicalize and print MySQL grants so you can effectively replicate, compare and version-control them.
mk-slave-delay Make a MySQL slave server lag behind its master.
mk-slave-find Find and print replication hierarchy tree of MySQL slaves.
mk-slave-move Move a MySQL slave around in the replication hierarchy.
mk-slave-prefetch Pipeline relay logs on a MySQL slave to pre-warm caches.
mk-slave-restart Watch and restart MySQL replication after errors.
mk-table-checksum Perform an online replication consistency check, or checksum MySQL tables efficiently on one or many servers.
mk-table-sync Synchronize MySQL tables efficiently.
mk-visual-explain Format EXPLAIN output as a tree.

原文来自:http://www.bigheaddba.net/