Linux system high-load MySQL database thoroughly optimized (1)

Source: Internet
Author: User

At the same time, the increase in the number of online traffic is obviously difficult for servers with 1 GB of memory. In severe cases, even the server crashes every day or gets stuck from time to time. This problem has plagued me for more than half a month. MySQL uses a very scalable algorithm, therefore, you can usually run with a small amount of memory or store MySQL more for better performance.

After installing mysql, the preparation file should be in the/usr/local/mysql/share/mysql directory, the preparation file has a few, there are my-huge.cnf my-medium.cnf my-large.cnf my-small.cnf, websites with different traffic and server environments with different configurations must have different preparation files.

Under normal circumstances, the preparation of the my-medium.cnf file can meet most of our needs; general we will copy the configuration file to/etc/my. cnf: you only need to modify this configuration file. You can use mysqladmin variables extended-status-uroot-p to see the current parameters. Three configuration parameters are the most important, that is, key_buffer_size, query_cache_size, and table_cache.

Key_buffer_size only applies to MyISAM tables. key_buffer_size specifies the size of the index buffer, which determines the speed of index processing, especially the speed of index reading. Generally, we set the value to 16 M. In fact, the number of sites that are slightly larger is far from enough. By checking the status values Key_read_requests and Key_reads, we can check whether the key_buffer_size setting is reasonable. The ratio of key_reads/key_read_requests should be as low as possible, at least, and is better. The above STATUS values can be obtained using show status like 'key _ read % ). Or if you have installed phpmyadmin, you can see it through the server running status. I recommend you use phpmyadmin to manage mysql. The following status values are all my instance analysis obtained through phpmyadmin:

This server has been running for 20 days

Key _buffer_size-128 M
Key_read_requests-650759289
Key_read-79112

Ratio close to 1: 8000 healthy

Another method to estimate key_buffer_size

Add up the size of the index space of each table in your website database. Take this server as an example: The index size of several large tables increases by about 125 MB.

MySQL provides a query buffer mechanism starting from 4.0.1. Using the Query Buffer, MySQL stores SELECT statements and query results in the buffer. In the future, the same SELECT statements are case-sensitive.) The results will be read directly from the buffer. According to the MySQL user manual, query buffering can achieve a maximum efficiency of 238%.

You can check whether query_cache_size is set properly by adjusting the following parameters.

Qcache inserts
Qcache hits
Qcache lowmem prunes
Qcache free blocks
Qcache total blocks

The value of Qcache_lowmem_prunes is very large, which indicates that the buffer is insufficient frequently. The value of Qcache_hits is very large, which indicates that the query buffer is frequently used. In this case, the value of Qcache_hits is not large, this indicates that your query repetition rate is very low. In this case, using the Query Buffer will affect the efficiency, so you can consider not to use the query buffer. In addition, adding SQL _NO_CACHE to the SELECT statement explicitly indicates that no Query Buffer is used.

Qcache_free_blocks. If the value is very large, it indicates that there are many fragments in the buffer. query_cache_type specifies whether to use Query Buffer.

My settings:

QUOTE:

Query_cache_size = 32 M
Query_cache_type = 1

Obtain the following status values:

Qcache queries in cache 12737 indicates the number of items currently cached
Qcache inserts 20649006
Qcache hits 79060095 seems that the repeat query rate is quite high
Qcache lowmem prunes 617913 has so many times that the cache is too low
Qcache not cached 189896
Qcache free memory 18573912
Qcache free blocks 5328 seems to be a little big.
Qcache total blocks 30953
If the memory allows 32 MB, you should add some more

Table_cache specifies the table cache size. When MySQL accesses a table, if there is space in the table buffer, the table is opened and put into it, so that the table content can be accessed more quickly. Check the status values Open_tables and Opened_tables of the peak time to determine whether to increase the value of table_cache. If you find that open_tables is equal to table_cache and opened_tables is growing, you need to increase the value of table_cache. You can use show status like 'open % tables 'to obtain the value above ). Note that you cannot blindly set table_cache to a large value. If it is set too high, the file descriptor may be insufficient, resulting in unstable performance or connection failure.

For machines with 1 GB memory, the recommended value is 128-256.

Set by the author

QUOTE:

Table_cache = 256

The following status is displayed:

Opening tables 256
Opened tables 9046

Although open_tables is already equal to table_cache, opened_tables has a very low value after 20 days of running compared to the server running time. Therefore, increasing the value of table_cache should be of little use. If the preceding value appears after six hours, you need to increase the value of table_cache.

If you do not need to record binary logs, you can disable this function. After you disable it, you cannot recover the data before the problem occurs. You need to manually back up the data, the binary log contains all the statements for updating data. It is used to restore the data to the final state when restoring the database. In addition, if you perform Replication, you also need to use binary logs to transmit modifications.

Log_bin specifies the log file. If no file name is provided, MySQL generates its own default file name. MySQL automatically adds a numeric reference after the file name. A new binary file is generated every time the service is started. In addition, you can use log-bin-index to specify the index file, binlog-do-db to specify the database for the record, and binlog-ignore-db to specify a database without record. Note: binlog-do-db and binlog-ignore-db specify only one database at a time and multiple statements are required for multiple databases. In addition, MySQL will change all database names to lowercase letters, and all database names must be in lower case when specifying the database, otherwise it will not work.

To disable this function, you only need to add the # sign before it.

QUOTE:

# Log-bin


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.