MySQL kill不掉线程的原因


Posted in MySQL onMay 07, 2021

背景

在日常的使用过程中,时不时会遇到个别,或者大量的连接堆积在 MySQL 中的现象,这时一般会考虑使用 kill 命令强制杀死这些长时间堆积起来的连接,尽快释放连接数和数据库服务器的 CPU 资源。

问题描述

在实际操作 kill 命令的时候,有时候会发现连接并没有第一时间被 kill 掉,仍旧在 processlist 里面能看到,但是显示的 Command 为 Killed,而不是常见的 Query 或者是 Execute 等。例如:

mysql> show processlist;
+----+------+--------------------+--------+---------+------+--------------+---------------------------------+
| Id | User | Host               | db     | Command | Time | State        | Info                            |
+----+------+--------------------+--------+---------+------+--------------+---------------------------------+
| 31 | root | 192.168.1.10:50410 | sbtest | Query   |    0 | starting     | show processlist                |
| 32 | root | 192.168.1.10:50412 | sbtest | Query   |   62 | User sleep   | select sleep(3600) from sbtest1 |
| 35 | root | 192.168.1.10:51252 | sbtest | Killed  |   47 | Sending data | select sleep(100) from sbtest1  |
| 36 | root | 192.168.1.10:51304 | sbtest | Query   |   20 | Sending data | select sleep(3600) from sbtest1 |
+----+------+--------------------+--------+---------+------+--------------+---------------------------------+

原因分析

遇事不决先翻官方文档,这里摘取部分官方文档的内容:

When you use KILL, a thread-specific kill flag is set for the thread. In most cases, it might take some time for the thread to die because the kill flag is checked only at specific intervals:During SELECT operations, for ORDER BY and GROUP BY loops, the flag is checked after reading a block of rows. If the kill flag is set, the statement is aborted.
      ALTER TABLE operations that make a table copy check the kill flag periodically for each few copied rows read from the original table. If the kill flag was set, the statement is aborted and the temporary table is deleted.
      The KILL statement returns without waiting for confirmation, but the kill flag check aborts the operation within a reasonably small amount of time. Aborting the operation to perform any necessary cleanup also takes some time.
      During UPDATE or DELETE operations, the kill flag is checked after each block read and after each updated or deleted row. If the kill flag is set, the statement is aborted. If you are not using transactions, the changes are not rolled back.
      GET_LOCK() aborts and returns NULL.
      If the thread is in the table lock handler (state: Locked), the table lock is quickly aborted.
      If the thread is waiting for free disk space in a write call, the write is aborted with a “disk full” error message.

官方文档第一段就很明确的说清楚了 kill 的作用机制:会给连接的线程设置一个线程级别的 kill 标记,等到下一次“标记检测”的时候才会生效。这也意味着如果下一次“标记检测”迟迟没有发生,那么就有可能会出现问题描述中的现象。

官方文档中列举了不少的场景,这里根据官方的描述列举几个比较常见的问题场景:

  • select 语句中进行 order by,group by 的时候,如果服务器 CPU 资源比较紧张,那么读取/获取一批数据的时间会变长,从而影响下一次“标记检测”的时间。
  • 对大量数据进行 DML 操作的时候,kill 这一类 SQL 语句会触发事务回滚(InnoDB引擎),虽然语句被 kill 掉了,但是回滚操作也会非常久。
  • kill alter 操作时,如果服务器的负载比较高,那么操作一批数据的时间会变长,从而影响下一次“标记检测”的时间。
  • 其实参考 kill 的作用机制,做一个归纳性的描述的话,那么:任何阻塞/减慢 SQL 语句正常执行的行为,都会导致下一次“标记检测”推迟、无法发生,最终都会导致 kill 操作的失败。

模拟一下

这里借用一个参数innodb_thread_concurrency来模拟阻塞 SQL 语句正常执行的场景:

Defines the maximum number of threads permitted inside of InnoDB. A value of 0 (the default) is interpreted as infinite concurrency (no limit). This variable is intended for performance tuning on high concurrency systems.

参照官方文档的描述,这个参数设置得比较低的时候,超过数量限制的 InnoDB 查询会被阻塞。因此在本次模拟中,这个参数被设置了一个非常低的值。

mysql> show variables like '%innodb_thread_concurrency%';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| innodb_thread_concurrency | 1     |
+---------------------------+-------+
1 row in set (0.00 sec)

然后开两个数据库连接(Session 1 和 Session 2),分别执行select sleep(3600) from sbtest.sbtest1语句,然后在第三个连接上 kill 掉 Session 2 的查询:

Session 1:
mysql> select sleep(3600) from sbtest.sbtest1;

Session 2:
mysql> select sleep(3600) from sbtest.sbtest1;
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql>

Session 3:
mysql> show processlist;
+----+------+--------------------+------+---------+------+--------------+----------------------------------------+
| Id | User | Host               | db   | Command | Time | State        | Info                                   |
+----+------+--------------------+------+---------+------+--------------+----------------------------------------+
| 44 | root | 172.16.64.10:39290 | NULL | Query   |   17 | User sleep   | select sleep(3600) from sbtest.sbtest1 |
| 45 | root | 172.16.64.10:39292 | NULL | Query   |    0 | starting     | show processlist                       |
| 46 | root | 172.16.64.10:39294 | NULL | Query   |    5 | Sending data | select sleep(3600) from sbtest.sbtest1 |
+----+------+--------------------+------+---------+------+--------------+----------------------------------------+
3 rows in set (0.00 sec)

mysql> kill 46;
Query OK, 0 rows affected (0.00 sec)

mysql> show processlist;
+----+------+--------------------+------+---------+------+--------------+----------------------------------------+
| Id | User | Host               | db   | Command | Time | State        | Info                                   |
+----+------+--------------------+------+---------+------+--------------+----------------------------------------+
| 44 | root | 172.16.64.10:39290 | NULL | Query   |   26 | User sleep   | select sleep(3600) from sbtest.sbtest1 |
| 45 | root | 172.16.64.10:39292 | NULL | Query   |    0 | starting     | show processlist                       |
| 46 | root | 172.16.64.10:39294 | NULL | Killed  |   14 | Sending data | select sleep(3600) from sbtest.sbtest1 |
+----+------+--------------------+------+---------+------+--------------+----------------------------------------+
3 rows in set (0.00 sec)

mysql>

可以看到,kill 命令执行之后,Session 2 的连接马上就断开了,但是 Session 2 发起的查询仍旧残留在 MySQL 中。当然,如果是因为innodb_thread_concurrency这个参数导致了类似的问题的话,直接使用set global的命令调高上限,或者直接设置为 0 就可以解决,这个参数的变更是实时对所有连接生效的。

总结一下

MySQL 的 kill 操作并不是想象中的直接强行终止数据库连接,只是发送了一个终止的信号,如果 SQL 自身的执行效率过慢,或者受到其他的因素影响(服务器负载高,触发大量数据回滚)的话,那么这个 kill 的操作很有可能并不能及时终止这些问题查询,反而可能会因为程序侧连接被断开之后触发重连,产生更多的低效查询,进一步拖垮数据库。

以上就是MySQL kill不掉线程的原因的详细内容,更多关于MySQL kill线程的资料请关注三水点靠木其它相关文章!

MySQL 相关文章推荐
52条SQL语句教你性能优化
May 25 MySQL
zabbix监控mysql的实例方法
Jun 02 MySQL
mysql联合索引的使用规则
Jun 23 MySQL
MySQL命令无法输入中文问题的解决方式
Aug 30 MySQL
面试被问select......for update会锁表还是锁行
Nov 11 MySQL
Mysql事务索引知识汇总
Mar 17 MySQL
mysql 生成连续日期及变量赋值
Mar 20 MySQL
mysql insert 存在即不插入语法说明
Mar 25 MySQL
Mysql如何实现不存在则插入,存在则更新
Mar 25 MySQL
MySQL表锁、行锁、排它锁及共享锁的使用详解
Apr 02 MySQL
MySQL中EXPLAIN语句及用法
May 20 MySQL
MySQL自定义函数及触发器
Aug 05 MySQL
MySQL数字类型自增的坑
May 07 #MySQL
MySQL获取所有分类的前N条记录
May 07 #MySQL
教你解决往mysql数据库中存入汉字报错的方法
MySQL时间设置注意事项的深入总结
仅用一句SQL更新整张表的涨跌幅、涨跌率的解决方案
May 06 #MySQL
MySQL创建高性能索引的全步骤
将图片保存到mysql数据库并展示在前端页面的实现代码
You might like
PHP操作XML中XPath的应用示例
2019/07/04 PHP
Laravel相关的一些故障解决
2020/08/19 PHP
one.php 多项目、函数库、类库 统一为一个版本的方法
2020/08/24 PHP
javascript appendChild,innerHTML,join性能比较代码
2009/08/29 Javascript
jQuery UI AutoComplete 使用说明
2011/06/20 Javascript
json对象转字符串如何实现
2012/12/02 Javascript
jquery实现tr元素的上下移动示例代码
2013/12/20 Javascript
javascript判断变量是否有值的方法
2015/04/20 Javascript
javascript随机显示背景图片的方法
2015/06/18 Javascript
javascript实现拖动元素交换位置
2015/11/29 Javascript
多个js毫秒倒计时同时进行效果
2016/01/05 Javascript
简易的JS计算器实现代码
2016/10/18 Javascript
vue指令以及dom操作详解
2017/03/04 Javascript
nodejs中art-template模板语法的引入及冲突解决方案
2017/11/07 NodeJs
禁止弹窗中蒙层底部页面跟随滚动的几种方法
2017/12/07 Javascript
详解JavaScript 的变量
2019/03/08 Javascript
python中list循环语句用法实例
2014/11/10 Python
python回溯法实现数组全排列输出实例分析
2015/03/17 Python
python中常用检测字符串相关函数汇总
2015/04/15 Python
详解在Python程序中自定义异常的方法
2015/10/16 Python
python中的插值 scipy-interp的实现代码
2018/07/23 Python
Transpose 数组行列转置的限制方式
2020/02/11 Python
美国韩国化妆品和护肤品购物网站:Beautytap
2018/07/29 全球购物
美容师的职业规划书
2013/12/27 职场文书
小学红领巾中秋节广播稿
2014/01/13 职场文书
家长给孩子的表扬信
2014/01/17 职场文书
2014新课程改革心得体会
2014/03/10 职场文书
党员干部廉洁承诺书
2014/05/28 职场文书
商业街策划方案
2014/05/31 职场文书
教师三严三实对照检查材料
2014/09/25 职场文书
2014县政府领导班子对照检查材料思想汇报
2014/09/25 职场文书
学校领导四风问题整改措施思想汇报
2014/10/09 职场文书
2014年爱国卫生工作总结
2014/11/22 职场文书
房产公证书
2015/01/23 职场文书
离婚起诉书范文2015
2015/05/19 职场文书
年中了,该如何写好个人述职报告?
2019/07/02 职场文书