MySQL抓包工具:MySQL Sniffer 和性能優化



簡介

MySQL Sniffer 是一個基於 MySQL 協議的抓包工具,實時抓取 MySQLServer 端的請求,並格式化輸出。輸出內容包訪問括時間、訪問用戶、來源 IP、訪問 Database、命令耗時、返回數據行數、執行語句等。有批量抓取多個端口,后台運行,日志分割等多種使用方式,操作便捷,輸出友好。

同時也適用抓取 Atlas 端的請求,Atlas 是奇虎開源的一款基於MySQL協議的數據中間層項目,項目地址:https://github.com/Qihoo360/Atlas

同類型工具還有vc-mysql-sniffer,以及 tshark 的 -e mysql.query 參數來解析 MySQL 協議。

使用

下載安裝就不多說了,github 地址:https://github.com/Qihoo360/mysql-sniffer (點擊文章末尾 “閱讀原文” 跳轉)。

建議在 centos6.2 及以上編譯安裝,並用 root 運行。

工具參數:

  1. ./mysql-sniffer -h

  2. Usage mysql-sniffer [-d] -i eth0 -p 3306,3307,3308 -l /var/log/mysql-sniffer/ -e stderr

  3.         [-d] -i eth0 -r 3000-4000

  4.         -d daemon mode.

  5.         -s how often to split the log file(minute, eg. 1440). if less than 0, split log everyday

  6.         -i interface. Default to eth0

  7.         -p port, default to 3306. Multiple ports should be splited by ','. eg. 3306,3307

  8.            this option has no effect when -f is set.

  9.         -r port range, Don't use -r and -p at the same time

  10.         -l query log DIRECTORY. Make sure that the directory is accessible. Default to stdout.

  11.         -e error log FILENAME or 'stderr'. if set to /dev/null, runtime error will not be recorded

  12.         -f filename. use pcap file instead capturing the network interface

  13.         -w white list. dont capture the port. Multiple ports should be splited by ','.

  14.         -t truncation length. truncate long query if it's longer than specified length. Less than 0 means no truncation

  15.         -n keeping tcp stream count, if not set, default is 65536. if active tcp count is larger than the specified count, mysql-sniffer will remove the oldest one

示例

1. 實時抓取某端口信息並打印到屏幕

輸出格式為:時間,訪問用戶,來源 IP,訪問 Database,命令耗時,返回數據行數,執行語句。

  1. mysql-sniffer -i eth0 -p 3306

  2. 2017-02-23 14:47:45     testuser    10.xx.xx.xx     NULL             0ms             1  select @@version_comment limit 1

  3. 2017-02-23 14:47:45     testuser    10.xx.xx.xx     NULL             0ms             1  select USER()

  4. 2017-02-23 14:47:48     testuser    10.xx.xx.xx     NULL             0ms            13  show databases

  5. 2017-02-23 14:47:51     testuser    10.xx.xx.xx     NULL             0ms             1  SELECT DATABASE()

  6. 2017-02-23 14:47:51     testuser    10.xx.xx.xx     mysql            0ms             0  use mysql

  7. 2017-02-23 14:47:53     testuser    10.xx.xx.xx     mysql            0ms            29  show tables

  8. 2017-02-23 14:47:54     testuser    10.xx.xx.xx     mysql            0ms             1  select 1

  9. 2017-02-23 14:48:01     testuser1   10.xx.xx.xx     NULL             0ms             0  set autocommit=1

  10. 2017-02-23 14:48:01     testuser1   10.xx.xx.xx     NULL             0ms             0  set autocommit=1

 

2. 實時抓取某端口信息並打印到文件

-l 指定日志輸出路徑,日志文件將以 port.log 命名。

  1. mysql-sniffer -i eth0 -p 3306 -l /tmp

3. 實時抓取多個端口信息並打印到文件

-l 指定日志輸出路徑,-p 指定需要抓取的端口列表逗號分割。日志文件將以各自 port.log 命名。

  1. mysql-sniffer -i eth0 -p 3306,3307,3310 -l /tmp

4. 實時抓取多個連續遞增的端口並打印到文件

-l 指定日志輸出路徑,-r 指定端口范圍,日志文件將以各自 port.log 命名。

  1. //抓取端口包括(3306,3307,3308,3309,3310)

  2. mysql-sniffer -i eth0 -r 3306-3310 -l /tmp

5. 實時抓取多個連續遞增的端口同時過濾某幾個端口,並打印到文件

-l 指定日志輸出路徑,-r 指定端口范圍, -w 指定過濾端口列表逗號分割,日志文件將以各自 port.log 命名。

  1. //抓取端口包括(3306,3307,3310)

  2. mysql-sniffer -i eth0 -r 3306-3310  -w 3308,3309 -l /tmp  

6. 抓取某個端口以 daemon 模式運行,並打印到文件

-l 指定日志輸出路徑,-p 指定端口, -n 指定數據包個數,日志文件將以各自 port.log 命名。

  1. mysql-sniffer -i eth0 -p 3306  -l /tmp -d  

7. 抓取某個端口並截取指定長度的 SQL

-p 指定端口, -t 指定SQL長度,將輸出 SQL的前n個字符(適用於 SQL 過長的場景)。

  1. mysql-sniffer -i eth0 -p 3306  -t 100

更多

MySQL Sniffer 更多使用方式以及 Atlas 等其他技術請關注我們后續的相關分享。

 

閱讀原文

一次 group by + order by 性能優化分析


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM