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
jdbc使用PreparedStatement批量插入数据的方法
Apr 27 MySQL
MySQL 常见存储引擎的优劣
Jun 02 MySQL
Mysql systemctl start mysqld报错的问题解决
Jun 03 MySQL
MySQL如何使用使用Xtrabackup进行备份和恢复
Jun 21 MySQL
SQL实现LeetCode(176.第二高薪水)
Aug 04 MySQL
mysql分组后合并显示一个字段的多条数据方式
Jan 22 MySQL
一文了解MySQL二级索引的查询过程
Feb 24 MySQL
Mysql开启外网访问
May 15 MySQL
mysql数据库隔离级别详解
Jun 16 MySQL
MySQL 原理与优化之Update 优化
Aug 14 MySQL
SQL中去除重复数据的几种方法汇总(窗口函数对数据去重)
May 08 MySQL
MySQL数字类型自增的坑
May 07 #MySQL
MySQL获取所有分类的前N条记录
May 07 #MySQL
教你解决往mysql数据库中存入汉字报错的方法
MySQL时间设置注意事项的深入总结
仅用一句SQL更新整张表的涨跌幅、涨跌率的解决方案
May 06 #MySQL
MySQL创建高性能索引的全步骤
将图片保存到mysql数据库并展示在前端页面的实现代码
You might like
php批量删除cookie的简单实现方法
2015/01/26 PHP
php生出随机字符串
2017/07/06 PHP
Laravel 将数据表的数据导出,并生成seeds种子文件的方法
2019/10/09 PHP
js中cookie的使用详细分析
2008/05/28 Javascript
jQuery遍历Table应用示例
2014/04/09 Javascript
javascript函数声明和函数表达式区别分析
2014/12/02 Javascript
Javascript实现获取窗口的大小和位置代码分享
2014/12/04 Javascript
javascript实现瀑布流自适应遇到的问题及解决方案
2015/01/28 Javascript
js实现图片点击左右轮播
2015/07/08 Javascript
JavaScript获取function所有参数名的方法
2015/10/30 Javascript
在javascript中创建对象的各种模式解析
2016/05/16 Javascript
Bootstrap编写一个同时适用于PC、平板、手机的登陆页面
2016/06/30 Javascript
Vue3 中的数据侦测的实现
2019/10/09 Javascript
解决vue安装less报错Failed to compile with 1 errors的问题
2020/10/22 Javascript
[00:42]《辉夜杯》—职业组预选赛12月3日15点 正式打响
2015/12/03 DOTA
python 简单备份文件脚本v1.0的实例
2017/11/06 Python
利用Python暴力破解zip文件口令的方法详解
2017/12/21 Python
Python中%是什么意思?python中百分号如何使用?
2018/03/20 Python
Python基于多线程操作数据库相关问题分析
2018/07/11 Python
django 信号调度机制详解
2019/07/19 Python
GitHub上值得推荐的8个python 项目
2020/10/30 Python
Django 实现图片上传和下载功能
2020/12/31 Python
分享CSS3制作卡片式图片的方法
2016/07/08 HTML / CSS
台湾团购、宅配和优惠券:17Life
2017/08/14 全球购物
英国领先的亚洲旅游专家:Wendy Wu Tours
2018/01/21 全球购物
澳洲女装时尚在线:Blue Bungalow
2018/05/05 全球购物
WatchShop法国:英国排名第一的独立手表零售商
2020/02/17 全球购物
《桂林山水》教学反思
2014/02/08 职场文书
管事部库房保管员岗位职责
2014/02/21 职场文书
矿泉水广告词
2014/03/20 职场文书
开工仪式策划方案
2014/05/23 职场文书
团队队名口号大全
2014/06/06 职场文书
2015年人事专员工作总结
2015/04/29 职场文书
JavaScript 对象创建的3种方法
2021/11/17 Javascript
Python matplotlib绘制条形统计图 处理多个实验多组观测值
2022/04/21 Python
Golang 实现 WebSockets 之创建 WebSockets
2022/04/24 Golang