- mysql版本:5.7.27
- mysql业务:因业务需要,每天需生成大量的表
一,查询系统日志得知mysql因OOM被kill了
1.查询硬盘上永久保存的系统日志
- journalctl命令: journalctl -k | grep -i "oom|kill|error"
- 可加时间参数: journalctl -k --since "2026-04-09 00:00" --until "2026-04-10 10:00" | grep -i "oom|kill|error"
查询结果:
bash
[root@db ~]# journalctl -k | grep -i "oom\|kill\|error"
12月 21 11:45:38 localhost.localdomain kernel: HEST: Enabling Firmware First mode for corrected errors.
12月 21 11:45:38 localhost.localdomain kernel: ERST: Error Record Serialization Table (ERST) support is initialized.
4月 09 22:43:08 27 kernel: mysqld invoked oom-killer: gfp_mask=0x201da, order=0, oom_score_adj=0
4月 09 22:43:08 27 kernel: [<ffffffffb7fba274>] oom_kill_process+0x254/0x3d0
4月 09 22:43:08 27 kernel: [ pid ] uid tgid total_vm rss nr_ptes swapents oom_score_adj name
4月 09 22:43:08 27 kernel: [22186] 0 22186 130450 0 40 192 0 gsd-rfkill
4月 09 22:43:08 27 kernel: Out of memory: Kill process 358400 (mysqld) score 565 or sacrifice child
4月 09 22:43:08 27 kernel: Killed process 358400 (mysqld) total-vm:80464500kB, anon-rss:76302072kB, file-rss:0kB, shmem-rss:0kB
[root@db ~]#
二,查看内存情况
- 共125G,只剩余19G了,已经用了 104G,内存压力非常大。Swap 已经满了(4G 全用光),说明系统之前内存严重不够用,开始频繁用交换分区,机器大概率会很卡。
bash
[root@db ~]# free -h
total used free shared buff/cache available
Mem: 125G 104G 632M 42M 20G 19G
Swap: 4.0G 4.0G 0B
[root@db ~]#
[root@db ~]# free -mt
total used free shared buff/cache available
Mem: 128419 107275 630 42 20513 19963
Swap: 4095 4095 0
Total: 132515 111371 630
[root@db ~]#
- 排查104G都是谁在用:ps aux --sort=-%mem | head -20
可以看到有两个mysql的实例占比很大
bash
[root@db ~]# ps aux --sort=-%mem | head -20
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
mysql 358200 6.3 44.2 62294972 58240152 ? Ssl 2024 75570:49 /home/mysql/mysql4001/bin/mysqld --defaults-file=/home/mysql/mysql4001/etc/my4001.cnf
mysql 438278 39.7 34.4 47782108 45246208 ? Ssl 4月10 3930:05 /home/mysql/mysql4002/bin/mysqld --defaults-file=/home/mysql/mysql4002/etc/my4002.cnf
mysql 358916 0.5 1.1 3068732 1558772 ? Ssl 2024 5973:52 /home/mysql/mysql4005/bin/mysqld --defaults-file=/home/mysql/mysql4005/etc/my4005.cnf
mysql 358563 18.6 0.5 2928924 733824 ? Ssl 2024 220969:14 /home/mysql/mysql4003/bin/mysqld --defaults-file=/home/mysql/mysql4003/etc/my4003.cnf
mysql 358753 0.0 0.4 2928924 558060 ? Ssl 2024 1003:10 /home/mysql/mysql4004/bin/mysqld --defaults-file=/home/mysql/mysql4004/etc/my4004.cnf
root 22229 2.9 0.1 896844 221296 ? Sl 2023 35667:54 /usr/libexec/gsd-color
root 22314 0.0 0.1 1551592 197708 ? Sl 2023 35:15 /usr/bin/gnome-software --gapplication-service
root 21991 0.0 0.1 7978324 138220 ? Sl 2023 245:57 /usr/bin/gnome-shell
root 22223 0.0 0.0 1257612 50020 ? S<l 2023 2:43 /usr/bin/pulseaudio --start
root 9665 0.0 0.0 80760 39028 ? Ss 2023 6:05 /usr/lib/systemd/systemd-journald
root 23957 0.0 0.0 742040 27724 ? Ssl 2023 56:16 /usr/sbin/rsyslogd -n
zabbix 438476 1.1 0.0 3222388 20768 ? Ssl 4月10 109:28 /usr/sbin/zabbix_agent2 -c /etc/zabbix/zabbix_agent2.conf
root 19201 0.0 0.0 489016 15316 tty1 Ssl+ 2023 5:17 /usr/bin/X :0 -background none -noreset -audit 4 -verbose -auth /run/gdm/auth-for-gdm-RFUQyH/database -seat seat0 vt1
root 114164 0.0 0.0 160844 5792 ? Ss 13:55 0:00 sshd: root@pts/0
root 18169 0.0 0.0 120132 5500 ? S 2023 419:12 /bin/bash /usr/sbin/ksmtuned
root 22354 0.0 0.0 591484 5364 ? Sl 2023 0:03 /usr/libexec/tracker-store
root 1 0.0 0.0 197652 4624 ? Ss 2023 31:25 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 19497 0.0 0.0 487764 4528 ? Ssl 2023 11:27 /usr/libexec/packagekitd
root 22236 0.0 0.0 1143196 3396 ? Sl 2023 2:26 /usr/libexec/gsd-media-keys
3.排查mysql实例内存大的原因
- 查看InnoDB 缓冲池大小:show variables like 'innodb_buffer_pool_size';
- 查看InnoDB 缓冲池切成几个小池:show variables like 'innodb_buffer_pool_instances';
- 查看当前内存实际使用:sql查询tables信息
- 查看缓冲池使用率(判断是否真的需要这么大):SHOW ENGINE INNODB STATUS;
bash
MySQL [(none)]> show variables like 'innodb_buffer_pool_size';
+-------------------------+------------+
| Variable_name | Value |
+-------------------------+------------+
| innodb_buffer_pool_size | 4294967296 |
+-------------------------+------------+
1 row in set (0.18 sec)
MySQL [(none)]> show variables like 'innodb_buffer_pool_instances';
+------------------------------+-------+
| Variable_name | Value |
+------------------------------+-------+
| innodb_buffer_pool_instances | 8 |
+------------------------------+-------+
1 row in set (0.00 sec)
MySQL [(none)]> SELECT
-> engine,
-> COUNT(*) AS tables,
-> SUM(data_length) / 1024 / 1024 / 1024 AS data_size_gb,
-> SUM(index_length) / 1024 / 1024 / 1024 AS index_size_gb,
-> (SUM(data_length) + SUM(index_length)) / 1024 / 1024 / 1024 AS total_size_gb
-> FROM information_schema.tables
-> WHERE table_schema NOT IN ('mysql','sys','information_schema','performance_schema')
-> GROUP BY engine;
+--------+--------+-------------------+------------------+-------------------+
| engine | tables | data_size_gb | index_size_gb | total_size_gb |
+--------+--------+-------------------+------------------+-------------------+
| InnoDB | 211469 | 7853.475524902344 | 184.029800415039 | 8037.505325317383 |
+--------+--------+-------------------+------------------+-------------------+
1 row in set (30 min 20.96 sec)
MySQL [(none)]> SHOW ENGINE INNODB STATUS;
+--------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Type | Name | Status |
+--------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| InnoDB | |
=====================================
2026-04-17 11:57:57 0x7f766c21f700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 27 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 567449 srv_active, 0 srv_shutdown, 1124 srv_idle
srv_master_thread log flush and writes: 568573
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 83450578
OS WAIT ARRAY INFO: signal count 51958414
RW-shared spins 0, rounds 1175431, OS waits 271863
RW-excl spins 0, rounds 33204095, OS waits 900821
RW-sx spins 48467, rounds 1400486, OS waits 44214
Spin rounds per wait: 1175431.00 RW-shared, 33204095.00 RW-excl, 28.90 RW-sx
------------
TRANSACTIONS
------------
Trx id counter 8298046688
Purge done for trx's n:o < 8298039869 undo n:o < 0 state: running but idle
History list length 48
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421626915763152, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421626915755856, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421626915762240, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421626915761328, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421626915760416, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421626915758592, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421626915757680, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421626915759504, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421626915756768, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests (read thread)
I/O thread 4 state: waiting for completed aio requests (read thread)
I/O thread 5 state: waiting for completed aio requests (read thread)
I/O thread 6 state: complete io for buf page (write thread)
I/O thread 7 state: waiting for completed aio requests (write thread)
I/O thread 8 state: waiting for completed aio requests (write thread)
I/O thread 9 state: waiting for completed aio requests (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 1
1839346 OS file reads, 45639629 OS file writes, 25692445 OS fsyncs
0.04 reads/s, 16384 avg bytes/read, 73.70 writes/s, 44.48 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 1182691, node heap has 1 buffer(s)
Hash table size 1182691, node heap has 1 buffer(s)
Hash table size 1182691, node heap has 1 buffer(s)
Hash table size 1182691, node heap has 3 buffer(s)
Hash table size 1182691, node heap has 6 buffer(s)
Hash table size 1182691, node heap has 1 buffer(s)
Hash table size 1182691, node heap has 2 buffer(s)
Hash table size 1182691, node heap has 1 buffer(s)
309.43 hash searches/s, 94.89 non-hash searches/s
---
LOG
---
Log sequence number 10160844592918
Log flushed up to 10160844554224
Pages flushed up to 10160835836712
Last checkpoint at 10160833904291
0 pending log flushes, 0 pending chkp writes
2090588 log i/o's done, 3.89 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 4397727744
Dictionary memory allocated 17237151
Buffer pool size 262112
Free buffers 8191
Database pages 253905
Old database pages 93563
Modified db pages 1282
Pending reads 0
Pending writes: LRU 0, flush list 2, single page 0
Pages made young 2437456, not young 12694603
0.00 youngs/s, 0.00 non-youngs/s
Pages read 1423368, created 12127375, written 36978151
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 253905, unzip_LRU len: 0
I/O sum[24328]:cur[395], unzip sum[0]:cur[0]
----------------------
INDIVIDUAL BUFFER POOL INFO
----------------------
---BUFFER POOL 0
Buffer pool size 32764
Free buffers 1024
Database pages 31738
Old database pages 11695
Modified db pages 140
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 303218, not young 1522120
0.00 youngs/s, 0.00 non-youngs/s
Pages read 177929, created 1518448, written 4992441
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 31738, unzip_LRU len: 0
I/O sum[3041]:cur[49], unzip sum[0]:cur[0]
---BUFFER POOL 1
Buffer pool size 32764
Free buffers 1023
Database pages 31736
Old database pages 11695
Modified db pages 198
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 330495, not young 1711760
0.00 youngs/s, 0.00 non-youngs/s
Pages read 178787, created 1524171, written 4563514
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 31736, unzip_LRU len: 0
I/O sum[3041]:cur[49], unzip sum[0]:cur[0]
---BUFFER POOL 2
Buffer pool size 32764
Free buffers 1024
Database pages 31737
Old database pages 11695
Modified db pages 165
Pending reads 0
Pending writes: LRU 0, flush list 2, single page 0
Pages made young 324339, not young 1683372
0.00 youngs/s, 0.00 non-youngs/s
Pages read 178189, created 1518713, written 4287636
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 31737, unzip_LRU len: 0
I/O sum[3041]:cur[49], unzip sum[0]:cur[0]
---BUFFER POOL 3
Buffer pool size 32764
Free buffers 1024
Database pages 31739
Old database pages 11696
Modified db pages 195
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 296753, not young 1508495
0.00 youngs/s, 0.00 non-youngs/s
Pages read 176588, created 1516645, written 4523849
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 31739, unzip_LRU len: 0
I/O sum[3041]:cur[49], unzip sum[0]:cur[0]
---BUFFER POOL 4
Buffer pool size 32764
Free buffers 1024
Database pages 31739
Old database pages 11696
Modified db pages 157
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 282041, not young 1639408
0.00 youngs/s, 0.00 non-youngs/s
Pages read 176711, created 1508214, written 4766030
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 31739, unzip_LRU len: 0
I/O sum[3041]:cur[49], unzip sum[0]:cur[0]
---BUFFER POOL 5
Buffer pool size 32764
Free buffers 1024
Database pages 31738
Old database pages 11695
Modified db pages 190
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 283485, not young 1605397
0.00 youngs/s, 0.00 non-youngs/s
Pages read 178423, created 1513823, written 4887543
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 31738, unzip_LRU len: 0
I/O sum[3041]:cur[50], unzip sum[0]:cur[0]
---BUFFER POOL 6
Buffer pool size 32764
Free buffers 1024
Database pages 31740
Old database pages 11696
Modified db pages 115
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 273763, not young 1592887
0.00 youngs/s, 0.00 non-youngs/s
Pages read 178941, created 1510240, written 4480946
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 31740, unzip_LRU len: 0
I/O sum[3041]:cur[50], unzip sum[0]:cur[0]
---BUFFER POOL 7
Buffer pool size 32764
Free buffers 1024
Database pages 31738
Old database pages 11695
Modified db pages 122
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 343362, not young 1431164
0.00 youngs/s, 0.00 non-youngs/s
Pages read 177800, created 1517121, written 4476192
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 31738, unzip_LRU len: 0
I/O sum[3041]:cur[50], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=438278, Main thread ID=140146741806848, state: sleeping
Number of rows inserted 281036613, updated 0, deleted 0, read 170516033
184.22 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
|
+--------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.24 sec)
结果
- innodb_buffer_pool_size = 4GB
- 物理内存占用:43.1G
- 业务:疯狂 INSERT 日志表,单库 21 万 + 张表
- 运行时长:几十天(因oom被kill后重启过)
- 验证Open_tables情况
bash
#4001实例:
MySQL [(none)]> show global status like 'Open_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 1984 |
+---------------+-------+
1 row in set (0.00 sec)
MySQL [(none)]> show global status like 'Opened_tables';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| Opened_tables | 1154279575 |
+---------------+------------+
1 row in set (0.01 sec)
#4002实例:
MySQL [(none)]> show global status like 'Open_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 1984 |
+---------------+-------+
1 row in set (0.03 sec)
MySQL [(none)]> show global status like 'Opened_tables';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| Opened_tables | 188864678 |
+---------------+-----------+
1 row in set (0.01 sec)
5.进入mysql的performance_schema库查询相关信息
5.1 查询MySQL里谁消耗的内存:
select event_name,SUM_NUMBER_OF_BYTES_ALLOC
from memory_summary_global_by_event_name
order by SUM_NUMBER_OF_BYTES_ALLOC desc LIMIT 10;
bash
MySQL [performance_schema]> select event_name,SUM_NUMBER_OF_BYTES_ALLOC
-> from memory_summary_global_by_event_name
-> order by SUM_NUMBER_OF_BYTES_ALLOC desc LIMIT 10;
+----------------------------------------------------------------------+---------------------------+
| event_name | SUM_NUMBER_OF_BYTES_ALLOC |
+----------------------------------------------------------------------+---------------------------+
| memory/performance_schema/file_instances | 299892736 |
| memory/performance_schema/table_shares | 218103808 |
| memory/performance_schema/table_handles | 171048960 |
| memory/performance_schema/table_io_waits_summary_by_index_usage | 149946368 |
| memory/performance_schema/rwlock_instances | 83230720 |
| memory/performance_schema/mutex_instances | 20316160 |
| memory/performance_schema/events_statements_history_long | 14320000 |
| memory/performance_schema/events_statements_history_long.tokens | 10240000 |
| memory/performance_schema/events_statements_summary_by_digest.tokens | 10240000 |
| memory/performance_schema/events_statements_history_long.sqltext | 10240000 |
+----------------------------------------------------------------------+---------------------------+
10 rows in set (0.20 sec)
5.2 哪些内部线程消耗了内存
select event_name, SUM_NUMBER_OF_BYTES_ALLOC
from memory_summary_by_thread_by_event_name
order by SUM_NUMBER_OF_BYTES_ALLOC desc limit 20;
bash
MySQL [performance_schema]> select event_name, SUM_NUMBER_OF_BYTES_ALLOC
-> from memory_summary_by_thread_by_event_name
-> order by SUM_NUMBER_OF_BYTES_ALLOC desc limit 20;
+----------------------------------------------+---------------------------+
| event_name | SUM_NUMBER_OF_BYTES_ALLOC |
+----------------------------------------------+---------------------------+
| memory/archive/record_buffer | 0 |
| memory/sql/QUICK_RANGE_SELECT::alloc | 0 |
| memory/sql/table_mapping::m_mem_root | 0 |
| memory/sql/sp_head::call_mem_root | 0 |
| memory/sql/sp_head::execute_mem_root | 0 |
| memory/sql/sp_head::main_mem_root | 0 |
| memory/sql/THD::sp_cache | 0 |
| memory/sql/Warning_info::m_warn_root | 0 |
| memory/sql/Protocol_local::m_rset_root | 0 |
| memory/sql/Prepared_statement::main_mem_root | 0 |
| memory/sql/Prepared_statement_map | 0 |
| memory/sql/servers | 0 |
| memory/sql/Table_triggers_list | 0 |
| memory/sql/gdl | 0 |
| memory/sql/new_frm_mem | 0 |
| memory/sql/help | 0 |
| memory/sql/thd::main_mem_root | 0 |
| memory/sql/Delegate::memroot | 0 |
| memory/sql/THD::transactions::mem_root | 0 |
| memory/sql/display_table_locks | 0 |
+----------------------------------------------+---------------------------+
20 rows in set (0.05 sec)
- table_shares:表结构缓存,218M
- table_handles:表打开句柄,171M
- file_instances:表文件句柄,299M
这三个加起来 ≈ 700M,但这只是 performance_schema 统计的,真实泄漏的是:底层 C++ 层内存,不进 performance_schema!
三, 结论:MySQL 5.7 在高频打开新表场景下的内存泄漏,导致内存只增不减,最终占满 100G
-
结论
两个 MySQL 内存暴涨 98G 的真正原因:
MySQL 5.7 高并发 + 高频创建新表 + 表结构频繁刷新 → 内存泄漏(table cache 层)
不是表多、不是 buffer pool、不是配置,就是官方 BUG 导致的内存泄漏! -
原因:
看Open_tables情况
bash#4001 Open_tables: 1984 → 当前打开的表(正常,很小) Opened_tables: 11亿+ → 历史累计打开过的表(天文数字!) #4002 Open_tables: 1984 → 当前打开的表(正常) Opened_tables: 1.8亿+ → 历史累计打开过的表(巨大!)
-
2.1 Open_tables = 1984
- MySQL 当前只打开了 1984 张表
- 表缓存没有爆
- 不是表太多占内存
-
2.2 Opened_tables = 11 亿 / 1.8 亿
- 业务不断创建新表(每天按设备建表)
- 不断打开 → 关闭 → 打开 → 关闭
- MySQL 5.7 在高频打开关闭表时,存在无法回收的内存泄漏
-
2.3 泄漏的是什么内存?
- 表定义(frm)、字典缓存、临时对象、metadata 锁资源
- 这些内存只分配不释放
- 运行越久 → 泄漏越多 → 内存涨到几十 G
InnoDB 本身只用了一点点内存,其余全是上层泄漏。Opened_tables 高达 1.8亿 ~ 11亿,只有高频创表、高频删表、高频开表才会出现这种值。 业务模式:每天为每个设备创建一张新表 → 极高频率打开新表 → 触发 MySQL 5.7 内存泄漏。这是 MySQL 5.7 官方公开的经典泄漏场景。
四,方案
- 调整业务,清理不用的表
- 升级 MySQL 版本:5.7.39 及以上
- 调整 my.cnf(降低泄漏速度)
bash
table_open_cache = 1024
table_definition_cache = 1024
max_connections = 500