代码改变世界

Innotop简单介绍

2017-10-12 15:30  潇湘隐者  阅读(1682)  评论(5编辑  收藏  举报

 

Innotop介绍

 

Innotop是一款Perl脚本编写、开源、功能强大的MySQ的监控工具,它通过文本模式(命令行模式)监控,功能强大,配置简单,易于使用等等特性。Innotop这个项目位于https://github.com/innotop/innotop上。官方的介绍如下:

 

innotop is a 'top' clone for MySQL with many features and flexibility.

·         completely customizable; it even has a plugin interface

·         monitors many servers at once and can aggregate across them

The manual is embedded into the program in Perl's POD format, so it should be available through perldoc and man, and is still available online on the previous hosting (the project moved from google code to git): http://innotop.googlecode.com/svn/html/index.html

 

 

Innotop安装

 

 

在安装之前先要确定你的系统安装了Time::HiRes,Term::ReadKey,DBI,DBD::mysql这四个包。当然在执行perl Makefile.PL命令是会检查这些依赖项,如下所示:

 

 

[root@DB-Server ~]# cd /tmp
[root@DB-Server tmp]# unzip innotop-master.zip 
[root@DB-Server tmp]# cd innotop-master/
[root@DB-Serverinnotop-master]# ls
Changelog  COPYING  innotop  innotop.spec  INSTALL  Makefile.PL  MANIFEST  README.md  snapshot_queries.png  t
[root@DB-Server innotop-master]# perl Makefile.PL 
Checking if your kit is complete...
Looks good
Warning: prerequisite DBD::mysql 1 not found.
Warning: prerequisite Term::ReadKey 2.1 not found.
Writing Makefile for innotop
[root@DB-Server innotop-master]# 
[root@DB-Server innotop-master]# yum search ReadKey
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * epel: ftp.cuhk.edu.hk
================================== N/S matched: ReadKey ==========================================
perl-TermReadKey.x86_64 : A perl module for simple terminal control
 
  Name and summary matches only, use "search all" for everything.
[root@DB-Server innotop-master]# 
[root@DB-Server innotop-master]#yum install perl-TermReadKey
[root@DB-Server innotop-master]#yum install perl-DBD-MySQL
[root@DB-Server innotop-master]#  perl Makefile.PL 
Writing Makefile for innotop
[root@DB-Server innotop-master]# make
cp innotop blib/script/innotop
/usr/bin/perl -MExtUtils::MY -e 'MY->fixin(shift)' -- blib/script/innotop
Manifying blib/man1/innotop.1
[root@DB-Server innotop-master]# make install
Installing /usr/local/share/man/man1/innotop.1
Installing /usr/local/bin/innotop
Appending installation info to /usr/lib64/perl5/perllocal.pod
[root@DB-Server innotop-master]# 

 

 

安装好Innotop后,有可能使用的时候,会遇到下面异常信息

 

[root@gettestlnx02 innotop-master]# innotop -u root -p123456
localhost PROCESSLIST_NO_IS: install_driver(mysql) failed: Can't load '/usr/lib64/perl5/auto/DBD/mysql/mysql.so' for module DBD::mysql: libmysqlclient.so.16: cannot open shared object file: No such file or directory at /usr/lib64/perl5/DynaLoader.pm line 200.
 at (eval 566) line 3
Compilation failed in require at (eval 566) line 3.
Perhaps a required shared library or dll isn't installed where expected
 at /usr/local/bin/innotop line 7737

 

 

这个一般是没有perl-DBD-MySQL组件,或者需要重新安装perl-DBD-MySQL组件即可(为什么重装即可解决的原因暂时不清楚,没有深入去研究这个)。

 

 #yum remove perl-DBD-MySQL

 #yum install perl-DBD-MySQL

 

 

 

 

Innotop使用

 

 

Innotop的帮助信息可以通过下面几种方式:

 

innotop --help

man innotop

perldoc innotop

 

[oracle@DB-Server admin]$ innotop --help
Usage: innotop <options> <innodb-status-file>
 
  --askpass          Prompt for a password when connecting to MySQL #当连接MySQL时提示输入密码
  --[no]color   -C   Use terminal coloring (default)     #使用终端颜色(默认)
  --config      -c   Config file to read              #读配置文件
  --count            Number of updates before exiting
  --delay       -d   Delay between updates in seconds    #多少秒更新一次
  --help             Show this help message          #显示帮助信息
  --host        -h   Connect to host              #连接的主机名
  --[no]inc     -i   Measure incremental differences
  --mode        -m   Operating mode to start in
  --nonint      -n   Non-interactive, output tab-separated fields
  --password    -p   Password to use for connection     #连接的密码
  --port        -P   Port number to use for connection     #连接的端口号
  --skipcentral -s   Skip reading the central configuration file
  --socket      -S   MySQL socket to use for connection #Scoket位置
  --spark            Length of status sparkline (default 10)
  --timestamp   -t   Print timestamp in -n mode (1: per iter; 2: per line)
  --user        -u   User for login if not current user  #连接MySQL的用户
  --version          Output version information and exit #输出Innotop版本信息并退出
  --write       -w   Write running configuration into home directory if no config files were loaded
 
innotop is a MySQL and InnoDB transaction/status monitor, like 'top' for
MySQL.  It displays queries, InnoDB transactions, lock waits, deadlocks,
foreign key errors, open tables, replication status, buffer information,
row operations, logs, I/O operations, load graph, and more.  You can
monitor many servers at once with innotop. 

 

 

innotop -u <username> -p <password> -h <hostname> -P <port>

 

如果密码中有特殊字符,最好使用双引号将密码括起来,否则老是提示你需要使用密码连接数据库。

 

innotop -uroot -pllo9kc&sq@2eq

 

innotop -uroot -p"llo9kc&sq@2eq"

 

clip_image001

 

 

连接成功后 输入?可以进入帮助, 另外注意快捷键的大小写, Q表示查询列表,而q表示退出。在不同模式切换可以使用SHIFT + 对应字母(也即大写字母在模式间切换)

 

 

clip_image002

 

 

Switch to a different mode:
   A  Dashboard         I  InnoDB I/O Info     Q  Query List
   B  InnoDB Buffers    K  InnoDB Lock Waits   R  InnoDB Row Ops
   C  Command Summary   L  Locks               S  Variables & Status
   D  InnoDB Deadlocks  M  Replication Status  T  InnoDB Txns
   F  InnoDB FK Err     O  Open Tables         U  User Statistics
 
Actions:  
   d  Change refresh interval (更改刷新间隔)           q  Quit innotop (退出innotop工具)
   k  Kill a query's connection (杀死一查询连接)       r  Reverse sort order (反向排序)
   n  Switch to the next connection(切换到下一个连接)  s  Choose sort column (选择排序字段)
   p  Pause innotop (停止innotop)                       x  Kill a query(杀死查询)
 
Other:
 TAB  Switch to the next server group  切换到另外一组服务器       /  Quickly filter what you see   快速过滤你所见信息
   !  Show license and warranty        显示License等相关信息       =  Toggle aggregation
   #  Select/create server groups      选择或创建服务器组       @  Select/create server connections 选择或创建服务器连接
   $  Edit configuration settings      编辑配置设置信息       \  Clear quick-filters           清除快速过滤设置
Press any key to continue

 

进入到交互式界面后,点击@键,就会进入到连接配置界面,如下截图所示:

 

clip_image003

 

 

配置好了后,就可以使用Inntop远程监控MySQL服务器了,那么如果监控多个MySQL,如何切换呢? SHIF +@就可以进入选择哪一个服务器

 

_____________________ Choose from _____________________

DB-Server   DBI:mysql:;host=10.20.57.15;port=3306   

localhost   DBI:mysql:;;mysql_read_default_group=client

 

Choose connections for this mode:

 

在Choose connections for this mode: 输入localhost按回车键就切换到本机,如果输入DB-Server按回车键就切换到DB-Server服务器。不过这种方式有一个弊端就是,Innotop退出后,这些信息就会丢失,需要重新配置,可以通过配置文件来保存。

 

默认情况下是没有innotop.conf这个配置文件的,你可以使用innotop --write进入交互界面,提示你需要输入密码,选择q退出即可,此时就会在当前目录生成.innotop/innotop.conf配置文件

 

 

[root@DB-Server ~]# innotop --write

[root@DB-Server ~]# find / -name "innotop.conf"

/root/.innotop/innotop.conf

[root@DB-Server ~]# ls .innotop/

innotop.conf  plugins

[root@DB-Server ~]# vi /root/.innotop/innotop.conf

 

clip_image004

 

[connections]
 
localhost=user=root have_user=1 have_pass=1 dsn=DBI:mysql:;;mysql_read_default_group=client savepass= dl_table=test.innotop_dl
mydb=user=root have_user=1 pass="xxxxxx" have_pass=1 dsn=DBI:mysql:;host=192.168.7.218;port=3306 savepass=1 dl_table=
 
[/connections]
 
[active_connections]
 
A=localhost
B=mydb
C=
D=
F=
..

 

可以在配置文件里面添加需要监控的MySQL主机,然后在active_connections下设置对应的MySQL服务器,如下所示,

 

clip_image005

 

localhost没有指定密码,所以默认连接到mydb这台服务器了。savepass=1 表示保存密码,savepass=0表示不保存密码。如果出于安全考虑,不要设置savepass参数,它默认为0。

 

clip_image006

 

 

 

本文也不打算详细讲述如何使用Innotop的各个功能,这个实在有点多,下面简单的介绍两个例子:

 

1:在Query List模式下(SHIFT+Q),你可以看到正在执行的SQL语句执行了多长时间,如下所示,我构造了一个执行时间较长的例子,那么在Query List模式下,你就可以看到脚本的执行时长,当然,执行完了后,这里就看不到相关信息了。

 

 

clip_image007

 

 

2:另外,在这篇博客中MySQL线程处于Waiting for table flush的分析,也介绍了Innotop使用的一个案例,如下所示:

 

对于实验中使用lock table read这种情况,这种会话可能处于Sleep状态,而且它也不会出现在show engine innodb status \G命令的输出信息中。 即使show open tables where in_use >=1;能找到是那张表被lock住了,但是无法定位到具体的线程(连接),其实这个是一个头痛的问题。但是inntop这款利器就可以定位到,如下所示,线程17锁住了表test,在innotop里面就能定位到是线程17。

 

clip_image008

 

clip_image009

 

Innotop的安装使用非常简单,工具功能也非常强大,关键还在于使用工具的人能否物尽其用,能够对各个指标深入了解,这样才能快速的分析问题、定位问题,解决问题。后续如果积累了一些使用心得和案例,再详细总结一下这个工具!

 

 

 

参考资料:

 

https://www.percona.com/blog/2013/10/14/innotop-real-time-advanced-investigation-tool-mysql/

http://www.cnblogs.com/ivictor/p/5101506.html