十大MySQL性能分析工具匯總!專治MySQL性能瓶頸
前言
MySQL 數(shù)據(jù)庫最常見的兩個(gè)瓶頸是CPU和I/O的瓶頸。CPU在飽和的時(shí)候一般發(fā)生在數(shù)據(jù)裝入內(nèi)存或從磁盤上讀取數(shù)據(jù)時(shí)候,磁盤I/O瓶頸發(fā)生在裝入數(shù)據(jù)遠(yuǎn)大于內(nèi)存容量的時(shí)候。
MySQL數(shù)據(jù)庫性能遇到瓶頸,如何快速定位問題的原因,是每個(gè)DBA或系統(tǒng)運(yùn)維人員應(yīng)該思考的問題。正確的借助一些性能分析工具,能夠幫助DBA或系統(tǒng)運(yùn)維人員進(jìn)行問題快速的定位。
下面小編匯總了一些 MySQL DBA工作中好用的性能分析工具,歡迎大家收藏轉(zhuǎn)發(fā)~
一、 Pt-qurey-digest
pt-query-digest主要用來分析mysql的慢日志,與mysqldumpshow工具相比,pt-querydigest 工具的分析結(jié)果更具體,更完善。pt-querydigest是PT工具集的子集。
1. 安裝
- yum install percona-toolkit-3.0.13-1.el7.x86_64.rpm
2. 用法
(1) 直接分析慢查詢文件:
- pt-query-digest /var/lib/mysql/slowtest-slow.log > slow_report.log
(2) 分析最近12小時(shí)內(nèi)的查詢:
- pt-query-digest --since=12h /var/lib/mysql/slowtest-slow.log > slow_report2.log
(3) 分析指定時(shí)間范圍內(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語句的慢查詢
- pt-query-digest --filter '$event->{fingerprint} =~ m/^select/i' /var/lib/mysql/slowtest-slow.log> slow_report4.log
(5) 針對某個(gè)用戶的慢查詢
- pt-query-digest --filter '($event->{user} || "") =~ m/^root/i' /var/lib/mysql/slowtest-slow.log> slow_report5.log
(6) 查詢所有所有的全表掃描或full join的慢查詢
- pt-query-digest --filter '(($event->{Full_scan} || "") eq "yes") ||(($event->{Full_join} || "") eq "yes")' /var/lib/mysql/slowtest-slow.log> slow_report6.log
二、Innotop
innotop是一個(gè)MySQL和InnoDB事務(wù)/狀態(tài)監(jiān)視器,它顯示查詢、InnoDB事務(wù)、鎖等待、死鎖、外鍵錯(cuò)誤,打開表,復(fù)制狀態(tài),緩沖區(qū)信息,行操作、日志、I/O操作、加載圖等等。你可以使用innotop同時(shí)監(jiān)控多個(gè)服務(wù)器。innotp可以綜合解你的 MySQL。
1. 安裝innotop
- yum install innotop-1.11.4-1.el7.noarch
2. 使用方法
(1) 基本使用
- innotop --host 192.168.1.181 --user admin --password 123456 --port 3306 --delay 1 -m Q
(2) 參數(shù)說明,如下圖:
注意::?可以切換至其他命令。
三、 Orzdba
是taobao開源出來一個(gè)數(shù)據(jù)庫實(shí)時(shí)性能查看工具,借助此工具你可以時(shí)刻了解的你的數(shù)據(jù)庫的性能情況。
使用方法:
- ./orzdba_remote --host=192.168.1.181 --user="admin" --password=123456 --port=3306 -mysql -sys 2>/dev/null
參數(shù)說明:
- --host:指定主機(jī)
- --user:指定用戶名
- --password:數(shù)據(jù)庫密碼
- --port:數(shù)據(jù)庫的端口號(hào)
四、 Tcp抓包
1. 安裝tcp包
- yum install tcpdump-4.9.2-3.el7.x86_64 -y
2. 使用
(1) 通過tcpdump抓包
- tcpdump -i any port 3306 -l -s 0 -w - |strings |grep -A 5 select|less
(2) tcpdump+pt-query-digest
- tcpdump -s 65535 -x -nn -q -tttt -i any -c 1000 port 3306 > mysql.tcp.txt
- pt-query-digest --type tcpdump mysql.tcp.txt> slow_report9.log
五、ioprofile
1. pt-ioprofile
pt-ioprofile定位負(fù)載來源文件,通過ps找出負(fù)載較高的進(jìn)程。
2. 使用
- pt-ioprofile --profile-pid=12036 --cell=sizes
參數(shù)說明:
- --profile-pid:mysql進(jìn)程的id
- --cell-sizes:該參數(shù)將結(jié)果已 B/s 的方式展示出來
六、Tcprstat
通過響應(yīng)時(shí)間判斷數(shù)據(jù)庫運(yùn)行狀況
1. 安裝tcprstat
如果是在64位操作系統(tǒng)中使用,可以直接下載二進(jìn)制文件使用。步驟如下:
- 下載文件 http://github.com/downloads/Lowercases/tcprstat/tcprstat-static.v0.3.1.x86_64
- 把下載的文件移動(dòng)到 /usr/bin
- 把文件名修改為 tcprstat
- 修改文件權(quán)限,增加執(zhí)行權(quán)限 chmod +x /usr/bin/tcprstat 如果你想在32位操作系統(tǒng)中使用,那你只能自己編譯了。
代碼下載地址 https://github.com/Lowercases/tcprstat https://launchpad.net/tcprstat
2. 使用
- [root@localhost ~]# tcprstat --p 3306 -t 1 -n 10
- timestamp count max min avg med stddev 95_max 95_avg 95_std 99_max 99_avg 99_std
- 1539760803 1 103 103 103 103 0 0 0 0 0 0 0
- 1539760804 1 108 108 108 108 0 0 0 0 0 0 0
- 1539760805 1 124 124 124 124 0 0 0 0 0 0 0
- 1539760806 1 115 115 115 115 0 0 0 0 0 0 0
- 1539760807 1 112 112 112 112 0 0 0 0 0 0 0
每個(gè)請求的時(shí)間在0.1ms~0.124ms
參數(shù)說明:
- --p:數(shù)據(jù)庫端口號(hào)
- -t:刷新間隔時(shí)間
- -n:輸出次數(shù)
七、 Nicstat
nicstat網(wǎng)絡(luò)利器,充分了解你的網(wǎng)卡運(yùn)行狀況)
1. 安裝nicstat
- yum install http://rpmfind.net/linux/fedora/linux/releases/28/Everything/x8664/os/Packages/n/nicstat-1.95-7.fc27.x8664.rpm
2. 使用方法
- [root@lkjtest ~]# nicstat -z 1
- Time Int rKB/s wKB/s rPk/s wPk/s rAvs wAvs %Util Sat
- 15:29:14 ens160 4.03 0.91 43.18 1.60 95.61 581.8 0.00 0.00
- 15:29:15 ens160 3.09 0.73 35.95 2.00 88.11 375.5 0.00 0.00
- 15:29:16 ens160 3.93 0.66 43.99 2.00 91.52 335.5 0.00 0.00
- 15:29:17 ens160 3.99 0.66 45.00 2.00 90.71 335.5 0.00 0.00
- 15:29:18 ens160 4.04 0.66 46.99 2.00 88.04 335.5 0.00 0.00
- 15:29:19 ens160 3.64 0.66 42.00 2.00 88.76 335.5 0.00 0.00
參數(shù)說明:
- -z:跳過0行
輸出參數(shù)說明:
- wKB/s,OutKB #每秒寫的千字節(jié)數(shù)(transmitted)
- rMbps,RdMbps #每秒讀的百萬字節(jié)數(shù)K(received)
- %Util #接口的利用率百分比
- Sat #每秒的錯(cuò)誤數(shù),接口接近飽和的一個(gè)指標(biāo)
八、 Dstat
1. 安裝dstat
- yum install dstat -y
2. 使用
- [root@localhost ~]# dstat -tclmndy 1
參數(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
vmtouch是一個(gè)學(xué)習(xí)和控制unix和類unix系統(tǒng)的文件系統(tǒng)緩存的工具。
1. 快速安裝
- $ git clone https://github.com/hoytech/vmtouch.git
- $ cd vmtouch
- $ make
- $ sudo make install
2. 使用方法
顯示
- $ vmtouch -v big-dataset.txt
回收
- vmtouch -ve a.txt
十、 oprofile
Oprofile 是一個(gè)開源的profiling工具,通過取樣來工作,是一個(gè)全局的抽樣統(tǒng)計(jì)工具。cpu無端占用高?應(yīng)用程序響應(yīng)慢?苦于沒有分析的工具?找它就對了!通過計(jì)數(shù)采樣,幫助我們從進(jìn)程、函數(shù)、代碼層面找出占用cpu的"罪魁禍?zhǔn)?quot;。
1. 安裝
- yum install http://www.rpmfind.net/linux/centos/7.5.1804/os/x86_64/Packages/oprofile-0.9.9-25.el7.x86_64.rpm -y
2. 使用方法
(1) 使用
- #加載oprofile內(nèi)核模塊 opcontrol --init
- #我們對內(nèi)核的取樣沒興趣 opcontrol --setup --no-vmlinux
- #在開始收集采樣數(shù)據(jù)前回顧下我們的設(shè)置 opcontrol --status
- #清除上一次采樣到的數(shù)據(jù) opcontrol --reset
- #運(yùn)行我們的程序 opcontrol --start
- #收集采樣數(shù)據(jù) opcontrol --dump
- #關(guān)閉守護(hù)程序, 同時(shí)準(zhǔn)備好采樣的數(shù)據(jù) opcontrol --shutdown
注意:如報(bào)Cannot find event CPUCLKUNHALTED,解決方法如下:
- #解決步驟
- $sudo opcontrol --deinit
- Daemon not running
- Unloading oprofile module
- $sudo modprobe oprofile timer=1
- $dmesg|grep oprofile|tail -n 1
- oprofile: using timer interrupt.
- 如果你看到上面的字說明你成功了。
- 我們再試驗(yàn)下:
- $sudo opcontrol --init && sudo opcontrol --reset && sudo opcontrol --start
- Using 2.6+ OProfile kernel interface.
- Using log file /var/lib/oprofile/samples/oprofiled.log
- Daemon started.
- Profiler running.
(2) 獲取采樣信息
- #系統(tǒng)級別的 opreport --long-filenames
- #模塊級別的 opreport image:foo -l
- #源碼級別的 opannotate image:foo -s
(3) 使用樣例
- opcontrol --deinit
- modprobe oprofile timer=1
- $dmesg|grep oprofile|tail -n 1
- (oprofile: using timer interrupt.)
- opcontrol --reset
- pcontrol --separate=lib --no-vmlinux
- --start --image=/home/mysql_user/mysqlhome/bin/mysqld
- opcontrol --dump
- opcontrol --shutdown
- opreport -l /home/mysql_user/mysqlhome/bin/mysqld