linux mysql io压力大_MySQL性能调优(四) Linux 磁盘IO
1. IO處理過(guò)程
磁盤(pán)IO經(jīng)常會(huì)成為系統(tǒng)的一個(gè)瓶頸,特別是對(duì)于運(yùn)行數(shù)據(jù)庫(kù)的系統(tǒng)而言。數(shù)據(jù)從磁盤(pán)讀取到內(nèi)存,在到CPU緩存和寄存器,然后進(jìn)行處理,最后寫(xiě)回磁盤(pán),中間要經(jīng)過(guò)很多的過(guò)程,下圖是一個(gè)以write為例的 Linux 磁盤(pán)IO子系統(tǒng)的架構(gòu):
可以看到IO操作分成了四個(gè)層面:
1)文件系統(tǒng)緩存:處理數(shù)據(jù)必須先從磁盤(pán)讀到緩存,然后修改,然后刷會(huì)磁盤(pán)。緩存的刷新涉及到兩個(gè)參數(shù):vm.dirty_background_ratio、vm.dirty_ratio。還有刷新寫(xiě)回時(shí),使用到 bio 結(jié)構(gòu),bio的組成是由磁盤(pán)上相鄰的block組成的,所以這里進(jìn)行了優(yōu)化。
2)block layer:該層就涉及到 IO調(diào)度算法,IO調(diào)度算法在mysql服務(wù)器是一個(gè)很重要的調(diào)優(yōu)手段。系統(tǒng)中所有進(jìn)程申請(qǐng)的IO操作,全部在這里進(jìn)行排隊(duì),等待調(diào)度,然后寫(xiě)回磁盤(pán)。調(diào)度算法有四種:
1> Anticipatory: 適用于個(gè)人PC,單磁盤(pán)系統(tǒng);
2> CFQ(Complete Fair Queuing):默認(rèn)的IO調(diào)度算法,完全公平的排隊(duì)調(diào)度算法。每一個(gè)進(jìn)程的IO請(qǐng)求會(huì)安排進(jìn)一個(gè)專門的IO隊(duì)列,然后按照進(jìn)程組來(lái)公平的調(diào)度IO,也就是每一個(gè)進(jìn)程組之間按照公平的方式來(lái)調(diào)度IO。顯然他適合多用戶的系統(tǒng),但是極為不適合作為數(shù)據(jù)庫(kù)系統(tǒng)的IO調(diào)度算法,因?yàn)轱@而易見(jiàn),數(shù)據(jù)庫(kù)系統(tǒng)中,數(shù)據(jù)庫(kù)進(jìn)程肯定是IO最多的一個(gè)進(jìn)程組,然后它卻只能獲得和其它進(jìn)程一樣多的IO調(diào)度機(jī)會(huì)。所以顯然這是極為不合理的。數(shù)據(jù)庫(kù)系統(tǒng)絕對(duì)不要使用該調(diào)度算法。
3> Deadline: 按照截止期限來(lái)循環(huán)在各個(gè)IO隊(duì)列中進(jìn)行調(diào)度,所以它提供了一個(gè)近實(shí)時(shí)的IO系統(tǒng),并且磁盤(pán)throughput也很好,也不會(huì)造成starvation.一般mysql系統(tǒng)建議采用該調(diào)度算法。
4> NOOP: 簡(jiǎn)單的FIFO隊(duì)列進(jìn)行調(diào)度,No operation的意思是,它沒(méi)有進(jìn)行額外的將臨近的IO進(jìn)行合并的操作,所以它對(duì)CPU的使用極少。該調(diào)度算法特別適合于SSD。因?yàn)镾SD在對(duì)待順序IO和隨機(jī)IO沒(méi)有什么區(qū)別。所以它不需要對(duì)臨近的IO進(jìn)行合并。避免了合并操作對(duì)CPU的使用。
所以一般而言,對(duì)于mysql的系統(tǒng),如果是SSD,那么應(yīng)該使用NOOP調(diào)度算法,如果是磁盤(pán),就應(yīng)該使用Deadline調(diào)度算法。
查看與修改IO調(diào)度算法:
臨時(shí)修改:
[root@localhost ~]# cat /sys/block/sda/queue/scheduler
noop anticipatory deadline [cfq]
[root@localhost ~]# echo noop > /sys/block/sda/queue/scheduler
[root@localhost ~]# cat /sys/block/sda/queue/scheduler
[noop] anticipatory deadline cfq
永久修改:
# vi /boot/grub/menu.lst
更改到如下內(nèi)容:
kernel /boot/vmlinuz-2.6.18-8.el5 ro root=LABEL=/ elevator=deadline rhgb quiet
重啟之后,查看調(diào)度方法:
# cat /sys/block/sda/queue/scheduler
noop anticipatory [deadline] cfq
已經(jīng)是deadline了
3)磁盤(pán)驅(qū)動(dòng)層:對(duì)于順序讀系統(tǒng)而言,很容易在磁盤(pán)接口層的帶寬上成為瓶頸所在;
4)磁盤(pán):對(duì)于隨機(jī)讀多的系統(tǒng)而言,磁盤(pán)很容易成為瓶頸所在,一般的優(yōu)化就是使用RAID或者換SSD;
2. IO瓶頸檢測(cè)
2.1 使用 iostat 查看磁盤(pán)IO
顯示單位問(wèn)題:默認(rèn)iostat是以磁盤(pán)的block為單位,也可以使用 -k 來(lái)指定以 kilobytes 為單位,或者使用 -m 指定 megabytes 為單位;
統(tǒng)計(jì)開(kāi)始時(shí)間問(wèn)題:默認(rèn)iostat和vmstat相似,默認(rèn)第一次/行都是從開(kāi)機(jī)到目前的一個(gè)數(shù)據(jù),可以使用 -y 選項(xiàng)去掉第一次/行的數(shù)據(jù);
CPU與磁盤(pán): iostat 默認(rèn)會(huì)顯示cpu和磁盤(pán)的數(shù)據(jù),如果只要cpu數(shù)據(jù)可以使用 -c 選項(xiàng),如果只需要磁盤(pán)數(shù)據(jù),可以使用 -d 選項(xiàng);
時(shí)間間隔和重復(fù)次數(shù):[interval [times]] 表示磁盤(pán)統(tǒng)計(jì)數(shù)據(jù)的間隔時(shí)間和次數(shù);
-x : 該選項(xiàng)顯示具體的擴(kuò)展信息;
[root@localhost ~]# iostat
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.60 0.00 7.80 0.31 0.00 91.30
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
scd0 0.02 0.21 0.00 536 0
sda 2.00 78.60 8.43 198702 21312[root@localhost ~]# iostat -c
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.48 0.00 6.51 0.25 0.00 92.76
[root@localhost ~]# iostat -d -k
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.02 0.08 0.00 268 0
sda 1.69 31.17 4.15 99363 13224
[root@localhost ~]# iostat -d -m
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
scd0 0.02 0.00 0.00 0 0
sda 1.69 0.03 0.00 97 12
[root@localhost ~]# iostat -d -m -x
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
scd0 0.02 0.00 0.02 0.00 0.00 0.00 10.94 0.00 4.96 4.88 0.01
sda 1.22 0.48 1.13 0.56 0.03 0.00 41.66 0.01 6.83 5.27 0.89
[root@localhost ~]# iostat -d -m -x 2 3
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
scd0 0.02 0.00 0.01 0.00 0.00 0.00 10.94 0.00 4.96 4.88 0.01
sda 1.19 0.48 1.10 0.55 0.03 0.00 41.52 0.01 6.81 5.25 0.87
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
scd0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
scd0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda 0.00 0.00 0.00 0.51 0.00 0.00 8.00 0.00 3.00 3.00 0.15
[root@localhost ~]# iostat -y -d -m -x 2 3
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
scd0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
scd0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
scd0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
字段含義:
Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn 分別表示:每秒讀取block的個(gè)數(shù),每秒寫(xiě)block的個(gè)數(shù),總共讀了多少個(gè)block,總共寫(xiě)了多少個(gè)block
tps: Indicate the number of transfers per second that were issued to the device. A transfer is an I/O request to the device. Multiple logical requests can be combined into a single I/O request to the device. A transfer is of indeterminate size.(就是對(duì)磁盤(pán)每秒請(qǐng)求多少次IO操作)
rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
rrqm/s wrqm/s 表示的磁盤(pán)讀和寫(xiě)時(shí)每秒發(fā)生多少次相鄰磁盤(pán)的merge操作;rrqm: read request merge; wrqm:write request merge
r/s w/s 表示每秒讀的次數(shù),寫(xiě)的次數(shù);
rMB/s wMB/s 表示每秒讀多少M(fèi)B,寫(xiě)多少M(fèi)B
avgrq-sz:The average size (in sectors) of the requests that were issued to the device. 平均一個(gè)IO請(qǐng)求涉及到多少個(gè)sector
avgqu-sz:The average queue length of the requests that were issued to the device.IO隊(duì)列的平均長(zhǎng)度,該數(shù)值很重要。
await:The average time (in milliseconds) for I/O requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.平均每一個(gè)IO花費(fèi)了多少毫秒(包括在IO隊(duì)列中的排隊(duì)時(shí)間和讀寫(xiě)操作花費(fèi)的時(shí)間)。這里可以理解為IO的響應(yīng)時(shí)間,一般地系統(tǒng)IO響應(yīng)時(shí)間應(yīng)該低于5ms,如果大于10ms就比較大了。
svctm:棄用
%util :Percentage of CPU time during which I/O requests were issued to the device (bandwidth utilization for the device). Device saturation occurs when this value is close to 100%. 在統(tǒng)計(jì)時(shí)間內(nèi)所有處理IO時(shí)間,除以總共統(tǒng)計(jì)時(shí)間。例如,如果統(tǒng)計(jì)間隔1秒,該設(shè)備有0.8秒在處理IO,而0.2秒閑置,那么該設(shè)備 的%util = 0.8/1 = 80%,所以該參數(shù)暗示了設(shè)備的繁忙程度。一般地,如果該參數(shù)是100%表示設(shè)備已經(jīng)接近滿負(fù)荷運(yùn)行了(當(dāng)然如果是多磁盤(pán),即使%util是100%,因 為磁盤(pán)的并發(fā)能力,所以磁盤(pán)使用未必就到了瓶頸)。
CPU的 %iowait io等待很高;
磁盤(pán)的 avgqu-sz數(shù)值很大;await數(shù)值很高;%util數(shù)值很高;都可能預(yù)示著磁盤(pán)存在瓶頸或者磁盤(pán)出現(xiàn)問(wèn)題或者故障。
2.2 使用 iostat 查看磁盤(pán)每個(gè)分區(qū)的IO
上面查看的都是整個(gè)磁盤(pán)的IO情況,下面的命令可以查看具體某個(gè)磁盤(pán)的所有分區(qū)的IO情況:
[root@localhost ~]# iostat -x -d -m -p sda 2 3
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
sda 0.82 0.43 0.76 0.52 0.02 0.00 38.49 0.01 6.27 4.82 0.62
sda1 0.80 0.42 0.53 0.51 0.02 0.00 45.09 0.01 6.92 5.50 0.57
sda2 0.01 0.02 0.12 0.01 0.00 0.00 9.70 0.00 2.95 2.79 0.04
sda3 0.01 0.00 0.07 0.00 0.00 0.00 8.67 0.00 3.72 3.65 0.03
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[root@localhost ~]# iostat -d -m -p sda 2 3
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda 1.25 0.02 0.00 97 18
sda1 1.02 0.02 0.00 92 17
sda2 0.13 0.00 0.00 2 0
sda3 0.07 0.00 0.00 1 0
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda 0.00 0.00 0.00 0 0
sda1 0.00 0.00 0.00 0 0
sda2 0.00 0.00 0.00 0 0
sda3 0.00 0.00 0.00 0 0
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda 0.00 0.00 0.00 0 0
sda1 0.00 0.00 0.00 0 0
sda2 0.00 0.00 0.00 0 0
sda3 0.00 0.00 0.00 0 0
2.3 使用 vmstat 查看磁盤(pán)IO
[root@localhost ~]# vmstat 2 4
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 454900 21808 76776 0 0 20 4 97 81 0 5 95 0 0
0 0 0 454892 21808 76772 0 0 0 8 83 106 0 3 97 0 0
1 0 0 454760 21816 76772 0 0 0 44 101 153 1 5 94 0 0
0 0 0 454760 21816 76784 0 0 0 0 57 68 0 1 99 0 0
bi: Blocks received from a block device (blocks/s). 每秒讀取多少個(gè)block到內(nèi)存
bo: Blocks sent to a block device (blocks/s). 每秒內(nèi)存寫(xiě)出多少個(gè)block到磁盤(pán)
2.4 使用 sar -b 查看磁盤(pán)IO
[root@localhost ~]# sar -b 2 4
Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/09/2015 _i686_ (1 CPU)
03:53:21 PM tps rtps wtps bread/s bwrtn/s
03:53:23 PM 0.00 0.00 0.00 0.00 0.00
03:53:25 PM 0.00 0.00 0.00 0.00 0.00
03:53:27 PM 0.00 0.00 0.00 0.00 0.00
03:53:29 PM 0.00 0.00 0.00 0.00 0.00
Average: 0.00 0.00 0.00 0.00 0.00
tps: 上面有介紹;rtps: 表示讀的tps;wtps: 表示寫(xiě)的tps;
bread/s: 每秒讀多少個(gè)block;bwrtn/s: 每秒寫(xiě)多少個(gè)block;
2.5 使用 iotop 找到IO最多的進(jìn)程/線程
iotop類似于top命令,默認(rèn)按照IO排序:
iotop :
iotop 是可以交互的:
Use? the? left? and? right arrows to change the sorting, r to reverse the sorting order, o to toggle the --only
option, p to toggle the --processes option, a to toggle the --accumulated option, q to quit or i to change? the
priority of a thread or a process’ thread(s). Any other key will force a refresh.
1)利用左右鍵 可以選擇排序的字段,默認(rèn)按照IO>倒序,可以按照SWAPIN,DISK WRITE 等等字段排序,使用左右方向鍵即可;
2)利用 p鍵 可以在按照 進(jìn)程顯示 和按照 線程顯示之間切換;
3)r 鍵可以改變排序的方向:倒序 和 順序
查看mysqld的IO:
iotop -k -u mysql (-k 表示KB,-u mysql表示顯示mysql用戶的所有進(jìn)程的IO):
3. 實(shí)例分析
$$iostat -d -k 1 |grep sda10
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda10 60.72 18.95 71.53 395637647 1493241908
sda10 299.02 4266.67 129.41 4352 132
sda10 483.84 4589.90 4117.17 4544 4076
sda10 218.00 3360.00 100.00 3360 100
sda10 546.00 8784.00 124.00 8784 124
sda10 827.00 13232.00 136.00 13232 136
上面看到,磁盤(pán)每秒傳輸次數(shù)平均約400;每秒磁盤(pán)讀取約5MB,寫(xiě)入約1MB。
iostat -d -x -k 1
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util
sda 1.56 28.31 7.84 31.50 43.65 3.16 21.82 1.58 1.19 0.03 0.80 2.61 10.29
sda 1.98 24.75 419.80 6.93 13465.35 253.47 6732.67 126.73 32.15 2.00 4.70 2.00 85.25
sda 3.06 41.84 444.90 54.08 14204.08 2048.98 7102.04 1024.49 32.57 2.10 4.21 1.85 92.24
可以看到磁盤(pán)的平均響應(yīng)時(shí)間<5ms,磁盤(pán)使用率>80。磁盤(pán)響應(yīng)正常,但是已經(jīng)很繁忙了。
4. 磁盤(pán)IO優(yōu)化
磁盤(pán)IO在優(yōu)化之前,首先要弄清楚系統(tǒng)的IO情況,是隨機(jī)IO多,還是順序IO多,是大文件IO多,還是小文件IO多(小文件IO一般也就是隨機(jī)IO)。比如隨機(jī)IO多,那么就可以通過(guò)加磁盤(pán)使用RAID技術(shù)來(lái)優(yōu)化,如果是順序IO遇到瓶頸,一般可能是磁盤(pán)驅(qū)動(dòng)的帶寬有瓶頸,就可以換一個(gè)更快的disk controller。搞清楚是磁盤(pán)有瓶頸,還是磁盤(pán)驅(qū)動(dòng)的帶寬有瓶頸。
因?yàn)榇疟P(pán)IO的操作分成了4個(gè)層面,所以IO的優(yōu)化也可以從這四個(gè)方面入手:
1)正對(duì)mysql系統(tǒng)的調(diào)優(yōu),還需要選擇正確的IO調(diào)度算法,如果是SSD,選擇NOOP調(diào)度算法,如果是磁盤(pán),那么選擇deadline調(diào)度算法;
2)針對(duì)mysql還顯然可以通過(guò)master-slave來(lái)讀寫(xiě)分離進(jìn)行磁盤(pán)IO優(yōu)化,如果沒(méi)有master-slave架構(gòu),那么可以在前端加上memcache/redis緩存。
3)對(duì)于寫(xiě)很多造成的壓力,則可以適當(dāng)?shù)牟捎肗osql(redis/mongdb/ssdb)等將可以分離出去的盡量分離出去。
4)另外增大內(nèi)存,可以對(duì)更多的磁盤(pán)文件進(jìn)行緩存,也能減輕IO壓力。
5)還有文件系統(tǒng)的掛載選項(xiàng) noatime, nodiratime也能減輕IO壓力,另外選擇正確的文件系統(tǒng)也能提高磁盤(pán)的tps. mysql數(shù)據(jù)庫(kù)推薦使用XFS文件系統(tǒng)。
注:上面顯示的磁盤(pán)IO的數(shù)據(jù),很多都是0,這是因?yàn)閿?shù)據(jù)來(lái)自于虛擬機(jī)中的Linux系統(tǒng)。
總結(jié)
以上是生活随笔為你收集整理的linux mysql io压力大_MySQL性能调优(四) Linux 磁盘IO的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 温州宏丰是做什么的
- 下一篇: mysql 遍历二叉树_【自考】数据结构