随便找了一个从库,看现在的堵塞的预处理语句有多少条mysql> show global status like '%stmt%';
Connection id: 14674786
Current database: *** NONE ***
+----------------------------+----------+
| Variable_name | Value |
+----------------------------+----------+
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 37502963 |
| Com_stmt_close | 14701947 |
| Com_stmt_execute | 14724848 |
| Com_stmt_fetch | 0 |
| Com_stmt_prepare | 14724856 |
| Com_stmt_reprepare | 0 |
| Com_stmt_reset | 0 |
| Com_stmt_send_long_data | 0 |
| Prepared_stmt_count | 0 |
+----------------------------+----------+
10 rows in set (0.00 sec)
用Com_stmt_prepare-Com_stmt_close发现有22909个语句堵着了,远远大于了 MySQL 配置的max_prepared_stmt_countmysql> show global variables like '%stmt%';
Connection id: 14674955
Current database: *** NONE ***
+----------------------------+----------------------+
| Variable_name | Value |
+----------------------------+----------------------+
| binlog_stmt_cache_size | 32768 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| max_prepared_stmt_count | 16382 |
+----------------------------+----------------------+
3 rows in set (0.00 sec)
最快速的办法就是将max_prepared_stmt_count数字加大,然后再去找代码中忘记关闭数据库连接的地方,或者其他查询时间过长的语句。
在 java 代码里看到我忘记关闭了结果集。之前只关闭了Statement和Connection,没有关闭ResultSet。都给补上了。
还有可能是某些请求非常耗时,导致查询语句一直没有关闭,比如之前的 http://mengkang.net/474.html