mysql optimize the configuration and principle introduction

1. MySQL non-cache parameters and modify variables introduced

1.1 back_log modify parameter values: 50 modified from the default is 500. (each connection 256kb, occupancy: 125M)

  = 500 back_log
back_log value indicates how many requests a short time before MySQL momentarily stops answering new requests may be the presence of the stack. That is, if the connection data MySql max_connections reached, the new request will be present in the stack, and waits for a connection to the release of resources, i.e. the number of the stack The back_log, if the connection exceeds the number of waiting The back_log, will not be grant connection resources. Will be reported:. Unauthenticated user | xxx.xxx.xxx.xxx | NULL | Connect | NULL | login | when NULL to be connected process
back_log value can not exceed the size of the TCP / IP connections listen queue. If it is invalid, the size of the command to view the current system of TCP / IP connections listen queue exceeds: cat / proc / sys / net / ipv4 / tcp_max_syn_backlog the current system of 1024. For Linux automatic set integer smaller than 512. Modify the kernel parameter,) http://www.51testing.com/html/64/n-810764.html
view mysql back_log current default value, the command: show variables like 'back_log'; see the current number of
 
1.2 to modify the parameter value wait_timeout by default for 8 hours, 30 minutes to modify

  wait_timeout = 1800 (the unit is wonderful)
my understanding of wait-timeout parameter of this: MySQL client database connection maximum idle time value.
A little more plainly, it is when your MySQL connection idle for more than a certain time will be forced to close. MySQL default wait-timeout value of eight hours, 'wait_timeout' value command to view the results show variables like;.
Setting this value is very meaningful, such as your site has a large number of link requests MySQL (MySQL connections are to each memory resource overhead), because of your program a large number of connection requests idle nothing but shoot , in vain memory resources, or cause MySQL to exceed the maximum number of connections never been able to lead New connection "Too many connections" error. Before setting you can look at your MYSQL status (available show processlist), if MYSQL often found in a large number of Sleep process, you need to modify the wait-timeout value.
wait_timeout: number of seconds to wait for the server to shut down activities before non-interactive connection. When the thread starts, according to the global value wait_timeout interactive_timeout or global session initialization value wait_timeout value, depending on the type of client (connection option CLIENT_INTERACTIVE mysql_real_connect defined by () in).
These two parameters must be used. Otherwise separately provided wait_timeout invalid
 
1.3 max_connections modify parameter values from the default 151, modified to 3000 (750M).

   = 3000 max_connections
max_connections refers to the maximum number of connections MySql if the concurrent connection requests to the server a greater amount, it is recommended to increase this value to increase the number of parallel connections, of course, can be established only when the machine is supported, because, if the number of connections multiple, provides connection between MySql buffer for each connection, it will cost more memory, so the value should be adjusted, not blindly increase the set value. Can lead 'conn%' wildcard view the current state of the number of connections, to decide the size of the value. Maximum number of connections allowed by MySQL server 16384;
Display the current maximum number of connections: Show Variables like 'max_connections';
 
1.4 max_user_connections modify the value by default 0, 800 modified

   = 800 max_user_connections
max_user_connections is the maximum database connections per user, concurrent maximum number of concurrent connections to the MYSQL service for all clients of a particular account. It simply refers to the same account can connect to the maximum number of connections mysql service at the same time. Set to 0 for no limit.
Incidentally, here introduced to Max_used_connections: It refers to the mysql service from start to now, the same time the maximum number of concurrent connections. It does not refer to the current connection, but a comparison value. If in the past a certain moment, MYSQL service at the same time there are over 1000 requests a connection, and then never so much concurrent requests occurs, Max_used_connections = 1000. Please note the difference max_user_connections show variables inside. The default of 0 means infinity.
See max_user_connections value Variables like Show 'max_user_connections';

for 1.5 thread_concurrency modified value from the current default 8, 64 is modified

   thread_concurrency = 64
is correct or not thread_concurrency value, a great impact on the performance of mysql, in a case where a plurality of CPU (or multi-core), the error of the set value thread_concurrency, mysql not cause full advantage of multi CPU (or multi core) appeared a moment, only a cpu (or nuclear) in the case of work.
thread_concurrency should be set to twice the number of CPU core such as a dual-core CPU, that thread_concurrency be 4; two pairs of cores cpu, the value thread_concurrency should 8.
For example: The description above configuration our current system, may be 4 is known CPU, each CPU core 8, according to the calculation rule above, here would be: 4 * 8 * 2 = 64
to check the present thread_concurrency default configuration commands: Show Variables like 'thread_concurrency';
 
1.6 Add skip-name -resolve, is commented by default, this parameter is absent.

  Resolve-name-Skip
Skip-name-Resolve: prohibit external connections to MySQL DNS resolution, use this option eliminates MySQL DNS resolution time. Note, however, if this option is turned on, all remote host connection should be authorized to use the IP address of the way, otherwise the connection requests MySQL can not handle normal!

1.7 skip-networking, the default is commented out. Without this parameter. (The useless)

 skip-networking recommendation is commented out, do not open, open the option to completely shut down the MySQL TCP / IP connections, if the way WEB server is a remote connection to access the MySQL database server, do not turn this option! Otherwise it will not connect properly!

1.8 default-storage-engine (MySQL set the default storage engine)

default-storage-engine = InnoDB (InnoDB setting type, may be provided additionally to MyISAM)
arranged to create a database table and the default storage type, show table status like 'tablename' displays the current state value storage table.
View MySQL which store the state and the default storage status: show engines;
create a table and specify the storage type: CREATE TABLE mytable (id int, title char (20)) ENGINE = INNODB;
modify table storage types: Alter table tableName engine = engineName

Remarks : after setting open to the following:
# Uncomment at the following IF you are a using the Tables InnoDB
innodb_data_home_dir = / var / lib / MySQL
#innodb_data_file_path = ibdata1: 1024M; ibdata2: 10M: AUTOEXTEND (to comment, otherwise it will create a new . the replacement of the original)
innodb_log_group_home_dir = / var / lib / MySQL
# buffer_pool_size by You CAN SET .._ up to 50 - 80%
# of the RAM Memory Usage Setting But Beware of High TOO
innodb_buffer_pool_size = 1000M
= 20M innodb_additional_mem_pool_size
# .._ log_file_size the Set Buffer to 25% of the pool size
innodb_log_file_size = 500M
innodb_log_buffer_size = 20M
innodb_flush_log_at_trx_commit = 0
innodb_lock_wait_timeout = 50

After setting must remember to address the MySQL installation directory (the default installation so we are currently address / var / lib / ib_logfile0 and ib_logfile1) under mysql / deleted. Otherwise, restart the MySQL start to fail.

2. MySQL cache variables introduce and modify

database belonging to the IO-intensive applications, its primary responsibility is to manage and store work data. And we know that a database is read from memory time is microsecond level, and read from a piece of ordinary hard disk IO is in milliseconds, a difference of three orders of magnitude. Therefore, to optimize the database, we first need to optimize the first step is the IO, as far as possible into memory disk IO IO. This paper begins with MySQL database IO-related parameters (cache parameters) can take a look at the angle by which parameters to optimize IO
 
2.1 global cache

It must assign global cache and is always there when you start MySQL. Currently there are: key_buffer_size (default: 402 653 184, that is 384M), innodb_buffer_pool_size (default: 134217728 namely: 128M), innodb_additional_mem_pool_size (default: 8388608 namely: 8M), innodb_log_buffer_size (default: 8388608 namely: 8M), query_cache_size (default value: 33554432 namely: 32M) five. . Total: 560M

values of these variables are available through a command such as: show variables like 'variable names'; view.

  1): key_buffer_size, the system is currently 384M, can be modified to 400M
    the key_buffer_size = 400M
    the key_buffer_size buffer size for the index block, incrementing the index (for all read and multiple write) it could be better addressed on MyISAM ( one type of MySQL table storage, you can view the details of Baidu, etc.) of a table greatest impact performance parameters. If you make it too large, the system will begin to change pages and really slowed down. Strictly speaking, it determines the speed of the database indexing process, particularly in the index reading speed. For memory 4GB server about this parameter can be set to 256M or 384M.

Key_buffer_size know how to set it is reasonable, generally can check the status and value Key_read_requests Key_reads, the proportion key_reads / key_read_requests should be as low as possible, such as 1: 100, 1: 1000, 1: 10000. Which value will be found to give the following command: show status like 'key_read%' ;

such as viewing the current system and key_read_request key_read values:
+ ------------------- + - + -----
| variable_name | Value |
+ ------------------- + ------- +
| Key_read_requests | 28535 |
| Key_reads | 269 |
+ ------- + ------- +
know there 28,535 requests, request 269 is not found in the memory directly from the hard disk the index of

the probability of a cache miss is: 0.94% = 269/28535 * 100% hit probability is generally not under 0.1 is better. It has been far greater than 0.1, not proved effective. If the hit rate is below 0.01, it is recommended appropriate changes key_buffer_size value.
Six of the difference http://dbahacker.com/mysql/innodb-myisam-compare(InnoDB and MyISAM)
http://kb.cnblogs.com/page/99810/ (see Storage Engine description)
MyISAM, InnoDB, MyISAM Merge engine, InnoDB, Memory (heap), Archive

  2): innodb_buffer_pool_size (default 128M)

innodb_buffer_pool_size = 1024M (1G)
   innodb_buffer_pool_size: The main argument for the greatest impact InnoDB table performance. Functions and Key_buffer_size same. InnoDB occupied memory, in addition to the page cache for storing data innodb_buffer_pool_size, the other under normal circumstances, and about 8% of the cost, mainly used in the description of each cache page frames, adaptive hash data structures, etc., if it is not securely closed to start also when restored, it also opened with about 12% of the memory is used for recovery, the two together have almost 21 percent of the cost. Assumptions: innodb_buffer_pool_size 12G, and most of the time it may take up to 14.5G InnoDB memory. If the system is only 16G, and only run MySQL, and MySQL only InnoDB, MySQL open so as 12G, is to maximize the use of memory.

Further different InnoDB and MyISAM storage engine, MyISAM key_buffer_size only the cache index key, but it can innodb_buffer_pool_size cached data blocks and index keys. Appropriate to increase the size of this parameter can effectively reduce the table type InnoDB disk I / O.

When we operate a InnoDB table, the index block any or all of the data returned to the data used in the process, will be born in this memory area.

By - calculate the cache hit rate (Innodb_buffer_pool_read_requests Innodb_buffer_pool_reads) / Innodb_buffer_pool_read_requests * 100 %, and to adjust the size parameters are optimized according innodb_buffer_pool_size hit rate. Values can be obtained using the following command to check: show status like 'Innodb_buffer_pool_read%' ; system such as viewing the current system in
| Innodb_buffer_pool_read_requests | 1,283,826 |
| Innodb_buffer_pool_reads | 519 |
+ ---------------- + --------- + -----------------------
its hit rate = 99.959% (1283826-519) / 1283826 * 100% hit rate as high as possible.

   3): innodb_additional_mem_pool_size (default 8M)

  innodb_additional_mem_pool_size = 20M
     innodb_additional_mem_pool_size set InnoDB storage engine used to store data dictionary information and memory size of some internal data structures, so when we have a lot of database objects in a MySQL Instance when it is needed adjust the size of the parameters to ensure that all data can be stored in memory to improve the efficiency of access.
This parameter is adequate size is relatively easy to know, because when the time is too small, MySQL will record Warning information to the error log database, this time you will know the size of the adjustment of this parameter.
View the current system mysql error logs cat / var / lib / mysql / machine name .error found many waring warning. So much to tune 20M. According to the MySQL manual for 2G memory machine, the recommended value is 20M. 32G memory 100M

   4): innodb_log_buffer_size (default 8M)

innodb_log_buffer_size = 20M
    innodb_log_buffer_size This is the transaction log InnoDB storage engine used by the buffer. Similar Binlog Buffer, InnoDB transaction log at the time of writing, in order to improve performance, but also the first to write information Innofb Log Buffer, when the appropriate conditions are met after innodb_flush_log_trx_commit parameter set (or log buffer is full), will be the log file is written to (or sync to disk) in. You can set the maximum amount of memory that can be used by innodb_log_buffer_size parameters.
   InnoDB buffer size written to the log before the log disk file. To a value of over 1M 8M. Large log buffer allows the transaction is running do not need to log saved to disk but only to the transaction is committed (commit). Thus, if a large transaction, provided a large log buffer can reduce disk I / O. Provided in digital format in the my.cnf.
The default is 8MB, such as frequent-based system may be appropriately increased to 4MB ~ 8MB. Of course, as mentioned above, we described, and this actually further parameter related parameters flush. Generally not recommended to exceed 32MB
Note: innodb_flush_log_trx_commit parameters are critical impact on write performance InnoDB Log, the default value of 1. This parameter can be set to 0, interpreted as follows:
0: a data log buffer in the second frequency will be written to the log file, and the file system will also be synchronized to disk operation, but each commit the transaction will not trigger any log buffer to the log file or file system to refresh refresh disk operation;
1: at the time of each transaction will be submitted in the log buffer data is written to a log file, but also trigger synchronize the file systems to disk;
2: Rights to trigger log buffer to refresh the log file, but does not trigger synchronous disk-to-disk file system. In addition, there will be a second-to-disk file system synchronization.
Actual test found that the value of the data rate inserted great influence, is set to record insertion 10000 2 take 2 seconds, is set to 0 requires only one second, and is set to 1 is required 229 seconds. Therefore, MySQL manual also recommended to insert merge into one transaction, which can significantly increase the speed. According to the MySQL manual, under the premise that there is danger of missing the nearest part of the transaction, it can put the value to 0.

  5): query_cache_size (default 32M)

the query_cache_size = 4OM
     query_cache_size: mainly used to cache in MySQL ResultSet, the result is a set of SQL statements executed, it is only just for the select statement. When we open the Query Cache function, MySQL after the request is received by a select statement, if the statement Query Cache meet the requirements (not allowed to use explicit instructions Query Cache, or have explicitly stated the need to use Query Cache), MySQL will be directly based on predefined hASH algorithm will receive the select statement as a string hash, then look directly into the Query cache is already cached. That is, if already in the cache, the request will select return data directly, thereby eliminating all subsequent steps (such as SQL statement parsing, optimization, and optimization engine requests data from the storage, etc.), greatly improved performance. According to MySQL user manual, query buffer can be up to 238% efficiency.
Of course, Query Cache also has a fatal flaw, that is, when there is any data in a table any change would cause all referencing the cached data in the table select statement in Query Cache failure. So, when our data changes very frequently the case, using the Query Cache might outweigh the benefits
   Query Cache use requires a plurality of fitting parameters, which are the most critical and the query_cache_type query_cache_size, the former provided for the memory size of the cache ResultSet, the latter is provided the use of any Query Cache at the scene. In past experience, if not used to cache data is essentially the same MySQL database, query_cache_size 256MB is generally a more appropriate size. Of course, this can be adjusted by calculating the Query Cache hit ratio (Qcache_hits / (Qcache_hits + Qcache_inserts) * 100)). query_cache_type can be set to 0 (OFF), 1 (ON ) or 2 (DEMOND), respectively, do not use the query cache, in addition to the explicit request does not use all of the select query than Cache (using SQL_NO_CACHE) are used query cache, it requires the use of query cache (using sql_cache) display only. Qcache_lowmem_prunes If the value is very large, it indicates that if the value of the buffer Qcache_hits is often very large, it indicates that the query cache used very frequently, then the need to increase the size of the buffer;.

The hit ratio (Qcache_hits / (Qcache_hits + Qcache_inserts) * 100) ) is adjusted, generally do not recommend too much, 256MB may have almost the same, large-type static configuration data may be appropriate to turn up.

by command: show status like 'Qcache_%' ; see the current system using the Query catch size
| Qcache_hits | 1892463 |
| Qcache_inserts | 35627  
hits 98.17 percent = 1892463 / (1892463 +35627) 100 *

2.2 local cache

in addition to the global buffer, MySql will release the connection buffer for each connection. Connections to the MySQL server thread requires its own buffer. Probably you need to immediately allocate 256K, even if thread is idle, they use the default thread stack, network caching. After the transaction is started, you need to add more space. Running small queries to the specified thread may only increase a small amount of memory consumption, but if you do complex operations such as data table scans, sorts, or need a temporary table, you need to allocate about read_buffer_size, memory space sort_buffer_size, read_rnd_buffer_size, tmp_table_size size but they are only allocated when needed, and after those operations done on the release. Some of them are allocated as separate chunks. tmp_table_size possible up to a maximum memory MySQL will allocate for this operation up. Note that not only one point to be considered here - may assign more than the same type of cache, for example, to handle subqueries. Some special queries memory usage may be greater - bulk_insert_buffer_size need to allocate memory size when inserted if the MyISAM table made in batches; the implementation of ALTER TABLE, need to allocate memory size myisam_sort_buffer_size command OPTIMIZE TABLE, REPAIR TABLE.

   1): read_buffer_size (default: 2097144 i.e. 2M)
as read_buffer_size = 4M
       as read_buffer_size MySql is read into the buffer size. Request table for assigning a sequential scans read into the buffer, MySql allocate some memory buffer for it. read_buffer_size variable controls the size of the buffer. If the request for a sequential scan of the table are very frequent, and often you think scanning too slow, its performance can be improved by increasing the value of the variable, and memory buffer size.

  2): sort_buffer_size (default: 2097144 i.e. 2M)
sort_buffer_size = 4M
    sort_buffer_size MySql perform the sort buffer size is used. If you want to increase ORDER BY speed, first see if you can get MySQL to use indexes rather than an extra sorting phase. If not, try increasing the size of the variable sort_buffer_size

   3): read_rnd_buffer_size (default: 8388608 i.e. 8M)
of read_rnd_buffer_size = 8M
of read_rnd_buffer_size is MySql random read buffer size. When reading a row in any order (e.g., sorted order), assigning a random read buffer. When ordering inquiries, MySql will first scan it again this buffer to avoid disk seeks, speed up the search, if needed sorting large amounts of data, it should adjust the value. But MySql will release the buffer space for each client connection, it should be possible to set the appropriate value to avoid memory overhead is too large.

   4): tmp_table_size (default: 8388608 namely: 16M)
the tmp_table_size = 64M
   tmp_table_size MySql is a heap (deposited) Table buffer size. All the joint within a DML instruction is completed, and most of the joint can not even have a temporary table that is complete. Most temporary tables are memory-based (HEAP) tables. Having a temporary table in the hard disk (and the length of all the columns) is large record length or a BLOB table storing column. If an internal heap (accumulate) table size exceeds tmp_table_size, MySQL can automatically memory heap table changed as needed based on the hard disk MyISAM table. You can also increase the temporary table size by setting tmp_table_size options. That is, if the increase in value, MySql while increasing the size of the heap table, it can achieve the effect of improving join query speed.

2.3 Other Cache:

   . 1): the table_cache (default value: 512)
table_cache (5.1.3 or later known TABLE_OPEN_CACHE)
the table_cache specified table size of the cache. Whenever access to a MySQL table, if there is space in the buffer table, the table is opened and put into it, so you can quickly access the table of contents. Peak time by checking the status value Open_tables and Opened_tables, can decide whether to increase the value of table_cache. If you find open_tables equal table_cache, and opened_tables growing, then you need to increase the value of the table_cache (value above state can use SHOW STATUS LIKE 'Open% tables' get). Note that not blindly table_cache set to a large value. If set too high, it may cause insufficient file descriptors, resulting in unstable performance or connection failure.

The STATUS the LIKE SHOW 'the Open Tables%';
+ ------- + --------------- +
| variable_name | the Value |
+ ---------- + ------- + -----
| Open_tables | 356 |
| Opened_tables | 0 |
+ --------------- + ------- +
open_tables denotes the number of cache tables currently open, flush tables if performing an operation, the cache will be closed, some of the tables are not currently used such that the value of this reduced state; opend_tables table represents the number of cache once opened, it would have been accumulated, if flush tables perform operations, the value is not reduced.
Mysql is installed by default in the case, when the value of table_cache value in 2G memory machine following the default 256-512, if the machine has 4G memory, the default value is 2048, but this decision means that the larger the machine's memory, this value It should be greater, because the table_cache increase, making mysql SQL response speed faster, inevitably produce more of a deadlock (dead lock), so instead of making the entire set of database operations slow down, seriously affecting performance. So usually maintenance or to make a judgment based on the actual situation to the library, find the most suitable for you to maintain libraries table_cache value.
Because MySQL is multi-threaded mechanism, in order to improve performance, each thread is to open its own file descriptor table of their own needs, rather than by sharing already open. Of course, not the same method for the treatment of different storage engines.
In myisam table engines descriptor data file (descriptor) is not shared, but the index file descriptor is shared by all threads .Innodb and using the type of table space, if it is shared table space then the actual data on a file , of course, occupied by the data file descriptor will be less than a separate table space.
to the mysql manual recommended size is: = max_connections * the n-table_cache
the n-represents the maximum number of tables in a query, you also need to keep some extra temporary tables and files file descriptor.
This data was questioned by many, table_cache good enough, check Opened_tables value, if this value is large, growing rapidly or you have to consider increasing the table_cache.

   2): thread_cache_size (server thread cache)

thread_cache_size = 64-
default thread_cache_size = 8, but see a lot of examples where the value of the configuration is generally 32, 64, or even 128, I feel this should help optimize the parameters, so check down:
according to the survey found that more than thread_cache_size thread cache server is not set, or set too small, this value indicates the number of threads can be reused saved in the cache, the cache when disconnected if there is space, then the client thread will be placed in the cache, if the thread again requested, then the request read from the cache, if the cache is empty or a new request, then the thread will be re-created, if there are many new threads, increase this value can improve system performance by comparing Connections and Threads_created state variable, this variable effect can be seen. (-> represents the value to be adjusted) in accordance with the following rules of physical memory provided:
. 1G ->. 8
2G -> 16
3G -> 32    
4G -> 64

  MySQL> Show Status like 'Thread%';
+ ------- + --- +
| variable_name | Value |
+ ------- + --- +
| Threads_cached | 0 | <- the current number of cached idle threads
| Threads_connected | 1 | <- is being used (in connection state) thread
| Threads_created | 1498 | <- since the service started, create a number of threads
| Threads_running | 1 | <- is busy thread (are querying data, data transmission, etc. operations)
+ ------- + --- +

Check boot up the database connection is how many times?

MySQL> Show Status like '% Connection%';
+ -------- + --- +
| variable_name | Value |
+ -------- + --- +
| Connections | 1504 | - > since the service started, the historical connections
| Max_used_connections | 2 |
+ -------- + --- +
to determine the percentage by connecting the thread pool settings are appropriate? More than 90% hit rate, set reasonable.
 (Connections - Threads_created) / Connections * 100%

Guess you like

Origin blog.csdn.net/weixin_41109346/article/details/81987488