Test and solution for high memory usage and CPU usage of MySQL

Source: Internet
Author: User
Tags mysql host

I think many of my friends will find that their mysql occupies a large amount of internal and cpu resources. To solve this problem, most of us will directly reduce the number of mysql accesses and connections and directly generate static or cache files, below I have more ways to solve mysql's high CPU and memory usage.

In order to install the mysql environment for testing, after installation, it is found that MySQL occupies a large amount of memory after startup, up to more than 8 hundred megabytes. I searched for it online and got some tips from my. ini. I have no more details .. I have to open my. ini line by line. Although the English language is a little poor, I still can see how many M ^-^

The changes are as follows:
Innodb_buffer_pool_size = 576 M-> 256 M InnoDB Engine Buffer occupies a large proportion, the first is to use it to open the knife
Query_cache_size = 100 M-> 16 M query Cache
Tmp_table_size = 102 M-> 64 M temporary table size
Key_buffer_size = 256 m-> 32 M

After the mysql service is restarted, the virtual memory is reduced to less than 200.
In addition, mysql installation directory has a few files: my-huge.ini, my-large.ini, my-medium.ini... these are based on the memory size for the recommended configuration, when the novice in the setting can also refer to it.
2 GB memory MYSQL database server my. ini optimization (my. ini)
2 GB memory, for low-site, high-quality settings, test features:
Table_cache = 1024 the larger the physical memory is, the larger the setting will be. The default value is 2402. The optimal value is adjusted to 512-1024.
Innodb_additional_mem_pool_size = 8 M; default value: 2 M
Innodb_flush_log_at_trx_commit = 0 wait until innodb_log_buffer_size is full and then store it in a unified manner. The default value is 1.
Innodb_log_buffer_size = 4 M; default value: 1 M
Innodb_thread_concurrency = 8 The number of CPUs on your server is set to 8 by default.
Key_buffer_size = 256 M. The default value is 218 to 128.
Tmp_table_size = 64 M. The default value is 16 M and the value is adjusted to 64-256.
Read_buffer_size = 4 M. The default value is 64 K.
Read_rnd_buffer_size = 16 M. The default value is 256 K.
Sort_buffer_size = 32 M. The default value is 256 K.
Max_connections = 1024 the default value is 1210.

Test 1:
Table_cache = 512 or 1024
Innodb_additional_mem_pool_size = 2 M
Innodb_flush_log_at_trx_commit = 0
Innodb_log_buffer_size = 1 M
Innodb_thread_concurrency = 8 The number of CPUs on your server is set to 8 by default.
Key_buffer_size = 128 M
Tmp_table_size = 128 M
Read_buffer_size = 64 K or 128 K
Read_rnd_buffer_size = 256 K
Sort_buffer_size = 512 K
Max_connections = 1024

Test 2:
Table_cache = 512 or 1024
Innodb_additional_mem_pool_size = 8 M
Innodb_flush_log_at_trx_commit = 0
Innodb_log_buffer_size = 4 M
Innodb_thread_concurrency = 8
Key_buffer_size = 128 M
Tmp_table_size = 128 M
Read_buffer_size = 4 M
Read_rnd_buffer_size = 16 M
Sort_buffer_size = 32 M
Max_connections = 1024

General:
Table_cache = 512
Innodb_additional_mem_pool_size = 8 M
Innodb_flush_log_at_trx_commit = 0
Innodb_log_buffer_size = 4 M
Innodb_thread_concurrency = 8
Key_buffer_size = 128 M
Tmp_table_size = 128 M
Read_buffer_size = 4 M
Read_rnd_buffer_size = 16 M
Sort_buffer_size = 32 M
Max_connections = 1024

Tested. No special circumstances, it is best to use the default.
2 GB memory, which is suitable for multiple sites and compression-type settings. The best solution is:
Table_cache = 1024 the larger the physical memory is, the larger the setting will be. The default value is 2402. The optimal value is adjusted to 512-1024.
Innodb_additional_mem_pool_size = 4 M; default value: 2 M
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; default value: 1 M
Innodb_thread_concurrency = 8 if you have a few CPUs on your server, set it to a few. We recommend that you use the default value of 8
Key_buffer_size = 256 M. The default value is 218 to 128.
Tmp_table_size = 64 M. The default value is 16 M and the value is adjusted to 64-256.
Read_buffer_size = 4 M. The default value is 64 K.
Read_rnd_buffer_size = 16 M. The default value is 256 K.
Sort_buffer_size = 32 M. The default value is 256 K.
Max_connections = 1024 the default value is 1210.
Thread_cache_size = 120 The default value is 60.
Query_cache_size = 64 M

Ten parameters for optimizing mysql database performance
(1), max_connections:
Number of customers allowed simultaneously. Increase the number of file descriptors required by mysqld. This number should be added. Otherwise, you will often see the too connector connections error. The default value is 100. I will change it to 1024.
(2) record_buffer:
Each thread that performs an ordered scan allocates a buffer of this size to each table it scans. If you perform many sequential scans, you may want to increase the value. The default value is 131072 (128 k). I changed it to 16773120 (16 m)
(3), key_buffer_size:
The index block is buffered and shared by all threads. Key_buffer_size is the buffer size used for index blocks. You can increase the size of indexes that can be better processed (for all reads and multi-Rewrite) so that you can afford that much. If you make it too large, the system will begin to change pages and it will really slow down. The default value is 8388600 (8 m). My mysql host has 2 GB of memory, so I changed it to 402649088 (400 mb ).
4) back_log:
The number of connections that mysql can have. When the main mysql thread receives a lot of connection requests in a very short period of time, this works, and then the main thread takes some time (although very short) to check the connection and start a new thread.
The back_log value indicates how many requests can be stored in the stack within a short time before mysql temporarily stops answering new requests. Only if you want to have many connections in a short period of time, you need to increase it. In other words, this value is the size of the listener queue for the incoming TCP/IP connection. Your operating system has its own limit on the queue size. Trying to set back_log to be higher than your operating system limit will be invalid.
When you observe the process list of your host and find a large number of 264084 | unauthenticated user | xxx. xxx. xxx. xxx | null | connect | null | login | when a null process is to be connected, increase the value of back_log. The default value is 50. I will change it to 500.
(5) interactive_timeout:
The number of seconds that the server waits for action on an interactive connection before closing it. An interactive customer is defined as a customer who uses the client_interactive option for mysql_real_connect. The default value is 28800. I will change it to 7200.
(6) sort_buffer:
Each thread that needs to be sorted allocates a buffer of this size. Add this value to accelerate the order by or group by operation. The default value is 2097144 (2 m). I changed it to 16777208 (16 m ).
(7), table_cache:
Number of tables opened for all threads. Increase this value to increase the number of file descriptors required by mysqld. Mysql requires two file descriptors for each unique opened table. The default value is 64. I changed it to 512.
(8), thread_cache_size:
The number of threads that can be reused. If yes, the new thread is obtained from the cache. If there is space when the connection is disconnected, the customer's thread is placed in the cache. If there are many new threads, this variable value can be used to improve performance. By comparing variables in connections and threads_created states, you can see the role of this variable. I set it to 80.
(9) mysql search function
Mysql is used for search. It is case-insensitive and can be searched in Chinese.
You only need to specify -- default-character-set = gb2312 when starting mysqld
(10), wait_timeout:
The number of seconds that the server waits for action on a connection before closing it. The default value is 28800. I will change it to 7200.

Note: you can modify the parameters by modifying the/etc/my. cnf file and restarting mysql. This is a relatively cautious job. The above results are just some of my views. You can further modify it based on the hardware situation of your host (especially the memory size.

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.