⌘BABAKOV.NET Let's code!
100% [light] [ru]

Configuring and Optimizing a MySQL Server

I'll say right away that this article is not mine, but I really liked it, and to avoid having to search for it later, I decided to post it here. Fortunately, the author does not prohibit this on his website. Thank you very much to him for that. Here is the original: http://blog.peter.am/index.php/2010/11/30/setup-optimzations-mysql

In this article, we will describe various MySQL settings, primarily those that affect performance. For convenience, all variables are divided into sections (basic settings, restrictions, thread settings, query caching, timeouts, buffers, InnoDB). First, let's clarify the names of some variables that changed in MySQL 4, and which are still found in the network, both old and new names, causing questions.

In version 4, some variables got the _size suffix. This applies to the thread_cache_size variable and variables in the Buffers section. The read_buffer_size variable was called record_buffer until version 4. Also, the skip_external_locking variable in the Basic Settings section was called skip_locking until version 4.

Variables are divided into two main categories: variables with values and flag variables. Variables with values are written to the configuration file in the form variable = value, while flag variables are simply specified. Also, you may have noticed that in some variable names, a hyphen is used, while in others, an underscore is used. Variables with a hyphen are startup options for the server and cannot be changed while the server is running (using SET); variables with an underscore are options for server operation and can be changed on the fly. If we are talking about a 'variable state' or it is recommended to monitor the value of the variable, the name of which is written in the form Variable_Name, you should perform the query SHOW STATUS LIKE 'Variable_Name' to get the value of this variable, or look at the Status tab in phpMyAdmin, where additional comments will be provided for the value of this variable.
Now let's move on to describing variables and their possible values.

Basic Settings

low-priority-updates - This option reduces the priority of INSERT/UPDATE operations compared to SELECT. Relevant if it's more important to read data quickly than to write it quickly.
skip-external-locking - The option is enabled by default, starting from version 4. It tells the MySQL server not to use external locks when working with the database. External locks are necessary in situations where multiple servers work with the same files, i.e., have the same datadir, which is not used in practice.
skip-name-resolve - Do not resolve domain names for IP addresses of connecting clients. In this case, user resolutions need to be set up not on hosts, but on IP addresses (except for localhost). If you connect to the server only from a local machine, it's not particularly important. For external connections, it will speed up connection establishment.
skip-networking - Do not use the network, i.e., do not process TCP/IP connections at all. Communication with the server will occur exclusively through a socket. Recommended if you don't have software that uses only TCP/IP to connect to the server.

Restrictions

bind-address - The interface that the server will listen on. For security reasons, it's recommended to set this to 127.0.0.1 if you don't use external connections to the server.
max_allowed_packet - The maximum size of data that can be sent in one query. Increase it if you encounter the 'Packet too large' error.
max_connections - The maximum number of parallel connections to the server. Increase it if you encounter the 'Too many connections' error.
max_join_size - Prevents SELECT operations that would analyze more than the specified number of rows or more than the specified number of disk searches. Used to protect against malicious queries that try to read millions of rows. The default value is over 4 billion, so you'll probably want to significantly reduce it.
max_sort_length - Specifies how many bytes from the beginning of BLOB or TEXT fields to use when sorting. The default value is 1024. If you're concerned about poorly designed tables or queries, you should reduce it.

Thread Settings

thread_cache_size - Specifies the number of cached threads. After processing a query, the server will not terminate the thread, but place it in the cache if the number of threads in the cache is less than the specified value. The default value is 0, increase it to 8 or 16. If you observe an increase in the value of the variable status Threads_Created, you should increase thread_cache_size even more.
thread_concurrency - Relevant only for Solaris/SunOS, contrary to what is written in the network. 'Suggests' to the system how many threads to start simultaneously, executing the thr_setconcurrency function call. The recommended value is double or triple the number of processor cores.

Query Caching

query_cache_limit - The maximum size of a cached query.
query_cache_min_res_unit - The minimum size of a block stored in the cache.
query_cache_size - The cache size. 0 disables the use of the cache. To choose an optimal value, you need to monitor the variable status Qcache_lowmem_prunes and make it increase slightly. Also, keep in mind that an excessively large cache will create unnecessary load.
query_cache_type - (OFF, DEMAND, ON). OFF disables caching, DEMAND - caching will be performed only if the SQL_CACHE directive is present in the query, ON enables caching.
query_cache_wlock_invalidate - Determines whether data will be taken from the cache if the table to which it relates is locked for reading.

The query cache can be thought of as a hash array, where the keys are queries, and the values are the results of the queries. In addition to the results, MySQL stores in the cache a list of tables, the selection from which is cached. If any of the tables, the selection from which is cached, undergo changes, MySQL will remove the selection from the cache. Also, MySQL does not cache queries whose results can change.

When starting MySQL, it allocates a block of memory of size query_cache_size. When executing a query, as soon as the first rows of the result are received, the server begins caching them: it allocates a block in the cache of size query_cache_min_res_unit, writes the result of the selection into it. If the entire selection does not fit into the block, the server allocates the next block and so on. At the moment of starting to write, MySQL does not know the size of the resulting selection, so if the written block size of the selection is greater than query_cache_limit, the writing is stopped and the occupied space is released, therefore, if you know in advance that the result of the selection will be large, it is worth executing it with the SQL_NO_CACHE directive.

Timeouts

interactive_timeout - The time in seconds during which the server will wait for activity from an interactive connection (using the CLIENT_INTERACTIVE flag) before closing it.
log_slow_queries - Tells the server to log slow queries (queries that take longer than long_query_time). The value is the full name of the file (for example, /var/log/slow_queries).
long_query_time - If a query takes longer than the specified time (in seconds), it will be considered slow.
net_read_timeout - The time in seconds during which the server will wait for data to be received before the connection is interrupted. If the server does not serve clients with very slow or unstable channels, 15 seconds will be enough here.
net_write_timeout - The time in seconds during which the server will wait for data to be received before the connection is interrupted. If the server does not serve clients with very slow or unstable channels, 15 seconds will be enough here.
wait_timeout - The time in seconds during which the server will wait for activity from the connection before interrupting it. In general, 30 seconds will be enough.

Buffers

All buffers have a common feature - if, due to the setting of a large buffer size, data will go to the swap file, then the buffer will do more harm than good. Therefore, always orient yourself on the available amount of physical RAM.
key_buffer_size - The size of the buffer allocated for indexes and available to all threads. A very important setting that affects performance. The default value is 8 MB, it's definitely worth increasing. Recommended 15-30% of the total RAM, but there's no sense in setting it higher than the total size of all .MYI files. Monitor the variables status Key_reads and Key_read_requests, the ratio of Key_reads/Key_read_requests should be as small as possible (< 0.01). If this ratio is large, you should increase the buffer size.
max_heap_table_size - The maximum allowed size of a table stored in memory (of type MEMORY). The default value is 16 MB, if you don't use MEMORY tables, set this value to tmp_table_size.
myisam_sort_buffer_size - The size of the buffer allocated by MyISAM for sorting indexes during REPAIR TABLE or creating indexes during CREATE INDEX, ALTER TABLE. The default value is 8 MB, it's worth increasing up to 30-40% of RAM. The gain in performance will be proportional to the execution of the above-mentioned queries.
net_buffer_length - The amount of memory allocated for the connection buffer and the result buffer for each thread. The connection buffer will be of the specified size, and the result buffer will be of the same size, i.e., for each thread, a double size net_buffer_length will be allocated. The specified value is the initial value, and the buffers will be increased as needed up to max_allowed_packet. The default value is 16 KB. In case of limited memory or using only small queries, the value can be reduced. In case of constant use of large queries and sufficient memory, the value should be increased to the expected average query size.
read_buffer_size - Each thread, when sequentially scanning a table, allocates the specified amount of memory for each table. As shown by tests, this value should not be increased too much. The default value is 128 KB, try increasing it to 256 KB, then to 512 KB, and monitor the speed of execution of queries of the type SELECT COUNT(*) FROM table WHERE expr LIKE 'a%'; on large tables.
read_rnd_buffer_size - Relevant for queries with 'ORDER BY', i.e., for queries whose result must be sorted and which access a table with indexes. The default value is 256 KB, increase it to 1 MB or more if possible. Keep in mind that the specified memory amount is also allocated for each thread.
sort_buffer_size - Each thread, performing sorting operations (ORDER BY) or grouping operations (GROUP BY), allocates a buffer of the specified size. The default value is 2 MB, if you use the above types of queries and if possible, increase the value. A large value of the variable status Sort_merge_passes indicates the need to increase sort_buffer_size. Also, check the speed of execution of queries of the type SELECT * FROM table ORDER BY name DESC on large tables, increasing the buffer may only slow down work (in some tests, this is the case).
table_cache (table_open_cache from version 5.1.3) - The number of cached open tables for all threads. Opening a table file can be a resource-intensive operation, so it's better to keep open tables in the cache. You should consider that each entry in this cache uses a system descriptor, so you may need to increase the limit on the number of descriptors (ulimit). The default value is 64, it's better to increase it to the total number of tables if their number is within acceptable limits. The variable status Opened_tables allows you to track the number of tables opened in bypass of the cache, it's desirable that its value is as low as possible.
tmp_table_size - The maximum size of memory allocated for temporary tables created by MySQL for its internal needs. This value is also limited by the variable max_heap_table_size, so the final value will be the minimum of max_heap_table_size and tmp_table_size, and the rest of the temporary tables will be created on disk. The default value depends on the system, try setting it to 32 MB and monitor the variable status Created_tmp_disk_tables, its value should be as low as possible.

Values in the configuration file are specified in bytes, so kilobytes and megabytes need to be translated into bytes.

InnoDB

innodb_additional_mem_pool_size - The size of memory allocated by InnoDB for storing various internal structures. If InnoDB will not have enough memory for this, it will request memory from the OS and write a warning to the MySQL error log.
innodb_buffer_pool_size - The size of memory allocated by InnoDB for storing and indexes and data. The value - the more, the better. You can increase it up to the total size of all InnoDB tables or up to 80% of RAM, depending on which is less.
innodb_flush_log_at_trx_commit - Has three allowed values: 0, 1, 2. At a value of 0, the log is flushed to disk once a second, regardless of transactions. At a value of 1, the log is flushed to disk with each transaction. At a value of 2, the log is written with each transaction, but never flushed to disk, leaving it to the OS. By default, 1 is used, which is the most reliable setting, but not the fastest. In general, you can safely use 2, data can only be lost in case of OS crash and only for a few seconds (depending on OS settings). 0 - the fastest mode, but data can be lost both in case of OS crash and in case of MySQL server crash (in fact, data can only be lost for 1-2 seconds).
innodb_log_buffer_size - The size of the log buffer. The default value is 1 MB, increase it if you know that there will be a large number of InnoDB transactions or if the value of the variable status Innodb_log_waits is growing. You will hardly need to increase it above 8 MB.
innodb_log_file_size - The maximum size of one log file. When this size is reached, InnoDB will create a new file. The default value is 5 MB, increasing the size will improve performance, but increase the time to recover data. Set this value in the range of 32 MB - 512 MB depending on the server size (subjectively evaluating it).

Also, for monitoring the server's work, it's convenient to use phpMyAdmin, the Status and Variables tabs are of interest. Additionally, phpMyAdmin provides recommendations for tuning specific variables depending on server parameters.

When preparing the article, in addition to the official documentation and my own head, the following materials were used:
http://blogfreebsd.com/freebsd/optimizaciya-mysql.html
http://habrahabr.ru/blogs/mysql/41166/
http://www.mysqlperformanceblog.com/
http://mysyslog.ru/posts/223

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.