mysql8性能参数查看及优化
mysql的整体性能和基本环境和参数配置都有很大的关系,在特定项目环境下,mysql的运行环境就是确定的,比如cpu配置,内存大小,磁盘类型等。为了充分利用环境提供的处理能力,就有必要对相关参数进行优化配置,以达到性能优化的目的。
当然,参数值的优化配置也是需要和具体的环境信息进行综合考虑的,比如对于16G内存的服务器和8G以及4G内存的服务器,优化后的参数值通常是不一样的。除此之外,还需要根据业务特定进行优化,比如对于需要快速响应的业务需求和批处理业务需求,可能在参数优化上的侧重点就会不一样。
下面对相关参数,分别列举了云生产环境、测试环境和本地开发环境,对比之后,就可以找到合适的参数配置:
1. innodb_buffer_pool_size(*)
innodb_buffer_pool_size
一般为内存的70%~80%
;innodb_buffer_pool_size = 256MB
查看方法:
-- 云生产环境 cloud mysql:
mysql> SELECT 'innodb_buffer_pool_size' AS name, @@innodb_buffer_pool_size/1024/1024 AS 'value(MB)';
+-------------------------+---------------+
| name | value(MB) |
+-------------------------+---------------+
| innodb_buffer_pool_size | 2868.00000000 |
+-------------------------+---------------+
1 row in set (0.00 sec)
-- 测试环境 192.168.4.125:
mysql> SELECT 'innodb_buffer_pool_size' AS name, @@innodb_buffer_pool_size/1024/1024 AS 'value(MB)';
+-------------------------+--------------+
| name | value(MB) |
+-------------------------+--------------+
| innodb_buffer_pool_size | 128.00000000 |
+-------------------------+--------------+
1 row in set (0.02 sec)
-- 本地开发环境 localhost
mysql> SELECT 'innodb_buffer_pool_size' AS name, @@innodb_buffer_pool_size/1024/1024 AS 'value(MB)';
+-------------------------+------------+
| name | value(MB) |
+-------------------------+------------+
| innodb_buffer_pool_size | 8.00000000 |
+-------------------------+------------+
1 row in set
2. join_buffer_size
join_buffer_size = 16M
-- 云生产环境 cloud mysql:
mysql> SELECT 'join_buffer_size' AS name, @@join_buffer_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| join_buffer_size | 0.34375000 |
+------------------+------------+
1 row in set (0.00 sec)
-- 测试环境 192.168.4.125:
mysql> SELECT 'join_buffer_size' AS name, @@join_buffer_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| join_buffer_size | 0.25000000 |
+------------------+------------+
1 row in set (0.00 sec)
3. query_cache_size
query_cache_size = 64M
-- 云生产环境cloud mysql:
mysql> SELECT 'query_cache_size' AS name, @@query_cache_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| query_cache_size | 3.00000000 |
+------------------+------------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'query_cache_size' AS name, @@query_cache_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| query_cache_size | 1.00000000 |
+------------------+------------+
1 row in set, 1 warning (0.00 sec)
4. sort_buffer_size
配置文件:
sort_buffer_size = 16M
-- 云生产环境cloud mysql:
mysql> SELECT 'sort_buffer_size' AS name, @@sort_buffer_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| sort_buffer_size | 0.68750000 |
+------------------+------------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'sort_buffer_size' AS name, @@sort_buffer_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| sort_buffer_size | 0.50000000 |
+------------------+------------+
1 row in set (0.00 sec)
-- 本地开发环境localhost
mysql> SELECT 'sort_buffer_size' AS name, @@sort_buffer_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| sort_buffer_size | 0.25000000 |
+------------------+------------+
1 row in set
5. read_buffer_size
配置文件:
read_buffer_size = 16M
-- 云生产环境cloud mysql:
mysql> SELECT 'read_buffer_size' AS name, @@read_buffer_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| read_buffer_size | 0.68750000 |
+------------------+------------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'read_buffer_size' AS name, @@read_buffer_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| read_buffer_size | 0.25000000 |
+------------------+------------+
1 row in set (0.00 sec)
-- 本地开发环境localhost
mysql> SELECT 'read_buffer_size' AS name, @@read_buffer_size/1024/1024 AS 'value(MB)';
+------------------+------------+
| name | value(MB) |
+------------------+------------+
| read_buffer_size | 0.06250000 |
+------------------+------------+
1 row in set
6. max_connections(*)
配置文件:
max_connections = 768
-- 云生产环境cloud mysql:
mysql> SELECT 'max_connections' AS name, @@max_connections AS 'value';
+-----------------+-------+
| name | value |
+-----------------+-------+
| max_connections | 1712 |
+-----------------+-------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'max_connections' AS name, @@max_connections AS 'value';
+-----------------+-------+
| name | value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
-- 本地开发环境localhost
mysql> SELECT 'max_connections' AS name, @@max_connections AS 'value';
+-----------------+-------+
| name | value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set
7. max_allowed_packet(*)
max_allowed_packet = 4M
-- 云生产环境cloud mysql:
mysql> SELECT 'max_allowed_packet' AS name, @@max_allowed_packet/1024/1024 AS 'value(MB)';
+--------------------+---------------+
| name | value(MB) |
+--------------------+---------------+
| max_allowed_packet | 1024.00000000 |
+--------------------+---------------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'max_allowed_packet' AS name, @@max_allowed_packet/1024/1024 AS 'value(MB)';
+--------------------+------------+
| name | value(MB) |
+--------------------+------------+
| max_allowed_packet | 1.00000000 |
+--------------------+------------+
1 row in set (0.00 sec)
-- 本地开发环境localhost
mysql> SELECT 'max_allowed_packet' AS name, @@max_allowed_packet/1024/1024 AS 'value(MB)';
+--------------------+------------+
| name | value(MB) |
+--------------------+------------+
| max_allowed_packet | 4.00000000 |
+--------------------+------------+
1 row in set
8. key_buffer_size
key_buffer_size = 128M
-- 云生产环境cloud mysql:
mysql> SELECT 'key_buffer_size' AS name, @@key_buffer_size/1024/1024 AS 'value(MB)';
+-----------------+-------------+
| name | value(MB) |
+-----------------+-------------+
| key_buffer_size | 16.00000000 |
+-----------------+-------------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'key_buffer_size' AS name, @@key_buffer_size/1024/1024 AS 'value(MB)';
+-----------------+-------------+
| name | value(MB) |
+-----------------+-------------+
| key_buffer_size | 16.00000000 |
+-----------------+-------------+
1 row in set (0.00 sec)
-- 本地开发环境localhost
mysql> SELECT 'key_buffer_size' AS name, @@key_buffer_size/1024/1024 AS 'value(MB)';
+-----------------+------------+
| name | value(MB) |
+-----------------+------------+
| key_buffer_size | 8.00000000 |
+-----------------+------------+
1 row in set
9. back_log(*)
back_log = 500
-- 云生产环境cloud mysql:
mysql> SELECT 'back_log' AS name, @@back_log AS 'value';
+----------+-------+
| name | value |
+----------+-------+
| back_log | 3000 |
+----------+-------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'back_log' AS name, @@back_log AS 'value';
+----------+-------+
| name | value |
+----------+-------+
| back_log | 80 |
+----------+-------+
1 row in set (0.00 sec)
-- 本地开发环境localhost
mysql> SELECT 'back_log' AS name, @@back_log AS 'value';
+----------+-------+
| name | value |
+----------+-------+
| back_log | 80 |
+----------+-------+
1 row in set
10. innodb_flush_log_at_trx_commit
innodb_flush_log_at_trx_commit = 0
事务日志写盘配置
N=0 每隔一秒,把事务日志缓存区的数据写到日志文件中,以及把日志文件的数据刷新到磁盘上;
N=1 每个事务提交时候,把事务日志从缓存区写到日志文件中,并且刷新日志文件的数据到磁盘上;
N=2 每事务提交的时候,把事务日志数据从缓存区写到日志文件中;
-- 云生产环境cloud mysql:
mysql> SELECT 'innodb_flush_log_at_trx_commit' AS name, @@innodb_flush_log_at_trx_commit AS 'value';
+--------------------------------+-------+
| name | value |
+--------------------------------+-------+
| innodb_flush_log_at_trx_commit | 1 |
+--------------------------------+-------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'innodb_flush_log_at_trx_commit' AS name, @@innodb_flush_log_at_trx_commit AS 'value';
+--------------------------------+-------+
| name | value |
+--------------------------------+-------+
| innodb_flush_log_at_trx_commit | 1 |
+--------------------------------+-------+
1 row in set (0.00 sec)
-- 本地开发环境localhost
mysql> SELECT 'innodb_flush_log_at_trx_commit' AS name, @@innodb_flush_log_at_trx_commit AS 'value';
+--------------------------------+-------+
| name | value |
+--------------------------------+-------+
| innodb_flush_log_at_trx_commit | 1 |
+--------------------------------+-------+
1 row in set
11. sync_binlog
sync_binlog = N:
N>0 每向二进制日志文件写入N条SQL或N个事务后,则把二进制日志文件的数据刷新到磁盘上;
N=0 不主动刷新二进制日志文件的数据到磁盘上,而是由操作系统决定;
-- 云生产环境cloud mysql:
mysql> SELECT 'sync_binlog' AS name, @@sync_binlog AS 'value';
+-------------+-------+
| name | value |
+-------------+-------+
| sync_binlog | 1 |
+-------------+-------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'sync_binlog' AS name, @@sync_binlog AS 'value';
+-------------+-------+
| name | value |
+-------------+-------+
| sync_binlog | 1 |
+-------------+-------+
1 row in set (0.00 sec)
12. innodb_buffer_pool_instances(*)
mysql innodb的性能与缓冲池的大小有很大的关系。可以通过以下两个参数进行调整。
innodb_buffer_pool_size 和 innodb_buffer_pool_instances
-- 云生产环境cloud mysql:
mysql> SELECT 'innodb_buffer_pool_instances' AS name, @@innodb_buffer_pool_instances AS 'value';
+------------------------------+-------+
| name | value |
+------------------------------+-------+
| innodb_buffer_pool_instances | 8 |
+------------------------------+-------+
1 row in set (0.00 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'innodb_buffer_pool_instances' AS name, @@innodb_buffer_pool_instances AS 'value';
+------------------------------+-------+
| name | value |
+------------------------------+-------+
| innodb_buffer_pool_instances | 1 |
+------------------------------+-------+
1 row in set (0.00 sec)
13. innodb_log_buffer_size
innodb_log_buffer_size = 2M
-- 云生产环境cloud mysql:
mysql> SELECT 'innodb_log_buffer_size' AS name, @@innodb_log_buffer_size/1024/1024 AS 'value(MB)';
+------------------------+------------+
| name | value(MB) |
+------------------------+------------+
| innodb_log_buffer_size | 8.00000000 |
+------------------------+------------+
1 row in set (0.01 sec)
-- 测试环境192.168.4.125:
mysql> SELECT 'innodb_log_buffer_size' AS name, @@innodb_log_buffer_size/1024/1024 AS 'value(MB)';
+------------------------+-------------+
| name | value(MB) |
+------------------------+-------------+
| innodb_log_buffer_size | 16.00000000 |
+------------------------+-------------+
1 row in set (0.00 sec)
14. tmp_table_size
tmp_table_size = 256M
-- 云生产环境cloud mysql:
mysql> SELECT 'tmp_table_size' AS name, @@tmp_table_size/1024/1024 AS 'value(MB)';
+----------------+------------+
| name | value(MB) |
+----------------+------------+
| tmp_table_size | 2.00000000 |
+----------------+------------+
1 row in set (0.01 sec)
-- 测试环境 192.168.4.125:
mysql> SELECT 'tmp_table_size' AS name, @@tmp_table_size/1024/1024 AS 'value(MB)';
+----------------+-------------+
| name | value(MB) |
+----------------+-------------+
| tmp_table_size | 16.00000000 |
+----------------+-------------+
1 row in set (0.00 sec)
15. 优化配置
为了能够让配置的参数能够在mysql重启后仍然有效,建议将优化参数配置在mysql的配置文件中,这样,每次mysql重启后,都能使用优化参数进行运行。
这里以8G内存服务器为例配置的优化参数。
innodb_buffer_pool_size = 4096MB
innodb_buffer_pool_instances = 8
max_connections = 1200
max_allowed_packet = 8M
back_log = 1000