這兩天看到群里發(fā)了一個開源軟件,是關(guān)于MYSQL實時抓包的工具。頓時讓我想到了之前做故障定位時的一些需求,之前針對連接數(shù)據(jù)庫的連接信息進行審計定位,做過MYSQL自身的審計功能開啟(有不少坑,慎用),也通過tcpdump做過一些分析(直接讀分析日志比較難以看明白,在數(shù)據(jù)庫連接abort值高時使用)。而實時的連接監(jiān)控、分析,我們一般使用mysqladmin/show full processlist 命令查看,也可直接查詢processlist表。但長時間監(jiān)控連接數(shù)據(jù)庫的應(yīng)用IP、操作記錄,目前沒有什么特別好的工具,此工具讓我眼前一亮,頓時有嘗試試用看看的想法,它的名字就是:MySQL Sniffer。 MySQL Sniffer簡介:(來自開源工具的readme) MySQL Sniffer 是一個基于 MySQL 協(xié)議的抓包工具,實時抓取 MySQLServer 端或 Client 端請求,并格式化輸出。輸出內(nèi)容包括訪問時間、訪問用戶、來源 IP、訪問 Database、命令耗時、返回數(shù)據(jù)行數(shù)、執(zhí)行語句等。有批量抓取多個端口,后臺運行,日志分割等多種使用方式,操作便捷,輸出友好。同時也適用抓取 Atlas 端的請求,Atlas 是奇虎開源的一款基于MySQL協(xié)議的數(shù)據(jù)中間層項目,項目地址:[https://github.com/Qihoo360/Atlas](https://github.com/Qihoo360/Atlas)。同類型工具還有vc-mysql-sniffer,以及 tshark 的 -e mysql.query 參數(shù)來解析 MySQL 協(xié)議。 MySQL Sniffer安裝使用: (1)安裝環(huán)境 建議在 centos6.2 及以上編譯安裝,并用 root 運行 (2)依賴包 glib2.0、libpcap、libnet (3)下載地址 https://github.com/Qihoo360/mysql-sniffer git clone git@github.com:Qihoo360/mysql-sniffer.git (4)安裝過程 [root@virtual01 mysql_sniff]# unzip mysql-sniffer-master.zip [root@virtual01 mysql_sniff]# cd mysql-sniffer-master [root@virtual01 mysql-sniffer-master]# cmake ./ [root@virtual01 mysql-sniffer-master]# make -j 8 [root@virtual01 mysql-sniffer-master]# cd ../ [root@virtual01 mysql_sniff]# mv mysql-sniffer-master /usr/local/mysql_sniffer [root@virtual01 mysql_sniff]# ln -s /usr/local/mysql_sniffer/bin/mysql-sniffer /usr/bin/ (5)命令參數(shù) [root@cyou_virtual01 ~]# mysql-sniffer -h 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 (多久分隔一次日志,分鐘為單位,如果小于0,按天分隔) -i interface. Default to eth0(指定監(jiān)控網(wǎng)卡接口) -p port, default to 3306. Multiple ports should be splited by ','. eg. 3306,3307 (指定監(jiān)聽的端口,多端口用逗號分隔) this option has no effect when -f is set. -r port range, Don't use -r and -p at the same time (指定端口范圍,例如 -r 2000-5000) -l query log DIRECTORY. Make sure that the directory is accessible. Default to stdout.(指定輸出日志的目錄,文件名義端口命名,例如3306.log,監(jiān)聽進程終止后輸出到日志) -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代替監(jiān)聽) -w white list. dont capture the port. Multiple ports should be splited by ','.(白名單,不監(jiān)控的端口) -t truncation length. truncate long query if it's longer than specified length. Less than 0 means no truncation (超過查詢長度,截斷處理) -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(tcp流數(shù)量,默認(rèn)65536) (6)使用案例