前言
MySQL 數(shù)據(jù)庫最常見的兩個瓶頸是CPU和I/O的瓶頸。CPU在飽和的時候一般發(fā)生在數(shù)據(jù)裝入內(nèi)存或從磁盤上讀取數(shù)據(jù)時候,磁盤I/O瓶頸發(fā)生在裝入數(shù)據(jù)遠大于內(nèi)存容量的時候。
MySQL數(shù)據(jù)庫性能遇到瓶頸,如何快速定位問題的原因,是每個DBA或系統(tǒng)運維人員應該思考的問題。正確的借助一些性能分析工具,能夠幫助DBA或系統(tǒng)運維人員進行問題快速的定位。
下面小編匯總了一些 MySQL DBA工作中好用的性能分析工具,歡迎大家收藏轉(zhuǎn)發(fā)~
pt-query-digest主要用來分析mysql的慢日志,與mysqldumpshow工具相比,pt-querydigest 工具的分析結(jié)果更具體,更完善。pt-querydigest是PT工具集的子集。
1.1 安裝
1.2 用法
1、直接分析慢查詢文件:
pt-query-digest /var/lib/mysql/slowtest-slow.log > slow_report.log
2、分析最近12小時內(nèi)的查詢:
3、分析指定時間范圍內(nèi)的查詢:
pt-query-digest /var/lib/mysql/slowtest-slow.log --since '2017-01-07 09:30:00' --until '2017-01-07 10:00:00'> > slow_report3.log
4、分析指含有select語句的慢查詢
5、針對某個用戶的慢查詢
pt-query-digest --filter '($event->{user} || '') =~ m/^root/i' /var/lib/mysql/slowtest-slow.log> slow_report5.log
6、查詢所有所有的全表掃描或full join的慢查詢
innotop是一個MySQL和InnoDB事務/狀態(tài)監(jiān)視器,它顯示查詢、InnoDB事務、鎖等待、死鎖、外鍵錯誤,打開表,復制狀態(tài),緩沖區(qū)信息,行操作、日志、I/O操作、加載圖等等。你可以使用innotop同時監(jiān)控多個服務器。innotp可以綜合了解你的 MySQL。
2.1、安裝innotop
yum install innotop-1.11.4-1.el7.noarch
2.2、使用方法
1、基本使用
2、參數(shù)說明,如下圖:
注意::?可以切換至其他命令。
是taobao開源出來一個數(shù)據(jù)庫實時性能查看工具,借助此工具你可以時刻了解的你的數(shù)據(jù)庫的性能情況。
3.1 使用方法:
./orzdba_remote --host=192.168.1.181 --user='admin' --password=123456 --port=3306 -mysql -sys 2>/dev/null
參數(shù)說明:
--host:指定主機
--user:指定用戶名
--password:數(shù)據(jù)庫密碼
--port:數(shù)據(jù)庫的端口號
4.1 安裝tcp包
4.2 使用
1、通過tcpdump抓包
tcpdump -i any port 3306 -l -s 0 -w - |strings |grep -A 5 select|less
2、tcpdump pt-query-digest
5.1 pt-ioprofile
pt-ioprofile定位負載來源文件,通過ps找出負載較高的進程。
5.2 使用
pt-ioprofile --profile-pid=12036 --cell=sizes
參數(shù)說明:
--profile-pid:mysql進程的id
--cell-sizes:該參數(shù)將結(jié)果已 B/s 的方式展示出來
通過響應時間判斷數(shù)據(jù)庫運行狀況
6.1 安裝tcprstat
如果是在64位操作系統(tǒng)中使用,可以直接下載二進制文件使用。步驟如下:
1)下載文件 http://github.com/downloads/Lowercases/tcprstat/tcprstat-static.v0.3.1.x86_64
2)把下載的文件移動到 /usr/bin
3)把文件名修改為 tcprstat
4)修改文件權(quán)限,增加執(zhí)行權(quán)限 chmod x /usr/bin/tcprstat 如果你想在32位操作系統(tǒng)中使用,那你只能自己編譯了。
代碼下載地址 https://github.com/Lowercases/tcprstat https://launchpad.net/tcprstat
6.2 使用
每個請求的時間在0.1ms~0.124ms
參數(shù)說明:
--p:數(shù)據(jù)庫端口號
-t:刷新間隔時間
-n:輸出次數(shù)
nicstat網(wǎng)絡利器,充分了解你的網(wǎng)卡運行狀況)
7.1、安裝nicstat
yum install http://rpmfind.net/linux/fedora/linux/releases/28/Everything/x8664/os/Packages/n/nicstat-1.95-7.fc27.x8664.rpm
7.2、使用方法
參數(shù)說明:
-z:跳過0行
輸出參數(shù)說明:
wKB/s,OutKB #每秒寫的千字節(jié)數(shù)(transmitted)
rMbps,RdMbps #每秒讀的百萬字節(jié)數(shù)K(received)
%Util #接口的利用率百分比
Sat #每秒的錯誤數(shù),接口接近飽和的一個指標
8.1 安裝dstat
yum install dstat -y
8.2 使用
參數(shù)說明:
-t:enable time/date output
-c:enable cpu stats
-l:enable load stats
-m:enable memory stats
-n:enable network stats
-d:enable disk stats
-y:enable system stats
vmtouch是一個學習和控制unix和類unix系統(tǒng)的文件系統(tǒng)緩存的工具。
9.1、快速安裝
$ git clone https://github.com/hoytech/vmtouch.git$ cd vmtouch$ make$ sudo make install
9.2、使用方法
顯示
回收
vmtouch -ve a.txt
Oprofile 是一個開源的profiling工具,通過取樣來工作,是一個全局的抽樣統(tǒng)計工具。cpu無端占用高?應用程序響應慢?苦于沒有分析的工具?找它就對了!通過計數(shù)采樣,幫助我們從進程、函數(shù)、代碼層面找出占用cpu的'罪魁禍首'。
10.1 安裝
10.2 使用方法
1、使用
#加載oprofile內(nèi)核模塊 opcontrol --init #我們對內(nèi)核的取樣沒興趣 opcontrol --setup --no-vmlinux#在開始收集采樣數(shù)據(jù)前回顧下我們的設(shè)置 opcontrol --status#清除上一次采樣到的數(shù)據(jù) opcontrol --reset #運行我們的程序 opcontrol --start #收集采樣數(shù)據(jù) opcontrol --dump#關(guān)閉守護程序, 同時準備好采樣的數(shù)據(jù) opcontrol --shutdown
注意:如報Cannot find event CPUCLKUNHALTED,解決方法如下:
2、獲取采樣信息
#系統(tǒng)級別的 opreport --long-filenames#模塊級別的 opreport image:foo -l#源碼級別的 opannotate image:foo -s
3、使用樣例