MySQL 5.7 memory_global_total view 内存使用不真实

有时做数据库监控像内存使用率,希望可以从数据库中使用SQL更加方便,在MySQL及同系的国产数据库(如GoldenDB)中同样有视图memory_global_total和sys.memory_global_by_current_bytes、memory_by_user_by_current_bytes, 但是在早期的MySQL 5.7存在bug 可能会导致查询视图与ps、top操作系统级看到的相差很多。

如下

select * from memory_global_total limit 10;
+-----------------+
| total_allocated |
+-----------------+
| 15.18 GiB       |
+-----------------+

select * from sys.memory_global_by_current_bytes limit 10;
+-----------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
| event_name                              | current_count | current_alloc | current_avg_alloc | high_count | high_alloc | high_avg_alloc |
+-----------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
| memory/innodb/buf_buf_pool              |            96 | 12.29 GiB     | 131.06 MiB        |         96 | 12.29 GiB  | 131.06 MiB     |
| memory/sql/TABLE                        |        186846 | 1.32 GiB      | 7.41 KiB          |     198458 | 1.61 GiB   | 8.48 KiB       |
| memory/innodb/hash0hash                 |            76 | 359.58 MiB    | 4.73 MiB          |         76 | 554.10 MiB | 7.29 MiB       |
| memory/innodb/os0event                  |       2407854 | 312.30 MiB    | 136 bytes         |    2407874 | 312.30 MiB | 136 bytes      |
| memory/innodb/mem0mem                   |         72421 | 288.40 MiB    | 4.08 KiB          |     127484 | 726.76 MiB | 5.84 KiB       |
| memory/performance_schema/table_handles |            17 | 154.06 MiB    | 9.06 MiB          |         17 | 154.06 MiB | 9.06 MiB       |
| memory/sql/Query_cache                  |             3 | 64.02 MiB     | 21.34 MiB         |          3 | 64.02 MiB  | 21.34 MiB      |
| memory/innodb/log0log                   |            11 | 64.01 MiB     | 5.82 MiB          |         11 | 64.01 MiB  | 5.82 MiB       |
| memory/innodb/ha_innodb                 |          9420 | 62.78 MiB     | 6.82 KiB          |      18818 | 144.27 MiB | 7.85 KiB       |
| memory/sql/TABLE_SHARE::mem_root        |         16940 | 55.81 MiB     | 3.37 KiB          |      16940 | 55.81 MiB  | 3.37 KiB       |
+-----------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+

$ ps -ef|grep mysql

The mem Mysqld exactly use is :
375302 mysql     20   0 36.968g 0.021t   7704 S   0.3 17.4 292:43.39 mysqld 

原因

MySQL 8.0 之前版本默认只开启了performance_schema相关内存时间监控项。其他内存监控项默认都未启用

解决方法

如需启用需要在配置文件中显示指定,可以通过如下方案启动所有内存监控项:

在配置文件中添加

performance-schema-instrument='memory/%=ON' 

并重启实例

在MySQL 8.0中该问题较少见,同时像国产GoldenDB验证数据也相对准确

注:5.7开启performance监控项会有一定的性能损耗

Leave a Comment