您的位置:首页 > 运维架构 > Linux

MySQL 优化之 Linux系统层面调优

2015-10-16 17:51 671 查看
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 半连接 [b]tcp_max_syn_backlog 数量[/b]:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: