MySQL configuration optimization

This configuration is the configuration used by the old boy on the production line. During training , he gave it. Here, I added Chinese to each parameter to explain that
this configuration has been optimized well. If your mysql has no special circumstances, you can Directly use this configuration parameter


MYSQL server my.cnf configuration document to explain the
hardware: memory 16G

[client]
port = 3306
socket = /data/3306/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3306/data open_files_limit = 10240
back_log = 600 #How    many


in a short time before MYSQL temporarily stops responding to new requests Requests can be stored on the stack. If the system has many connections in a short period of time, you need to increase the value of this parameter, which specifies the size of the listening queue for incoming TCP/IP connections. The default value is 50.

max_connections = 3000   
#MySQL allows the maximum number of process connections. If the error prompt of Too Many Connections frequently appears, you need to increase this value.

max_connect_errors = 6000 #Set   
the maximum number of abnormal interruptions of connection requests for each host. When the number of times is exceeded, the MYSQL server will prohibit the connection request of the host until the mysql server is restarted or the relevant information of this host is cleared through the flush hosts command .

table_cache = 614 #Instruct  
the table to adjust the buffer size. # The table_cache parameter sets the number of table caches. Every connection comes in, at least one table cache is opened. # Therefore, the size of table_cache should be related to the setting of max_connections. For example, for 200 #joins running in parallel, you should have a table cache of at least 200 × N , where N is the maximum number of tables in a join that the application can execute the query#. Also, some extra file descriptors need to be reserved for temporary tables and files.
# When  Mysql  accesses a table, if the table has been opened in the cache, you can directly access the cache; if # has not been cached, but there is still space in the Mysql table buffer, then the table is opened and placed Enter the table buffer#buffer; if the table cache is full, the currently unused table will be released according to certain rules, or the table cache will be temporarily expanded for storage. The advantage of using the table cache is that the content of the table can be accessed more quickly . Executing flush tables will #empty the contents of the cache. In general, it can be done by looking at the databaseRun the status values ​​of Open_tables # and Opened_tables at the peak time, and determine whether to increase the value of table_cache (where open_tables is the number of currently opened tables, and Opened_tables is the number of tables that have been opened). That is, if open_tables is close to table_cache, and the value of Opened_tables is gradually increasing, consider increasing the size of this # value. There is also a need to increase table_cache when Table_locks_waited is relatively high.


external-locking=FALSE  
#Use the --skip-external-locking MySQL option to avoid external locking. This option is enabled by default

max_allowed_pack et = 32M  
#Set the maximum value of one message transmission in network transmission. The system default value is 1MB, the maximum value is 1GB, and a multiple of 1024 must be set.

sort_buffer_size = 2M  
# Sort_Buffer_Size is a connection-level parameter. When each connection (session) needs to use the buffer for the first time, the set memory is allocated at one time.
#Sort_Buffer_Size is not as big as possible, because it is a connection-level parameter, too large setting + high concurrency may exhaust system memory resources. For example: 500 connections will consume 500*sort_buffer_size(8M)=4G memory
#When Sort_Buffer_Size exceeds 2KB, mmap() will be used instead of malloc() for memory allocation, resulting in reduced efficiency.
#Technical introduction http://blog.webshuo.com/2011/02/16/mysql-sort_buffer_size/
#dev-doc: http://dev.mysql.com/doc/refman/5.5/en/server-parameters. html
#explain select*from table where order limit; appears filesort #Key
optimization parameter

join_buffer_size = 2M   
#Used for the size of the associated cache between tables. Like sort_buffer_size, the allocated memory corresponding to this parameter is also exclusive to each connection.

thread_cache_size = 300   
# Server thread cache This value indicates the number of threads that can be reused in the cache. If there is space in the cache when disconnecting, the client's thread will be placed in the cache. If the thread is re-requested, Then the request will be read from the cache, if the cache is empty or there is a new request, then the thread will be recreated, if there are many new threads, increasing this value can improve system performance. By comparing Connections and Threads_created status variable, you can see the role of this variable. The setting rules are as follows: 1GB memory is configured as 8, 2GB is configured as 16, 3GB is configured as 32, 4GB or higher memory can be configured larger.

thread_concurrency = 8   
# Whether the value of thread_concurrency is set correctly or not has a great impact on the performance of mysql, in multiple cpsIn the case of u (or multi-core), incorrectly set the value of thread_concurrency, which will cause mysql to not make full use of multiple cpu (or multi-core), and only one cpu (or core) can work at the same time. thread_concurrency should be set to 2 times the number of CPU cores. For example, if there is a dual-core CPU, then the thread_concurrency should be 4; if there are 2 dual-core CPUs, the thread_concurrency value should be 8 #Key
optimization parameter

query_cache_size = 64M   
## For using MySQL Users of , you must not be unfamiliar with this variable. In the MyISAM engine optimization in previous years, this parameter is also an important optimization parameter. But with the development, this parameter also revealed some problems. The memory of the machine is getting larger and larger, and people are habitually assigning larger and larger values ​​to the previously useful parameters. Increasing this parameter also caused a series of problems. Let's first analyze the working principle of query_cache_size: after a SELECT query works in the DB, the DB will cache the statement. When the same SQL is called again in the DB, the DB will store the table without any changes. The result is returned to the Client from the cache. There is a key point here, that is, when the DB uses Query_cache to work, it requires that the table involved in the statement has not changed during this period. So if the table is changed, what to do with the data in Query_cache? First, all the statements related to Query_cache and the table should be invalidated, and then the update will be written. Then if the Query_cache is very large, the query structure of the table is more, and the query statement invalidation is slow, an update or an Insert will be very slow, so what you see is why the Update or Insert is so slow. Therefore, in a system with a large amount of database writes or updates, this parameter is not suitable for too large allocation. Moreover, in systems with high concurrency and a large amount of writing, it is recommended to disable this function.
#Key optimization parameters (main library additions, deletions and changes-MyISAM)

query_cache_limit = 4M #Specify    
the buffer size that a single query can use, the default is 1M

query_cache_min_res_unit = 2k #The    
default is 4KB, setting a large value is good for big data queries, but if your queries are all small data queries, it is easy to cause Memory fragmentation and waste
#Query cache fragmentation rate = Qcache_free_blocks / Qcache_total_blocks * 100% #If the
query cache fragmentation rate exceeds 20%, you can use FLUSH QUERY CACHE to sort out cache fragments, or try reducing query_cache_min_res_unit, if your queries are all small data quantity.
#Query cache utilization = (query_cache_size – Qcache_free_memory) / query_cache_size * 100% #If
the query cache utilization is below 25%, the query_cache_size setting is too large and can be appropriately reduced; the query cache utilization is above 80% and Qcache_lowmem_prunes > 50 It means that query_cache_size may be a little small, or there are too many fragments.
#Query cache hit rate = (Qcache_hits – Qcache_inserts) / Qcache_hits * 100%

default-storage-engine = MyISAM
#default_table_type = InnoDB

thread_stack = 192K #Set  

the stack size of each thread of MYSQL, the default value is large enough to meet normal operations. The range that can be set is 128K to 4GB, and the default is 192KB.

transaction_isolation = READ-COMMITTED   
# Set the default transaction isolation level. The available levels are as follows:
# READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE
# 1.READ UNCOMMITTED-read uncommitted 2.READ COMMITTE-read committed 3.REPEATABLE READ - Repeatable read 4.SERIALIZABLE - Serial

tmp_table_size = 256M   
# The default size of tmp_table_size is 32M. If a temporary table exceeds this size, MySQL generates an error of the form The table tbl_name is full. If you do many advanced GROUP BY queries, increase the tmp_table_size value. If this value is exceeded, the temporary table will be written to disk.
max_heap_table_size = 256M
long_query_time = 2
log_long_form at log-slow-queries=/data/3306/slow-log.log # log-bin = /data/3306/mysql-bin log-bin binlog_cache_size = 4M max_binlog_cache_size = 8M max_binlog_size = 512M expire_logs_days = 7








key_buffer_size = 2048M #Batch 

the size of the buffer used for indexing. Increasing it can get better index processing performance. For servers with about 4GB of memory, this parameter can be set to 256MB or 384MB.

read_buffer_size = 1M  
# MySql read buffer size. A request for a sequential scan of the table will allocate a read buffer, and MySql will allocate a memory buffer for it. The read_buffer_size variable controls the size of this buffer. If the sequential scan requests to the table are very frequent, and you think that the frequent scan is going too slowly, you can improve its performance by increasing the value of this variable and the size of the memory buffer. Like sort_buffer_size, the allocated memory corresponding to this parameter is also exclusive to each connection.

read_rnd_buffer_size = 16M   
# MySql's random read (query operation) buffer size. When rows are read in any order (for example, in sorted order), a random read buffer is allocated. When performing a sorting query, MySql will first scan the buffer to avoid disk search and improve query speed. If you need to sort a large amount of data, you can appropriately increase the value. However, MySql will release the buffer space for each client connection, so you should try to set this value appropriately to avoid excessive memory overhead.

bulk_insert_buffer_size = 64M #Bulk   
insert data cache size, which can effectively improve insertion efficiency, the default is 8M

myisam_sort_buffer_size = 128M   
# The buffer required for reordering when MyISAM table changes

myisam_max_sort_file_size = 10G   
# The size of the maximum temporary file allowed by MySQL when rebuilding the index (when REPAIR, ALTER TABLE or LOAD DATA INFILE).
# If the file size is larger than this value, the index will be created through the key-value buffer (slower)

myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1   

# If a table has more than one index, MyISAM can use more than one thread to repair them by parallel sorting.
# This is a good choice for users with multiple CPUs and a lot of memory.

myisam_recover #Automatically   
check and repair MyISAM tables that are not properly closed
skip-name-resolve
lower_case_table_names = 1

server-id = 1


innodb_additional_mem_pool_size = 16M   
#this The parameter is used to set the memory pool size of the data directory information and other internal data structures stored by InnoDB, similar to Oracle's lib rar y cache. This is not a mandatory parameter and can be breached.

innodb_buffer_pool_size = 2048M   
# This is very important for Innodb tables. Innodb is more sensitive to buffering than MyISAM tables. MyISAM works fine with the default key_buffer_size setting, but Innodb is snail-like with the default innodb_buffer_pool_size setting. Since Innodb caches data and indexes, there is no need to leave too much memory for the operating system, so if you only need to use Innodb, you can set it up to 70-80% of the available memory. Some of the rules applied to key_buffer are - if your data volume is not large, and it will not explode, then you don't need to set innodb_buffer_pool_size too large

innodb_data_file_path = ibdata1:1024M:autoextend #tablespace   
file important data

innodb_file_io_threads = 4   
#file IO The number of threads is generally 4, but under Windows, it can be set to a larger value.

innodb_thread_concurrency = 8 #Set   
the number of CPUs the server has. It is recommended to use the default setting, generally 8.

innodb_flush_log_at_trx_commit = 2   
# If this parameter is set to 1, the log will be written to disk after each transaction is committed. Can be set to 0 or 2 for performance, but at the risk of losing data in the event of a failure. A setting of 0 means that the transaction log is written to the log file, and the log file is flushed to disk every second. A setting of 2 means that the transaction log will be written to the log on commit, but the log file will be flushed to disk once per time.

innodb_log_buffer_size = 16M  
#This parameter determines the memory size used by these log files, in M ​​units. Larger buffers can improve performance, but unexpected failures will lose data. MySQL developers recommend setting between 1-8M

innodb_log_file_size = 128M   
#This parameter determines the size of the data log file, in M ​​units, larger settings Can improve performance, but also increase the time required to recover a failed database

innodb_log_files_in_group = 3 #To   
improve performance, MySQL can write log files to multiple files in a circular fashion. The recommended setting is 3M

innodb_max_dirty_pages_pct = 90   
#Recommended reading http://www.taobaodba.com/html/221_innodb_max_dirty_pages_pct_checkpoint.html
# The number of Dirty_Pages in Buffer_Pool directly affects the closing time of InnoDB. The parameter innodb_max_dirty_pages_pct can directly control the ratio of Dirty_Page in Buffer_Pool, and luckily innodb_max_dirty_pages_pct can be dynamically changed. Therefore, before closing InnoDB, reducing innodb_max_dirty_pages_pct and forcing the data block to Flush for a period of time can greatly shorten the time for MySQL to close.

innodb_lock_wait_timeout = 120   
# InnoDB has its built-in deadlock detection mechanism that can cause unfinished transactions to be rolled back. However, if you use MyISAM's lock tables statement or a third-party transaction engine with InnoDB, InnoDB cannot recognize deadlocks. To eliminate this possibility, innodb_lock_wait_timeout can be set to an integer value indicating how long (in seconds) MySQL waits before allowing other transactions to modify data that is eventually rolled back by the transaction

innodb_file_per_table = 0   
#exclusive tablespace ( Close)

[mysqldump]
quick
max_allowed_packet = 32M

[mysqld_safe]
log-error=/data/3306/mysql_oldboy.err
pid-file=/data/3306/mysqld.pid




#Add
#wait_timeout = 10 #Specify   
the maximum connection time for a request , for a server with around 4GB of memory, you can set it to 5-10.
#skip_networking #Enable   
this option to completely turn off the MYSQL TCP/IP connection method. If the WEB server accesses the MYSQL database server through a remote connection, do not enable this option, otherwise it will not be able to connect normally.

#log-queries-not-using-indexes
also log queries that do not use indexes

转载请注明linux系统运维
http://www.linuxyw.com/linux/shujuku/20130506/216.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326237492&siteId=291194637