MySQL5.7 tuning and optimization scheme configured InnoDB

Mysql InnoDB engine used in the application is very extensive, share this article a little PHP configuration and tuning program for the Mysql5.7 in InnoDB.
InnoDB provides MySQL with a with a commit, rollback, and crash recovery capabilities of transaction-safe (ACID compliant) storage engine. InnoDB row-level locking SELECT statement and also provides an Oracle-style consistent non-locking read. These features increase multi-user deployment and performance. There is no need to expand locked in InnoDB, because InnoDB BOC-level locking for very small space. InnoDB also supports FOREIGN KEY mandatory. In the SQL query, you can freely type InnoDB table types with other MySQL tables mixed up, even in the same query can also be mixed.
Before performing optimization, we make sure the current configuration of the database with the following command:
MySQL> Show the Variables like "% innodb%"; this will innodb all relevant parameters are displayed, then we optimize the key parameters.
A, innodb_buffer_pool_size
this is the most important parameter Innodb, the main role is innodb table index buffer, the data buffer when inserting data, the default value is 128M. If you are a dedicated DB server, it can account for 70% -80% of memory. The larger is not set, the better. Set too large, will lead to the system swap space is occupied, causing the operating system slows down, thereby reducing the efficiency of the sql query. If your data is relatively small, so you can allocate data size + about 10% as the value of the parameter. For example: the data size is 50M, then the values assigned to innodb_buffer_pool_size = 64M enough. Set Method: In my.cnf file: innodb_buffer_pool_size = 4G
Two, the innodb_log_file_size This parameter specifies a log group, the size of each log. The logfile is innodb transaction logs for recovery after the mysql crash. So set a reasonable size is very important for the performance of mysql, directly affect the write speed of the database, transaction size, recovery after abnormal reboot. Mysql 5.5 and 5.5 in the previous innodb logfile is set to maximum 4GB, 5.6 in future versions logfile can be set to a maximum of 512GB. 256M can generally take into account the performance and speed of recovery. Set Method: In my.cnf file: innodb_log_file_size = 256M
transaction buffer in memory, that is, the log buffer size, default settings, with a high number of transactions can be considered to 16M.
Three, innodb_flush_log_at_trx_commit submission control transactions, that is the way to control disk log to refresh. This parameter only three values (0, 1). The default is 1, higher performance can be set to 0 or 2, which can be appropriate to reduce disk IO (but lost a second transaction.), Game MySQL database proposal is set to 0. Do not change the main library. Where: 0: The data in the log buffer once per second will be written to the log file, and the file will also be synchronized operating system to disk, but commit each transaction and will not trigger any log buffer to the log refresh file to disk or file system refresh operation; the default is 1 :( 1) at each commit will logbuffer the data is written to a log file, the file system will also trigger synchronization to disk; 2 : transaction commit triggers 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.
Description: This setting parameters have a great impact on the performance of Innodb, so here more than to explain.
When this value is 1: innodb transaction LOG written to the log file after each submission, and the logs do flushed to disk. This can be done without losing any transaction.
When the value of 2: Submit in each log buffer is written to the file, but not to the log file flush to disk operation, refresh the log files also occur in the case of a value of 2 per second at a time. But note that, due to the problem calls in the process, and can not guarantee 100% of the per occurrence. So the performance is the fastest. But the operating system crash or power failure will delete the last second of transactions.
When this value is 0: once per second the log buffer is written to the log file, and the flush to disk operation of the log file, but submitted no operation in a transaction. mysqld process crash can erase the last second transaction before the collapse. From the above analysis, when this value is not 1, it can achieve better performance, but an exception is encountered there will be a loss, so it is necessary to measure according to their own circumstances.
Five, innodb_flush_method this parameter controls innodb data files and redo log is open, flashing mode. There are three values: fdatasync (default), O_DSYNC, O_DIRECT.
The default is fdatasync, call fsync () to brush buffer data files and redo log in.
When is O_DSYNC, innodb uses O_DSYNC opened and flashed redo log, using the fsync () brush to write the data file.
Is O_DIRECT, innodb use O_DIRECT open the data file, use fsync () to write the data file with the brush redo log. In unix operating system, the file is opened influence way O_DIRECT minimizes buffering io and io the document is directly on the buffer in user space operation, and io operation is synchronous, and therefore whether it is read () system call or write () system call, data is guaranteed to be read from disk.
innodb_flush_method = O_DIRECT
MySQL 5.7 provides a more appropriate default values, as long as the following three options to adjust the general can, and then the remaining parameters according to the actual situation.
#################
innodb_buffer_pool_size = 8G
innodb_log_file_size = 256M
innodb_flush_method = O_DIRECT
Linux server, the memory is 32G, as also deployed other applications, all here buffer_pool_size to set up 8G.

Published 65 original articles · won praise 88 · views 280 000 +

Guess you like

Origin blog.csdn.net/uisoul/article/details/85321652