MySQL Performance Optimization Path-modify the configuration file my. cnf

Source: Internet
Author: User

In the architecture of Apache, PHP, and MySQL, MySQL has the greatest impact on performance and is also a key core component. For Discuz! The same is true for Forum programs. Whether MySQL settings are reasonably optimized directly affects the speed and carrying capacity of the Forum! At the same time, MySQL is also the most difficult part of optimization. It not only needs to understand some MySQL professional knowledge, but also requires a long period of observation statistics and judgment based on experience, and then set reasonable parameters. Next, let's take a look at some of the basics of MySQL optimization. MySQL optimization is divided into two parts: one is the optimization of the physical hardware of the server, and the other is the optimization of MySQL itself (my. cnf.

I. Impact of server hardware on MySQL Performance

① Disk seek capability (disk I/O). Taking the current high-speed SCSI hard disk (7200 RPM) as an example, this hard disk theoretically finds 7200 channels per second, which is determined by the physical characteristics, there is no way to change. MySQL performs a large number of complex query operations every second. You can imagine the disk read/write volume. Therefore, we usually think that disk I/O is one of the biggest factors restricting MySQL performance. For Discuz with an average daily access volume of more than 1 million PVS! Forum, due to disk I/O constraints, MySQL performance will be very low! To solve this problem, consider the following solutions: Use a RAID-0 + 1 disk array. Do not try RAID-5, the efficiency of MySQL on the RAID-5 disk array is not as fast as you expected.

② CPU is recommended for MySQL applications. m. p. multi-channel symmetric CPU in the architecture. For example, two Intel Xeon GHz CPUs can be used. Now I recommend using a 4 U server for dedicated database servers, not just for mysql.

③ For a Database Server using MySQL, we recommend that the Server memory be no less than 2 GB. We recommend that you use more than 4 GB physical memory, however, the memory is negligible for the current server, and the memory of the high-end server exceeds 16 GB at work.

Ii. MySQL's own factors when the above server hardware constraints are solved, let's see how MySQL's own optimization works. The optimization of MySQL is mainly to optimize and adjust the parameters in its configuration file my. cnf. The following describes some parameters that have a great impact on performance. Because my. the optimization settings of the cnf file are closely related to the server hardware configuration. Therefore, we specify a hypothetical server hardware environment: CPU: 2 Intel Xeon 2.4 GHz memory: 4 gb ddr hard drive: SCSI 73 GB (Common 2U servers ).

Next, we will describe the optimized my. cnf based on the above hardware configuration:

[Mysqld]
Port = 3306
Serverid = 1
Socket =/tmp/mysql. sock
Skip-locking
# Avoid external locks of MySQL to reduce the chance of errors and enhance stability.
Skip-name-resolve
# Prohibit MySQL from performing DNS resolution on external connections. Using this option can eliminate the time for MySQL to perform DNS resolution. However, if this option is enabled, IP addresses are required for all remote host connection authorizations. Otherwise, MySQL cannot process connection requests normally!
Back_log = 384
# The value of the back_log parameter indicates how many requests can be stored in the stack within a short time before MySQL temporarily stops responding to a new request. If the system has many connections in a short period of time, you need to increase the value of this parameter, which specifies the size of the listener queue for the incoming TCP/IP connection. Different operating systems have their own limits on the queue size. Trying to set back_log to be higher than your operating system limit will be invalid. The default value is 50. We recommend that you set the value to an integer smaller than 512 in Linux.
Key_buffer_size = 256 M
# Key_buffer_size specifies the buffer size used for the index. Increasing the size can improve the index processing performance. This parameter can be set to 384 M or M for servers with around 4 GB of memory. Note: If this parameter value is set too large, the overall efficiency of the server will be reduced!
Max_allowed_packet = 4 M
Thread_stack = 256 K
Table_cache = 128 K
Sort_buffer_size = 6 M
# The buffer size that can be used for sorting. Note: The allocated memory for this parameter is exclusive to each connection. If there are 100 connections, the total size of the actually allocated sort buffer is 100 × 6 = 600 MB. Therefore, we recommend that you set the size of a server with around 4 GB to 6-8 Mb.
Read_buffer_size = 4 M
# The buffer size that can be used by the read query operation. Like sort_buffer_size, the allocated memory corresponding to this parameter is also exclusive to each connection.
Join_buffer_size = 8 M
# The size of the buffer that can be used by the Joint query operation. Like sort_buffer_size, the allocated memory corresponding to this parameter is exclusive to each connection.
Myisam_sort_buffer_size = 64 M
Table_cache = 512
Thread_cache_size = 64
Query_cache_size = 64 M
# Specify the size of the MySQL Query Buffer. You can observe on the MySQL console that if the Qcache_lowmem_prunes value is very large, it indicates that the buffer is often insufficient. If the Qcache_hits value is very large, it indicates that the query buffer is frequently used, if this value is small, it will affect the efficiency, you can consider not to query the buffer; Qcache_free_blocks, if this value is very large, it indicates that there are many fragments in the buffer.
Tmp_table_size = 256 M
Max_connections = 768
# Specify the maximum number of connection processes allowed by MySQL. If the Too connector Connections error is frequently reported during Forum access, you need to increase the value of this parameter.
Max_connect_errorrs = 10000000
Wait_timeout = 10
# Specify the maximum connection time of a request. For servers with around 4 GB of memory, you can set it to 5-10.
Thread_concurrency = 8
# The value of this parameter is the number of logical CPUs of the server x 2. In this example, the server has two physical CPUs, and each physical CPU supports H.T hyper-threading, therefore, the actual value is 4*2 = 8.
Skip-networking
# Enabling this option can completely disable the MySQL TCP/IP connection mode. If the WEB server accesses the MySQL database server remotely, do not enable this option! Otherwise, the connection will fail!
Table_cache = 1024
# The larger the physical memory is, the larger the setting will be. The default value is 2402, and the optimal value is adjusted.
Innodb_additional_mem_pool_size = 4 M
# The default value is 2 MB.
Innodb_flush_log_at_trx_commit = 1
# If it is set to 0, innodb_log_buffer_size is stored after the queue is full. The default value is 1.
Innodb_log_buffer_size = 2 M
# The default value is 1 MB.
Innodb_thread_concurrency = 8
# Set the number of CPUs on your server to 8 by default.
Key_buffer_size = 256 M
# The default value is 218. The optimal value is 128.
Tmp_table_size = 64 M
# The default value is 16 MB, and the maximum value is adjusted to 64-256.
Read_buffer_size = 4 M
# The default value is 64 KB.
Read_rnd_buffer_size = 16 M
#256 kb by default
Sort_buffer_size = 32 M
#256 kb by default
Thread _ cache_size = 120
# The default value is 60.
Query_cache_size = 32 M
※Note:

In many cases, specific analysis is required.

1. If Key_reads is too large, increase Key_buffer_size in my. cnf to keep Key_reads/Key_read_requests above 1/100. The smaller the value, the better.

2. If Qcache_lowmem_prunes is large, you need to increase the value of Query_cache_size.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.