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

MySQL 优化之 Linux系统层面调优

2016-06-24 22:51 495 查看
http://www.cnblogs.com/digdeep/p/4885948.html

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是否被休眠导致降频:

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.


查看目前的所有的限制:

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