search
HomeDatabaseMysql TutorialMySQL 优化之 Linux系统层面调优_MySQL

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.bitsCN.com/database/201510/445109.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.bitsCN.com/database/201510/445112.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 中进行修改。

 

具体参见:http://www.bitsCN.com/database/201510/445114.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;

 

具体参见:http://www.bitsCN.com/database/201510/445288.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

 

当然如果有需要可以在增加其他服务。

 

使用 chckconfig --level 35 servicename off; 可以进行关闭。35表示在runlevel的level =3 和 level =5级别进行关闭。

 

6. 总结:

 

Linux系统和硬件的调优,除了一些通用的调优之外。其它比如TCP/IP的调优,我们首先是要使用相关的各种命令查清楚瓶颈在哪里,然后才好对症下药。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),