Documentation Home
MySQL 8.3 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.8Mb
PDF (A4) - 40.9Mb
Man Pages (TGZ) - 294.0Kb
Man Pages (Zip) - 409.0Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb
Excerpts from this Manual

MySQL 8.3 Reference Manual  /  ...  /  Monitoring MySQL Memory Usage

10.12.3.2 监控 MySQL 内存使用情况

以下示例演示如何使用 性能架构sys 架构 监控 MySQL 内存使用情况。

大多数性能架构内存仪表默认情况下是禁用的。仪表可以通过更新性能架构 setup_instruments 表的 ENABLED 列来启用。内存仪表的名称以 memory/code_area/instrument_name 形式命名,其中 code_area 是一个值,例如 sqlinnodbinstrument_name 是仪表详细信息。

  1. 要查看可用的 MySQL 内存仪表,请查询性能架构 setup_instruments 表。以下查询将返回所有代码区域的数百个内存仪表。

    mysql> SELECT * FROM performance_schema.setup_instruments
           WHERE NAME LIKE '%memory%';

    您可以通过指定代码区域来缩小结果。例如,您可以将结果限制为 InnoDB 内存仪表,方法是指定 innodb 作为代码区域。

    mysql> SELECT * FROM performance_schema.setup_instruments
           WHERE NAME LIKE '%memory/innodb%';
    +-------------------------------------------+---------+-------+
    | NAME                                      | ENABLED | TIMED |
    +-------------------------------------------+---------+-------+
    | memory/innodb/adaptive hash index         | NO      | NO    |
    | memory/innodb/buf_buf_pool                | NO      | NO    |
    | memory/innodb/dict_stats_bg_recalc_pool_t | NO      | NO    |
    | memory/innodb/dict_stats_index_map_t      | NO      | NO    |
    | memory/innodb/dict_stats_n_diff_on_level  | NO      | NO    |
    | memory/innodb/other                       | NO      | NO    |
    | memory/innodb/row_log_buf                 | NO      | NO    |
    | memory/innodb/row_merge_sort              | NO      | NO    |
    | memory/innodb/std                         | NO      | NO    |
    | memory/innodb/trx_sys_t::rw_trx_ids       | NO      | NO    |
    ...

    根据您的 MySQL 安装,代码区域可能包括 performance_schemasqlclientinnodbmyisamcsvmemoryblackholearchivepartition 等。

  2. 要启用内存仪表,请将 performance-schema-instrument 规则添加到您的 MySQL 配置文件中。例如,要启用所有内存仪表,请将以下规则添加到配置文件中,然后重新启动服务器:

    performance-schema-instrument='memory/%=COUNTED'
    Note

    在启动时启用内存仪表,以确保在启动时的内存分配被计数。

    重新启动服务器后,性能架构 setup_instruments 表的 ENABLED 列应该报告您启用的内存仪表为 YES。性能架构 setup_instruments 表的 TIMED 列将被忽略,因为内存操作不计时。

    mysql> SELECT * FROM performance_schema.setup_instruments
           WHERE NAME LIKE '%memory/innodb%';
    +-------------------------------------------+---------+-------+
    | NAME                                      | ENABLED | TIMED |
    +-------------------------------------------+---------+-------+
    | memory/innodb/adaptive hash index         | NO      | NO    |
    | memory/innodb/buf_buf_pool                | NO      | NO    |
    | memory/innodb/dict_stats_bg_recalc_pool_t | NO      | NO    |
    | memory/innodb/dict_stats_index_map_t      | NO      | NO    |
    | memory/innodb/dict_stats_n_diff_on_level  | NO      | NO    |
    | memory/innodb/other                       | NO      | NO    |
    | memory/innodb/row_log_buf                 | NO      | NO    |
    | memory/innodb/row_merge_sort              | NO      | NO    |
    | memory/innodb/std                         | NO      | NO    |
    | memory/innodb/trx_sys_t::rw_trx_ids       | NO      | NO    |
    ...
  3. 查询内存仪表数据。在这个示例中,内存仪表数据被查询在性能架构 memory_summary_global_by_event_name 表中,该表按 EVENT_NAME 汇总数据。EVENT_NAME 是仪表的名称。

    以下查询返回 InnoDB 缓冲池的内存数据。有关列描述,请参阅 第 29.12.20.10 节,“内存摘要表”

    mysql> SELECT * FROM performance_schema.memory_summary_global_by_event_name
           WHERE EVENT_NAME LIKE 'memory/innodb/buf_buf_pool'\G
                      EVENT_NAME: memory/innodb/buf_buf_pool
                     COUNT_ALLOC: 1
                      COUNT_FREE: 0
       SUM_NUMBER_OF_BYTES_ALLOC: 137428992
        SUM_NUMBER_OF_BYTES_FREE: 0
                  LOW_COUNT_USED: 0
              CURRENT_COUNT_USED: 1
                 HIGH_COUNT_USED: 1
        LOW_NUMBER_OF_BYTES_USED: 0
    CURRENT_NUMBER_OF_BYTES_USED: 137428992
       HIGH_NUMBER_OF_BYTES_USED: 137428992

    同样的基础数据可以使用 sys 架构 memory_global_by_current_bytes 表查询,该表显示服务器全局当前内存使用情况,按分配类型划分。

    mysql> SELECT * FROM sys.memory_global_by_current_bytes
           WHERE event_name LIKE 'memory/innodb/buf_buf_pool'\G
    *************************** 1. row ***************************
           event_name: memory/innodb/buf_buf_pool
        current_count: 1
        current_alloc: 131.06 MiB
    current_avg_alloc: 131.06 MiB
           high_count: 1
           high_alloc: 131.06 MiB
       high_avg_alloc: 131.06 MiB

    这个 sys 架构查询聚合当前分配的内存 (current_alloc) 按代码区域:

    mysql> SELECT SUBSTRING_INDEX(event_name,'/',2) AS
           code_area, FORMAT_BYTES(SUM(current_alloc))
           AS current_alloc
           FROM sys.x$memory_global_by_current_bytes
           GROUP BY SUBSTRING_INDEX(event_name,'/',2)
           ORDER BY SUM(current_alloc) DESC;
    +---------------------------+---------------+
    | code_area                 | current_alloc |
    +---------------------------+---------------+
    | memory/innodb             | 843.24 MiB    |
    | memory/performance_schema | 81.29 MiB     |
    | memory/mysys              | 8.20 MiB      |
    | memory/sql                | 2.47 MiB      |
    | memory/memory             | 174.01 KiB    |
    | memory/myisam             | 46.53 KiB     |
    | memory/blackhole          | 512 bytes     |
    | memory/federated          | 512 bytes     |
    | memory/csv                | 512 bytes     |
    | memory/vio                | 496 bytes     |
    +---------------------------+---------------+

    有关 sys 架构的更多信息,请参阅 第 30 章,《MySQL sys 架构》