MySQL 優化之 Linux系統層面調優


MySQL 一般運行於Linux系統中。對於MySQL的調優一般分為Linux操作系統層面的調優和MySQL層面的調優(當然還有架構層面、業務層面、應用程序層面的調優)。操作系統主要是管理和分配硬件資源,所以其實系統層面的調優包括了硬件的調優,也就是調整硬件參數。Linux系統層面的調優一般分為 CPU的調優、內存的調優、磁盤的調優、網絡的調優、Linux后台service調優等等。

1. CPU 調優

1.1 CPU 的節能模式

在server環境的CPU一定要關閉節能模式,節能模式不適應於服務器環境。因為他會自動給CPU降頻進入休眠模式!一般筆記本電腦,手機為了續航時間,才需要。關閉CPU的節能模式有兩種方法:

1)在BIOS中進行設置,徹底關閉;

2)關閉Linux中的服務 cpuspeed 和 irqbalance;

[root@localhost ~]# chkconfig --level 35 cpuspeed off
[root@localhost ~]# chkconfig | grep cpuspeed
cpuspeed        0:off   1:on    2:off   3:off   4:off   5:off   6:off
[root@localhost ~]# chkconfig --level 35 irqbalance off
[root@localhost ~]# chkconfig | grep irqbalance
irqbalance      0:off   1:off   2:off   3:off   4:off   5:off   6:off

cpuspeed 就是負責CPU節能的后台服務;而irqbalance在cpuspeed將某個或某幾個CPU調節進入休眠模式時,它負責將中斷發送到沒有休眠的CPU。關閉irqbalance會將所有中斷均衡的發送到所有cpu.

1.2 關閉CPU的numa

numa的會導致mysqld產生swap,嚴重影響性能。因為numa架構的CPU和內存是bind的,如果CPU自己node中的內存不夠,就會導致swap的產生,即使此時其它node中有大量的空閑內存,它也不會去使用。這就是numa的一個缺陷。有多種方法關閉CPU的numa:

1)在BISO中進行配置;

2)numactl --interleave=all

[root@localhost ~]# numactl --interleave=all

interleave=all 其實是將NUMA架構的各個node中的內存,又重新虛擬成了一個共享的內存來進行分配,但是和SMP不同的是,因為每兩個node之間有 inter-connect ,所以又避免了SMP架構總線爭用的缺陷。

查看CPU是否被休眠導致降頻:

[root@localhost ~]# cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           E5405  @ 2.00GHz
stepping        : 10
cpu MHz         : 1995.288

 看上面的 cpu MHz : 1995.288 和 實際是否一致。

具體參見:http://www.cnblogs.com/digdeep/p/4847484.html

2. 內存的調優

內存主要是要防止發生 swap。因為發生swap的話,從內存訪問直接下降為硬盤訪問,隨機訪問的速度下降10的6次方倍,也就是10萬倍。順序訪問下降10倍左右。

2.1 防止發生swap:

1)關閉CPU的numa,防止numa導致的swap;

2)設置 vm.swappiness=1; 在 /etc/sysctl.conf 中添加:vm.swappiness=1,然后 sysctl -p; 也可以 sysctl vm.swappiness=1臨時修改,然后sysctl -p

    注意:在RHEL/CentOS 6.4及更新的內核中 vm.swappiness = 0 的默認行為被修改了,如果繼續設置vm.swappiness = 0,

            有可能導致系統內存溢出,從而導致MySQL被意外kill掉。所以這里我們設置為 1 而不是傳統的 0.

3)設置 /proc/$(pidof -s mysqld)/oom_adj為較小的值(-15,-16或者-17)來盡量避免MySQL由於內存不足而被關閉

[root@localhost ~]# echo -17 > /proc/$(pidof mysqld)/oom_adj
[root@localhost ~]# cat /proc/$(pidof mysqld)/oom_adj
-17

這個oom_adj中的變量的范圍為15到-16之間。越大越容易在內存不足時被kill。-17 則表示該進程不會被kill掉,當內存不足時,會kill其它進程。

4)使用 hugepage 可以避免swap out; 但是 huagepage也是有代價的(導致page爭用加劇)

2.2 在BIOS 設置內存為最大性能模式;

2.3 調節 disk cache 刷新到磁盤的行為

因為Linux默認會大量的進行文件cache,也就是將大量內存用於disk cache。這樣的話,會影響mysql使用內存。所以我們可以調節disk cache在臟塊達到多大的百分比時,進行刷新。vm.dirty_background_ratio=10; 默認值為10,表示disk cache中的臟頁數量達到10%時,pdflush內核線程會被調用,異步刷新disk cache; vm.dirty_ratio=20; 表示disk cache中的臟頁數量達到20%時,會進行同步的disk cache刷新,從而阻塞系統中應用進程的IO操作!我們可以調低vm.dirty_background_ratio來降低disk cache對mysql使用內存的影響,但是可能會增加磁盤IO,因為文件cache減少了,增加其他進程的page fault;(vm.dirty_background_ratio / vm.dity_ratio 帶有backround表示異步刷新,沒有帶的是同步刷新。)

具體參見:http://www.cnblogs.com/digdeep/p/4850460.html

3. 磁盤IO的調優

磁盤IO的調優涉及到文件系統的調優和磁盤的調優。

3.1 文件系統的調優

1)文件系統的選擇:在rhel6.4之前ext4性能比xfs好,因為xfs有lock爭用的bug。但是6.4開始,xfs的bug被fix了。測試表明xfs性能比ext4好。

2)文件掛載選項:文件掛載時啟用noatime,nodiratime,可以在 /etc/fstab 中進行修改。man mount 手冊中有相關選項的說明。

4)文件掛載選項:barrier/nobarrier(如果有電池保護,可以啟用nobarrier選項來提供性能)

     If your disks are battery-backed in one way or another,  disabling barriers may safely improve performance. The mount options

    "barrier" and "nobarrier" can also be used to enable or disable barriers, for consistency with other ext4 mount options.

5)文件掛載選項:data={journal|ordered|writeback},如果有電池保護,啟用 data=writeback可能會提升性能。

     journal:All data is committed into the journal prior to being written into the main filesystem.
     ordered:This  is  the default mode.  All data is forced directly out to the main file system prior to its meta-data being committed

                   to the journal.
   writeback: Data ordering is not preserved - data may be written into the main filesystem after its metadata has been committed to

                   the journal. This is rumoured to be the highest-throughput option. It guarantees internal filesystem integrity, however

                   it can allow old data to appear in files after a crash and journal recovery.

6)XFS掛載選項:inode64,如果采用的是XFS文件系統,並且一個分區容量大於1T時,需要采用inode64選項掛載,不然可能會錯誤的報"disk full"

    參見:http://imysql.cn/2013/02/21/using-xfs-with-large-partition-for-backup.html

具體參見:http://www.cnblogs.com/digdeep/p/4857987.html

3.2 磁盤的調優

1)IO調度算法:mysql服務器一定不要使用默認的CFQ調度算法如果是SSD,那么應該使用NOOP調度算法,如果是磁盤,就應該使用Deadline調度算

修改方法:

[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

這是臨時修改,重啟失效。永久修改,需要修改文件 /boot/grub/menu.lst 中的elevator=deadline 或者noop:

# vi /boot/grub/menu.lst
kernel /boot/vmlinuz-2.6.18-8.el5 ro root=LABEL=/ elevator=deadline rhgb quiet

2)加大內存,可以使mysql緩存更大的內容,減少IO操作。

3)磁盤RAID10 或者 換SSD;

4)適當的采用Nosql(redis/mongdb/ssdb)等減輕mysql的負擔;也可以架構master-slave減輕master的IO壓力。

5)使用memcache或者reids在mysql前面加一層緩存,減輕磁盤IO;

6)有陣列卡時,設置陣列寫策略為WB,甚至FORCE WB(若有雙電保護,或對數據安全性要求不是特別高的話)

具體參見:http://www.cnblogs.com/digdeep/p/4863502.html

4. 網絡調優

網絡調優分為硬件層面和TCP/IP軟件層面參數的調優。

4.1 網絡硬件調優:

1)換延遲更小,throught更大的網卡;

2)雙網卡綁定,進行負載均衡和高可用;

4.2 TCP/IP參數調優:

1)socket buffer 參數調節:

1>/proc/sys/net/ipv4/tcp_mem TCP全局緩存,單位為內存頁(4k);

對應的內核參數:net.ipv4.tcp_mem ,可以在 /etc/sysctl.conf 中進行修改;

2>/proc/sys/net/ipv4/tcp_rmem 接收buffer,單位為字節

對應的內核參數:net.ipv4.tcp_rmem, 可以在 /etc/sysctl.conf 中進行修改;

3>/proc/sys/net/ipv4/tcp_wmem 接收buffer,單位為字節

對應的內核參數:net.ipv4.tcp_wmem, 可以在 /etc/sysctl.conf 中進行修改;

4>/proc/sys/net/core/rmem_default 接收buffer默認大小,單位字節

對應內核參數:net.core.rmem_default, 可以在 /etc/sysctl.conf 中進行修改;

5>/proc/sys/net/core/rmem_max 接收buffer最大大小,單位字節

對應內核參數:net.core.rmem_max, 可以在 /etc/sysctl.conf 中進行修改;

6>/proc/sys/net/core/wmem_default 發送buffer默認大小,單位字節

對應內核參數:net.core.rmem_default, 可以在 /etc/sysctl.conf 中進行修改;

7>/proc/sys/net/core/wmem_max 發送buffer最大大小,單位字節

對應內核參數:net.core.rmem_max, 可以在 /etc/sysctl.conf 中進行修改;

2)offload配置

將tso,checksum等功能交給網卡硬件來完成:

ethtool -K eth0 rx on|off

ethtool -K eth0 tx on|off

ethtool -K eth0 tso on|off

3)調大網卡的接收隊列和發送隊列:

1>接收隊列:/proc/sys/net/core/netdev_max_backlog 對應內核參數:net.core.netdev_max_backlog

2>發送隊列:

查看大小:ifconfig eth0 | grep txqueue

修改大小:ifconfig eth0 txqueuelen 20000

4)調大 SYN 半連接 tcp_max_syn_backlog 數量

sysctl -w net.ipv4.tcp_max_syn_backlog=4096

也可以在/etc/sysctl.conf文件中配置。

5)net.core.somaxconn

該參數為完成3次握手,已經建立了連接,等待被accept然后進行處理的數量。默認為128,我們可以調整到 65535,甚至更大。也就是尅有容納更多的等待處理的連接。

MTU 大小 調優

如果TCP連接的兩端的網卡和網絡接口層都支持大的 MTU,那么我們就可以配置網絡,使用更大的mtu大小,也不會導致被 切割重新組裝發送。

配置命令:ifconfig eth0 mtu 9000 up

6)TCP連接的 CLOSE_WAIT 和 TIME_WAIT

如果TCP連接的 CLOSE_WAIT 和 TIME_WAIT 狀態過多時,分別需要調優TCP的keepalive相關的參數和TCP的回收相關的參數。

TCP/IP的調優極其復雜,具體參見博文:http://www.cnblogs.com/digdeep/p/4869010.html

5. Linux 系統中的各種后台daemon的調優

Linux系統中存在各種各樣的后台daemon,也就是各種service,對於mysql服務器來說很多沒有必要的service就可以通通關閉掉。

mysql的最小化的后台服務,可以只有:crond,sshd,rsyslog,network,sysstat

當然如果有需要可以在增加其他服務。

使用 chkconfig --level 35 servicename off; 可以進行關閉。35表示在runlevel的level =3 和 level =5級別進行關閉。

deamon的調優可以參考:http://wubx.net/category/optimize/

6. ulimit 資源限制的調優

ulimit provides control over the resources available to the shell and to processes started by it, on systems that allow such control.

The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as a ceiling for the soft limit.

ulimit命令可以控制 shell 可以獲得的資源的限制,同時也控制在 shell 中啟動的進程可以獲得的資源的限制。分為 soft limit 和 hard limit.

ulimit 的手冊:

User limits - limit the use of system-wide resources.

Syntax
      ulimit [-acdfHlmnpsStuv] [limit]

Options

   -S   Change and report the soft limit associated with a resource. 
   -H   Change and report the hard limit associated with a resource. 

   -a   All current limits are reported. 
   -c   The maximum size of core files created. 
   -d   The maximum size of a process's data segment. 
   -f   The maximum size of files created by the shell(default option) 
   -l   The maximum size that can be locked into memory. 
   -m   The maximum resident set size. 
   -n   The maximum number of open file descriptors. 
   -p   The pipe buffer size. 
   -s   The maximum stack size. 
   -t   The maximum amount of cpu time in seconds. 
   -u   The maximum number of processes available to a single user. 
   -v   The maximum amount of virtual memory available to the process. 

ulimit provides control over the resources available to the shell and to processes started by it, on systems that allow such control.

The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as a ceiling for the soft limit.

An unprivileged process may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit. A privileged process can make arbitrary changes to either limit value.

If limit is given, it is the new value of the specified resource. Otherwise, the current value of the soft limit for the specified resource is printed, unless the `-H' option is supplied.

When setting new limits, if neither `-H' nor `-S' is supplied, both the hard and soft limits are set.

Restricting per user processes ( -u) can be useful for limiting the potential effects of a fork bomb.

Values are in 1024-byte increments, except for `-t', which is in seconds, `-p', which is in units of 512-byte blocks, and `-n' and `-u', which are unscaled values.

The return status is zero unless an invalid option is supplied, a non-numeric argument other than unlimited is supplied as a limit, or an error occurs while setting a new limit.

ulimit is a bash built in command.
ulimit

查看目前的所有的限制:

[root@localhost ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7908
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7908
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

一般而言,我們很容易在 open files 上被限制,從而報錯。我們可以臨時修改和永久修改:

ulimit -n 8192

永久修改,需要修改文件 /etc/security/limits.conf, 加上:

* soft nofile 10240
* hard nofile 20480

修改之后,還需要在 vi /etc/pam.d/login 最后加上一行:

session required pam_limits.so

7. 總結:

Linux系統和硬件的調優,除了一些通用的調優之外。其它比如TCP/IP的調優,我們首先是要使用相關的各種命令查清楚瓶頸在哪里,然后才好對症下葯。

 

參考:

http://mp.weixin.qq.com/s?__biz=MjM5NzAzMTY4NQ==&mid=207641943&idx=1&sn=d124286ea8811950be5a872d57a27357

http://wubx.net/category/optimize/

http://imysql.cn/2015/05/24/mysql-optimization-reference-1.shtml

http://imysql.cn/2013/02/21/using-xfs-with-large-partition-for-backup.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM