MySQL optimized instance Wikipedia, a free encyclopedia
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.
(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! You can consider the following solutions to solve this constraint:
Use a raid-0 + 1 disk array. Do not try raid-5. The efficiency of MySQL on a raid-5 disk array will not be as fast as you expected; discard the traditional hard disk, use a faster flash storage device. 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 MySQL external locks, reduce the chance of errors, and 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 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 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 = 4M
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 = 8M
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 = 64M table_cache = 512 thread_cache_size = 64 query_cache_size = 64M
Specify the size of the MySQL Query Buffer. 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 the buffer is insufficient frequently;
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 = 256M max_connections = 768
Specifies 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_errors = 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 4x2 = 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!