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 相关文章推荐
mysql优化
Apr 06 MySQL
MySQL之PXC集群搭建的方法步骤
May 25 MySQL
解决mysql问题:由于找不到MSVCR120.dll,无法继续执行代码
Jun 26 MySQL
Mysql8.0递归查询的简单用法示例
Aug 04 MySQL
MySQL数据库必备之条件查询语句
Oct 15 MySQL
mysql sum(if())和count(if())的用法说明
Jan 18 MySQL
Mysql存储过程、触发器、事件调度器使用入门指南
Jan 22 MySQL
MySQL RC事务隔离的实现
Mar 31 MySQL
MySQL 数据库范式化设计理论
Apr 22 MySQL
讲解MySQL增删改操作
May 06 MySQL
MySQL中order by的执行过程
Jun 05 MySQL
MySQL详解进行JDBC编程与增删改查方法
Jun 16 MySQL
MySQL数字类型自增的坑
May 07 #MySQL
MySQL获取所有分类的前N条记录
May 07 #MySQL
教你解决往mysql数据库中存入汉字报错的方法
MySQL时间设置注意事项的深入总结
仅用一句SQL更新整张表的涨跌幅、涨跌率的解决方案
May 06 #MySQL
MySQL创建高性能索引的全步骤
将图片保存到mysql数据库并展示在前端页面的实现代码
You might like
用PHP发电子邮件
2006/10/09 PHP
浅谈PHP定义命令空间的几个注意点(推荐)
2016/10/29 PHP
wordpress自定义标签云与随机获取标签的方法详解
2019/03/22 PHP
php实现的支付宝网页支付功能示例【基于TP5框架】
2019/09/16 PHP
运用jquery实现table单双行不同显示并能单行选中
2009/07/25 Javascript
关于innerHTML后丢失动态绑定的EVENT问题解决方法
2013/05/19 Javascript
JQUERY 获取IFrame中对象及获取其父窗口中对象示例
2013/08/19 Javascript
Javascript之Date对象详解
2016/06/07 Javascript
jQuery实现邮箱下拉列表自动补全功能
2016/09/08 Javascript
利用Vue v-model实现一个自定义的表单组件
2017/04/27 Javascript
微信小程序冒泡事件及其阻止方法实例分析
2018/12/06 Javascript
vue语法自动转typescript(解放双手)
2019/09/18 Javascript
Selenium 模拟浏览器动态加载页面的实现方法
2018/05/16 Python
Pycharm如何打断点的方法步骤
2019/06/13 Python
python实现从wind导入数据
2019/12/03 Python
python 已知一个字符,在一个list中找出近似值或相似值实现模糊匹配
2020/02/29 Python
django rest framework serializer返回时间自动格式化方法
2020/03/31 Python
Python求凸包及多边形面积教程
2020/04/12 Python
Python设计密码强度校验程序
2020/07/30 Python
解决PyCharm IDE环境下,执行unittest不生成测试报告的问题
2020/09/03 Python
如何通过python实现IOU计算代码实例
2020/11/02 Python
利用python+request通过接口实现人员通行记录上传功能
2021/01/13 Python
在HTML5中使用MathML数学公式的简单讲解
2016/02/19 HTML / CSS
JavaScript+Canvas实现自定义画板的示例代码
2019/05/13 HTML / CSS
Under Armour澳大利亚官网:美国知名的高端功能性运动品牌
2018/02/22 全球购物
中国汽车租赁行业头部企业:一嗨租车
2019/05/16 全球购物
开发房地产协议书
2014/09/14 职场文书
省委召开党的群众路线教育实践活动总结大会报告
2014/10/21 职场文书
临时用工协议书范本
2014/10/29 职场文书
研究生给导师的自荐信
2015/03/06 职场文书
幼儿园教师求职信
2015/03/20 职场文书
2015年学校党建工作总结
2015/05/19 职场文书
刑事申诉状范文
2015/05/20 职场文书
三好学生主要事迹怎么写
2015/11/03 职场文书
springboot拦截器无法注入redisTemplate的解决方法
2021/06/27 Java/Android
十大最强飞行系宝可梦,BUG燕上榜,第二是飞行系王者
2022/03/18 日漫