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 05 MySQL
MySQL获取所有分类的前N条记录
May 07 MySQL
MySQL查询学习之基础查询操作
May 08 MySQL
详解MySQL中的主键与事务
May 27 MySQL
mysql 带多个条件的查询方式
Jun 05 MySQL
浅谈MySQL user权限表
Jun 18 MySQL
新手入门Mysql--sql执行过程
Jun 20 MySQL
详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
Dec 06 MySQL
SQL基础查询和LINQ集成化查询
Jan 18 MySQL
mysql字段为NULL索引是否会失效实例详解
May 30 MySQL
MySql如何将查询的出来的字段进行转换
Jun 14 MySQL
MySQL池化框架学习接池自定义
Jul 23 MySQL
MySQL数字类型自增的坑
May 07 #MySQL
MySQL获取所有分类的前N条记录
May 07 #MySQL
教你解决往mysql数据库中存入汉字报错的方法
MySQL时间设置注意事项的深入总结
仅用一句SQL更新整张表的涨跌幅、涨跌率的解决方案
May 06 #MySQL
MySQL创建高性能索引的全步骤
将图片保存到mysql数据库并展示在前端页面的实现代码
You might like
将OICQ数据转成MYSQL数据
2006/10/09 PHP
PHP实现简单的新闻发布系统实例
2015/07/28 PHP
php+redis实现商城秒杀功能
2020/11/19 PHP
Javascript String.replace的妙用
2009/09/08 Javascript
学习ExtJS border布局
2009/10/08 Javascript
jquery下将选择的checkbox的id组成字符串的方法
2010/11/28 Javascript
让新消息在网页标题闪烁提示的jQuery代码
2013/11/04 Javascript
javascript获取元素偏移量的方法有哪些
2014/06/24 Javascript
Angular.js 实现数字转换汉字实例代码
2016/07/14 Javascript
JS在浏览器中解析Base64编码图像
2017/02/09 Javascript
详解vue-cli 2.0配置文件(小结)
2019/01/14 Javascript
ES6学习笔记之字符串、数组、对象、函数新增知识点实例分析
2020/01/22 Javascript
Vue实现导航栏菜单
2020/08/19 Javascript
[01:30]2016国际邀请赛中国区预选赛神秘商店火爆开启
2016/06/26 DOTA
Python中用pycurl监控http响应时间脚本分享
2015/02/02 Python
python中__slots__用法实例
2015/06/04 Python
python下MySQLdb用法实例分析
2015/06/08 Python
Python实现拷贝多个文件到同一目录的方法
2016/09/19 Python
python3安装pip3(install pip3 for python 3.x)
2018/04/03 Python
在Python中实现替换字符串中的子串的示例
2018/10/31 Python
Django框架视图层URL映射与反向解析实例分析
2019/07/29 Python
python将四元数变换为旋转矩阵的实例
2019/12/04 Python
python相对企业语言优势在哪
2020/06/12 Python
pyqt5 textEdit、lineEdit操作的示例代码
2020/08/12 Python
浅析Python中字符串的intern机制
2020/10/03 Python
pandas处理csv文件的方法步骤
2020/10/16 Python
美国在线购买和出售礼品卡网站:EJ Gift Cards
2019/06/09 全球购物
2013年大学生的自我鉴定
2013/10/24 职场文书
法学专业自我鉴定
2014/02/05 职场文书
小学生评语集锦
2014/04/18 职场文书
介绍信的格式
2015/01/30 职场文书
敬业奉献模范事迹材料(2016精选版)
2016/02/26 职场文书
Python利用folium实现地图可视化
2021/05/23 Python
Java 死锁解决方案
2022/05/11 Java/Android
Beekeeper Studio开源数据库管理工具比Navicat更炫酷
2022/06/21 数据库
MySQL表字段数量限制及行大小限制详情
2022/07/23 MySQL