现象:在mysql运行一天之后,发现内存占用量达到总内存的百分之77,且swap被占用百分之50
问题检查如下:
第一步:开启内存使用的监控服务
mysql> UPDATE performance_schema.setup_instruments SET ENABLED = 'YES' WHERE NAME LIKE 'memory/%';
禁用方法:
mysql> UPDATE performance_schema.setup_instruments SET ENABLED = 'NO' WHERE NAME LIKE 'memory/%';
查看方法:
mysql> select * from performance_schema.setup_instruments where NAME like 'memory%' and NAME not like 'memory/performance_schema%';
mysql> select * from performance_schema.setup_instruments where NAME like 'memory%';
第二步: 查看正常实例的使用情况
mysql> select USER,HOST,EVENT_NAME,COUNT_ALLOC,COUNT_FREE,CURRENT_COUNT_USED,SUM_NUMBER_OF_BYTES_ALLOC,SUM_NUMBER_OF_BYTES_FREE,CURRENT_NUMBER_OF_BYTES_USED from performance_schema.memory_summary_by_account_by_event_name order by CURRENT_NUMBER_OF_BYTES_USED desc limit 10;
COUNT_ALLOC:内存分配次数
COUNT_FREE:内存回收次数
SUM_NUMBER_OF_BYTES_ALLOC:内存分配大小
SUM_NUMBER_OF_BYTES_FREE:内存回收大小
CURRENT_COUNT_USED:当前分配的内存,通过COUNT_ALLOC-COUNT_FREE计算得到
CURRENT_NUMBER_OF_BYTES_USED:当前分配的内存大小,通过SUM_NUMBER_OF_BYTES_ALLOC-SUM_NUMBER_OF_BYTES_FREE计算得到
LOW_COUNT_USED:CURRENT_COUNT_USED的最小值
HIGH_COUNT_USED:CURRENT_COUNT_USED的最大值
LOW_NUMBER_OF_BYTES_USED:CURRENT_NUMBER_OF_BYTES_USED的最小值
HIGH_NUMBER_OF_BYTES_USED:CURRENT_NUMBER_OF_BYTES_USED的最大值
第三步: 查看内存监控表数据
select event_name, current_alloc, high_alloc from sys.memory_global_by_current_bytes where current_count > 0;
第三步: 查看事件类型下的内存量
mysql> select substring_index(
-> substring_index(event_name, '/', 2),
-> '/',
-> -1
-> ) as event_type,
-> round(sum(CURRENT_NUMBER_OF_BYTES_USED)/1024/1024, 2) as MB_CURRENTLY_USED
-> from performance_schema.memory_summary_global_by_event_name
-> group by event_type
问题解决:
当事件的内存占用是performance_schema过高的话,可以考虑关闭或者减少该事件使用内存的量,操作如下
降低performance_schema的内存使用率:
performance_schema_max_table_instances=400
table_definition_cache=400
table_open_cache=256
关闭performance_schema:
performance_schema=off
相关帮助链接:
- MySQL最佳实践:内存管理分析
- mysql开启缓存、设置缓存大小、缓存过期机制
- 记一次mysql内存占用过大-不释放问题排查
- 排除故障指南:MySQL运行内存不足时应采取的措施?