参数 innodb_dedicated_server的说明
MySQL 8 中可以设置参数 innodb_dedicated_server=ON来让MySQL自动探测服务器的内存大小,根据内存大小设置innodb_buffer_pool_size, innodb_log_file_size 和 innodb_flush_method 三个参数。具体策略如下:
innodb_buffer_pool_size: <1G: 128M(innodb_dedicated_server=为OFF时的默认取值)
<=4G: 探测到的物理内存 * 0.5
大于4G: 探测到的物理内存 * 0.75
innodb_log_file_size:
<1G: 48M(innodb_dedicated_server=为OFF时的默认取值)
<=4G: 128M
<=8G: 512M
<=16G: 1024M
大于16G: 2G
innodb_flush_method:
如果系统允许设置为O_DIRECT_NO_FSYNC。如果系统不允许,则设置为InnoDB默认的Flush method。
上述这些参数在MySQL每次启动时自动探测服务器(包括虚拟机和容器的内存)配置并自动生效。
设置效果
innodb_dedicated_server=为OFF时
scutech@scutech:~$ mysqladmin variables|grep -E 'innodb_buffer_pool_size|innodb_flush_method|innodb_log_file_size' | innodb_buffer_pool_size | 134217728 | | innodb_flush_method | fsync | | innodb_log_file_size | 2073034752 innodb_dedicated_server=ON
mysqladmin variables|grep -E 'innodb_buffer_pool_size|innodb_flush_method|innodb_log_file_size' | innodb_buffer_pool_size | 1073741824 | | innodb_flush_method | O_DIRECT_NO_FSYNC | | innodb_log_file_size | 2073034752
对参数innodb_log_files_in_group的作用
MySQL文档中有这样的说明:
The number of log files is configured according to the automatically configured buffer pool size (in gigabytes). Automatic configuration of the innodb_log_files_in_group variable was added in MySQL 8.0.14.
从 MySQL 8.0.14后这个参数还影响日志文件的个数。
Table Automatically Configured Number of Log Files
Buffer Pool Size Number of Log Files
Less than 8GB ROUND(buffer pool size)
8GB to 128GB ROUND(buffer pool size * 0.75)
Greater than 128GB 64
配置了innodb_dedicated_server后,系统一启动多了64个日志文件ib_logfile,吓了我一跳!看看!
[root@localhost data]# ls auto.cnf client-cert.pem ib_logfile10 ib_logfile18 ib_logfile25 ib_logfile32 ib_logfile4 ib_logfile47 ib_logfile54 ib_logfile61 mysql server-key.pem binlog.000001 client-key.pem ib_logfile11 ib_logfile19 ib_logfile26 ib_logfile33 ib_logfile40 ib_logfile48 ib_logfile55 ib_logfile62 mysqld-auto.cnf sys binlog.000002 #ib_16384_0.dblwr ib_logfile12 ib_logfile2 ib_logfile27 ib_logfile34 ib_logfile41 ib_logfile49 ib_logfile56 ib_logfile63 mysql.ibd undo_001 binlog.000003 #ib_16384_1.dblwr ib_logfile13 ib_logfile20 ib_logfile28 ib_logfile35 ib_logfile42 ib_logfile5 ib_logfile57 ib_logfile7 performance_schema undo_002 binlog.000004 ib_buffer_pool ib_logfile14 ib_logfile21 ib_logfile29 ib_logfile36 ib_logfile43 ib_logfile50 ib_logfile58 ib_logfile8 private_key.pem binlog.index ibdata1 ib_logfile15 ib_logfile22 ib_logfile3 ib_logfile37 ib_logfile44 ib_logfile51 ib_logfile59 ib_logfile9 public_key.pem ca-key.pem ib_logfile0 ib_logfile16 ib_logfile23 ib_logfile30 ib_logfile38 ib_logfile45 ib_logfile52 ib_logfile6 ibtmp1 sbtest ca.pem ib_logfile1 ib_logfile17 ib_logfile24 ib_logfile31 ib_logfile39 ib_logfile46 ib_logfile53 ib_logfile60 #innodb_temp server-cert.pem
这个机器内存256G,看看innodb_buffer_pool_size:
mysql> show variables like 'innodb_buffer_pool_size'; +-------------------------+--------------+ | Variable_name | Value | +-------------------------+--------------+ | innodb_buffer_pool_size | 202937204736 | +-------------------------+--------------+ 1 row in set (0.00 sec)
注意事项
innodb_dedicated_server=ON的情况下,如果还显式设置了 innodb_buffer_pool_size / innodb_log_file_size / innodb_flush_method 参数,显式设置的这些参数会优先生效,并且在MySQL的错误日志中会打印如下内容:
2020-03-14T03:24:27.437740Z 0 [Warning] [MY-012360] [InnoDB] Option innodb_dedicated_server is ignored for innodb_log_file_size because innodb_log_file_size=2073034752 is specified explicitly.
显式指定某一个值,并不会影响其他变量的自适应参数值设置。
在innodb_dedicated_server=ON的情况下,mysqld服务进程每次重启后都会自动调整上述几个参数值。在任何时候MySQL都不会将自适应值保存在持久配置中,利用这个参数就可以保证服务器(虚拟机或者容器)扩展以后,MySQL能“自适应”,以尽量消耗更多的服务器资源。