背景
總是有些開發和 測試在抱怨MySQL沒有SQL Server的高頻語句分析功能和
trace功能。其實MySQL是個輕量型的資料庫軟體,相比臃腫無比的SQL Server,在
搭建效率和軟硬體性能上更勝一籌,更不要提Windows域環境的各種坑了。輕便高
效的同時,必然會舍棄一些與高速OLTP無關的功能,如資料倉庫方向的優化,語句
跟蹤等功能。
但在5.7版本的sys庫加入後,也可以配合官方的workbench軟體進行高頻語句,
索引使用情況,I/O性能等等的分析,雖然不能完全媲美SSMS的全能,但是比人肉
讀取sys庫和i_s庫,p_s庫來說還是雲泥之别。
2.MySQL sniffer
在沒有sys庫,甚至不知道密碼,擔心general log影響性能,或者對連接配接情況進
行分析的話,可以通過MySQL sniffer對端口通信進行抓包分析。
是360開源的一款連接配接層抓包分析工具。
1.安裝:
<code>root# git clone [email protected]:Qihoo360/mysql-sniffer.git</code>
<code>root# cd mysql-sniffer</code>
<code>root# yum -y install glib2-devel libpcap-devel libnet-devel cmake</code>
<code>root# yum -y install gcc gcc-c++</code>
<code>root# mkdir sniffer</code>
<code>root# make&&cmake</code>
2.使用
<code>root# cd sniffer</code>
<code>root# ./mysql-sniffer -h<br/></code>Usage ./mysql-sniffer [-d] -i eth0 -p 3306,3307,3308 -l /var/log/mysql-sniffer/ -e stderr
[-d] -i eth0 -r 3000-4000
-d daemon mode. #背景方式
-s how often to split the log file(minute, eg. 1440). if less than 0, split log everyday #日志切割頻率
-i interface. Default to eth0 #網卡接口
-p port, default to 3306. Multiple ports should be splited by ','. eg. 3306,3307 this option has no effect when -f is set. #監聽的端口
-r port range, Dont use -r and -p at the same time #端口範圍
-l query log DIRECTORY. Make sure that the directory is accessible. Default to stdout. #輸出到指定目錄下面的端口.log檔案中,不在輸出到螢幕
-e error log FILENAME or 'stderr'. if set to /dev/null, runtime error will not be recorded #輸出錯誤資訊到指定的錯誤檔案中
-f filename. use pcap file instead capturing the network interface #使用給定的Pcap檔案進行分析,而不是分析實時的網卡資訊
-w white list. dont capture the port. Multiple ports should be splited by ','. #白名單,不捕獲這些端口
-t truncation length. truncate long query if it is longer than specified length. Less than 0 means no truncation #控制記錄捕獲的SQL的長度
-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`
3.使用方法:
先使用ip addr指令觀察自己的網卡,再指定端口,指定日志檔案
例如:./mysql-sniffer -d -i ens192 -p 3306,3307 -l /data/log/3306gen.log
./mysql-sniffer -i lo -r 3306-3310
注:本機流量通過lo也就是回環口進行監聽。
本文轉自 angry_frog 51CTO部落格,原文連結:http://blog.51cto.com/l0vesql/2052056