This article mainly explains the problem of MySQL optimization in the CentOS system, and describes some basics of MySQL optimization. MySQL optimization is divided into two parts. Optimization of server physical hardware and MyS 
This article mainly explains the problem of MySQL optimization in the CentOS system, and describes some basics of MySQL optimization. MySQL optimization is divided into two parts. Optimization of server physical hardware and MyS
 
 
 
 
 
This article mainly explains the problem of MySQL optimization in the CentOS system, and describes some basics of MySQL optimization. MySQL optimization is divided into two parts. Physical server hardware optimization and MySQL optimization.
 
The CentOS system is indeed very useful, but we still need to make correct settings and Optimization in many places. 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, we will analyze in detail the MySQL optimization of the CentOS system,
 
 
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.
 
(1) Impact of server hardware on MySQL Performance
 
A) disk tracing capability (disk I/O). Taking the current high-speed SCSI hard disk (7200 RPM) as an example, this hard disk is theoretically found 7200 times 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 in the CentOS system, 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;
Abandon traditional hard disks and use faster flash storage devices. After Discuz! The company's technical engineering tests show that the use of flash storage devices is about 6-10 times faster than traditional hard disks.
 
B) For MySQL applications, we recommend that you use a multi-channel symmetric CPU in the S. M. P. architecture. For example, you can use two Intel Xeon GHz CPUs.
 
C) 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.
 
(2) 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.
 
Since the optimization settings of the my. cnf file are closely related to the server hardware configuration, we specify a hypothetical server hardware environment:
CPU: 2 Intel Xeon 2.4 GHz
Memory: 4 GB DDR
Hard Disk: SCSI 73 GB
 
Next, we will describe the optimized my. cnf based on the above hardware configuration: # vi/etc/my. cnf
 
Only the content in the [mysqld] section in the my. cnf file is listed below. The content in other sections has little impact on MySQL running performance, so ignore it.
[Mysqld]
Port = 3306
Serverid = 1
Socket =/tmp/mysql. sock
Skip-locking
 
Avoid external locks of MySQL and reduce the chance of errors to enhance stability.
Skip-name-resolve
 
Prohibit MySQL from performing DNS resolution on external connections. 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
 
Specify the number of possible MySQL connections. When the MySQL main thread receives many connection requests within a short period of time, this parameter takes effect. The main thread takes a short time to check the connection and start a new thread. 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 CentOS 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 CentOS 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 size of the buffer 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 to query sorting. Note: The allocated memory corresponding to 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 exclusive to each connection!
Join_buffer_size = 8 M
 
The buffer size that can be used by the Joint query operation. The same as 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 in CentOS. You can run the following command on the MySQL console:
> Show variables like '% query_cache % ';
> Show status like 'qcache % ';
If the Qcache_lowmem_prunes value is very large, it indicates that there is often insufficient buffer;
If the Qcache_hits value is very large, it indicates that the query buffer is used very frequently. If the value is small, it will affect the efficiency, you can consider not to use the Query Buffer; Qcache_free_blocks, if the value is very large, it indicates that there are many fragments in the buffer.
Tmp_table_size = 256 M
Max_connections = 768
 
Specifies the maximum number of connection processes allowed by MySQL. If the Too connector Connections error message appears frequently 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
 
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 = 8skip-networking.
 
Enabling this option can completely disable the MySQL TCP/IP connection mode. If the CentOS WEB Server accesses the MySQL database server remotely, do not enable this option! Otherwise, the connection will fail!
 
In the above section, we have made a simple description of my. cnf. MySQL optimization is a task that requires long-term observation, long-term experience, and long-term testing. If you are interested, you can view documents and materials and perform experiments to gain more experience in practical applications.
 
After all the optimization operations are completed, you need to restart the MySQL service of the CentOS system. In this way, after logging on, we will find great changes.
 
 
,