MYSQL common configuration parameter optimization

Foreword

  MySQL optimization is the most difficult part, not only need to understand MySQL professional knowledge, but also need long-term observation and statistical judgment based on experience, and then set reasonable parameters. Let's look at some basic MySQL optimization.

MySQL parameters optimized for different websites and online volume, number of visits, number of posts, network conditions, and machine hardware configurations have a relationship, a one-time optimization can not be completed, requires constant observation and testing, will it be possible to get the best Effect.

1) variable connection request

1、max_connections

  MySQL the maximum number of connections , the connection request to the server if a concurrent large 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 more connections, each aftertaste MySQL connection provides the connection buffer, it will cost more memory, so to adjust the value, not blindly increase the set value.

    Too mant connetcions error: The value is too small often ERROR 1040 will be by mysql> show status like 'connections' ; wildcards to view the number of connections current state (trying to connect to MySQL (number of connections regardless of whether the connection is successful)) to the size of the decided value.
    Variables like Show ' max_connections ': the maximum number of connections
    Show Variables like ' max_used_connection ': a corresponding number of connections
    (max_used_connection / max_connections) * 100% ( equal to about 85% over the value)
    if max_used_connections and max_connections same, then the value is set too low, or max_connections a server load exceeds the upper limit, is set less than 10% is too large.

2、back_log

  The number of connections MySQL can be temporarily stored , default is 50. When the main MySQL thread is get very many connection requests in a short period of time, he will play a role. If the connection data reaches max_connections MySQL, 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 number exceeds The back_log for connections, connection resources will not be accepted.

3、wait_timeout和interative_timeout

  wait_timeout = 120: After completion of the operation application connection mysql, 120 seconds after the idle disconnect
  interative_timeout = 120: the Client refers mysql mysql connection operation is completed, the idle 120 seconds OFF

  Effects on the Properties of
  the wait_timeout
        (. 1), if set too small, then the connection is closed quickly, so that some permanent connection does not work
        (2) If too easily lead connection open for too long, when show processlist, can be see the connection too much sleep state, resulting in too many connections error.
        (3) It is generally desirable wait_timeuot as low as possible
        interative_timeout setting will not have much impact on your web application

2) buffer variable

Global buffer

4、key_buffer_size

  key_buffer_size specified index buffer size, he decided to deal with the speed of the index, especially an index reading speed.

  Value key_read_requests and key_reads by checking the status, you can know key_buffer_size set is reasonable.

  Ratio key_reads / key_read_requests should be as low as possible, is at least 1: 100, 1: 1000 better (using the above-described state value may show status like 'key_read%' obtained)

    The probability of cache misses:
    key_cache_miss_rate = key_reads / Key_read_requests * 100%
    key_buffer_size only MAISAM tables.

    Key_buffer_size how to adjust the value of
    the default configuration number 8388608 (8M), the host has 4G memory can tune value 268435456 (256M)

5, query_cache_size (query cache referred QC)

    Use the query cache, MySQL query results stored in the buffer, in the future, will read the results of the same select statement (case-sensitive) directly from the buffer.
    If you start with a SQL query select, then MySQL server will attempt to use its query cache.

    Note: The two SQL statements, even if it is just a difference of a character (for example, the case is different: more than one space, etc.), then the two will use a different SQL cache

    By show status like 'Qcache%' you can know query_cache_size settings are reasonable

    Qcache_free_blocks: the number of adjacent memory block cache. If the value displayed is too large, then the Query Cache is a memory fragmentation more.
    Note: When a table is updated, the associated cache block and he will be free. But the block may still exist in the queue, unless it is in the tail of the queue. You can flush query cache statement to clear the free blocks.

    Qcache_free_memory: Query Cache remaining in the current memory size. By this parameter we can provide a more accurate observation Query Cache memory size, the current system is adequate, or too much of an increase is needed.

    Qcache_hits: indicates how many times a cache hit. We can verify the main query can be cached by the effect of our values. The larger the number the more ideal buffer effect.

    Qcache_inserts: indicates how many times misses insert, which means new to SQL request was not found in the cache and had to perform query processing, query processing the results after execution with insert query cache. The more the number of such cases, the application to indicate query cache is relatively small, the effect is not ideal.

    Qcache_lowmen_prunes:多少条Query因为内存不足而被清除出Query Cache,通过Qcache_lowmem_prunes和Qcache_free_memory 相互结合,能够更清楚的了解到我们系统中Query Cache的内存大小是否真的足够,是否非常频繁的出现因为内存不足而有Query被换出。这个数字最好是长时间来看,如果这个数字在不断增长,就表示可能碎片化非常严重,或者内存很少。

    Qcache_queries_in_cache:当前Query Cache 中cache的Query数量
    Qcache_total_blocks:当前Query Cache中block的数量

    Query Cache的限制
    a)所有子查询中的外部查询SQL 不能被Cache:
    b)在p'rocedure,function以及trigger中的Query不能被Cache
    c)包含其他很多每次执行可能得到不一样的结果的函数的Query不能被Cache

6、max_connect_errors:

  是一个MySQL中与安全有关的计数器值,他负责阻止过多尝试失败的客户端以防止暴力破解密码的情况,当超过指定次数,MySQL服务器将禁止host的连接请求,直到mysql服务器重启或通过flush hotos命令清空此host的相关信息。(与性能并无太大的关系)

7、sort_buffer_size:

每个需要排序的线程分配该大小的一个缓冲区。增加这值加速ORDER BY 或 GROUP BY操作
    sort_buffer_size是一个connection级的参数,在每个connection(session)第一次需要使用这个buffer的时候,一次性分配设置的内存。
    sort_buffer_size:并不是越大越好,由于是connection级的参数,过大的设置+高并发可能会耗尽系统的内存资源。例如:500个连接将会消耗500*sort_buffer_size(2M)=1G

8、max_allowed_packet=32M

    根据配置文件限制server接受的数据包大小。

9、join_buffer_size=2M

    用于表示关联缓存的大小,和sort_buffer_size一样,该参数对应的分配内存也是每个连接独享。

10、thread_cache_size=300

  服务器线程缓存,这个值表示可以重新利用保存在缓存中的线程数量,当断开连接时,那么客户端的线程将被放到缓存中以响应下一个客户而不是销毁(前提时缓存数未达上限)。

  1)如果线程重新被请求,那么请求将从缓存中读取;

  2)如果缓存中是空的或者是新的请求,这个线程将被重新请求,那么这个线程将被重新创建;

  3)如果有很多新的线程,增加这个值可以改善系统性能,通过比较Connections和Threads_created状态的变量,可以看到这个变量的作用。

  设置规则如下:1G内存配置为8,2G内存为16.  服务器处理此客户的线程将会缓存起来以响应下一个客户而不是被销毁(前提是缓存数未到达上限)

  Threads_cached:代表当前此时此刻线程缓存中有多少空闲线程。
  Threads_connected:代表当前已建立连接的数量,因为一个连接就需要一个线程,所以也可以看成当前被使用的线程数。
  Threads_created:代表最近一次服务启动,已创建线程的数量,如果发现Threads_created值过大的话,说明MySQL服务器一直在创建线程,这也比较消耗资源,可以适当增加配置文件中thread_cache_size值
  Threads_running:代表当前激活的(非睡眠状态)线程数。并不是代表正在使用的线程数,有时候连接已建立,但是连接处于sleep状态。

3)配置Innodb的几个变量

11、innodb_buffer_pool_size

  对于innodb表来说,innodb_buffer_pool_size的作用相当于key_buffer_size对于MyISAM表的作用一样。Innodb使用该参数指定大小的内存来缓冲数据和索引。最大可以把该值设置成物理内存的80%。

12、innodb_flush_log_at_trx_commit

    主要控制了innodb将log buffer中的数据写入日志文件并flush磁盘的时间点,取值分别为0,1,2.

    实际测试发现,该值对插入数据的速度影响非常大,设置为2时插入10000条记录只需要两秒,设置为0时只需要一秒,设置为1时,则需要229秒。因此,MySQL手册也建议尽量将插入操作合并成一个事务,这样可以大幅度提高速度。

13、innodb_thread_concurrency=0

    此参数用来设置innodb线程的并发数,默认值为0表示不被限制,若要设置则与服务器的CPU核心数相同或是CPU的核心数的2倍。

14、innodb_log_buffer_size

    此参数确定日志文件所用的内存大小,以M为单位。缓冲区更大能提高性能,对于较大的事务,可以增大缓存大小。

15、innodb_log_file_size=50M

    此参数确定数据日志文件的大小,以M为单位,更大的设置可以提高性能。

16、innodb_log_files_in_group=3

    为提高性能,MySQL可以以循环方式将日志文件写到多个文件。推荐设置为3

17、read_buffer_size=1M

    MySQL 读入缓冲区大小。对表进行顺序扫描的请求将分配到一个读入缓冲区MySQL会为他分配一段内存缓冲区

18、read_rnd_buffer_size=16M

    MySQL 的随机读(查询操作)缓冲区大小。当按任意顺序读取行时(例如,按照排序顺序),将分配到一个随机都缓冲区。进行排序查询时,MySQL会首先扫描一遍该缓冲区,以避免磁盘搜索,提高查询速度,如果需要排序大量数据,可适当调高该值。但是MySQL会为每个客户连接发放该缓冲空间,所以应尽量适当设置该值,以避免内存消耗过大。
    注:顺序读是根据索引的叶节点数据就能顺序的读取所需要的行数据。随机读是指一般需要根据辅助索引叶节点中的主键寻找侍其巷进行数据,而辅助索引和主键所在的数据端不同,因此访问方式是随机的。

19、bulk_insert_buffer_size=64M

    批量插入数据缓存大小,可以有效的提高插入效率,默认为8M

20、binary log

    binlog_cache_size=2M   //为每个session分配的内存,在事务过程中用来存储二进制日志的缓存,提高记录bin-log的效率。

    max_binlog_cache_size=8M //表示的是binlog能够使用的最大cache内存大小

    max_binlog_size=512M  //指定binlog日志文件的大小。不能将变量设置为大于1G或小于4096字节。默认值为1G.在导入大容量的sql文件时,建议关闭,sql_log_bin,否则硬盘扛不住,而且建议定期做删除。

    expire_logs_days=7  //定义了mysql清除过期日志的时间

    参数汇总:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
server_id = 1
socket = /usr/local/mysql/mysql.sock
log-error = /usr/local/mysql/data/mysqld.err
slow_query_log = 1
slow_query_log_file=/usr/local/mysql/data/slow-query.log
long_query_time = 1
log-queries-not-using-indexes
max_connections = 1024
back_log = 128
wait_timeout = 60
interactive_timeout = 7200
key_buffer_size = 256M
query_cache_size = 256M
query_cache_type = 1
query_cache_limit = 50M
max_connect_errors = 20
sort_buffer_size = 2M
max_allowed_packet = 32M
join_buffer_size = 2M
thread_cache_size = 200
innodb_buffer_pool_size = 2048M
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
log-bin=/usr/local/mysql/data/mysqlbin
binlog_cache_size = 2M
max_binlog_cache_size = 8M
max_binlog_size = 512M
expire_logs_days = 7
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
 
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
 
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
 
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

Guess you like

Origin www.cnblogs.com/FondWang/p/12582844.html