mysql死锁情况(MySQL kill不掉线程的原因)
mysql死锁情况
MySQL kill不掉线程的原因背景
在日常的使用过程中,时不时会遇到个别,或者大量的连接堆积在 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的not like找不出NULL的问题)
- mysql8.0关键字段使用(MySQL 8.0新特性之隐藏字段的深入讲解)
- mysqlnull所占空间(详解mysql三值逻辑与NULL)
- mysql的decimal类型(MySQL数据类型DECIMAL用法详解)
- mysql的复制原理(浅析MySQL的WriteSet并行复制)
- mysql8.0.23的安装步骤(mysql 8.0.22 下载安装配置方法图文教程)
- mysql和mycat半主从复制项目(Linux如何使用 MyCat 实现 MySQL 主从读写分离)
- centos系统mysql安装及连接教程(CentOS8安装SQLServer2019的过程)
- docker搭建mysql服务(Docker部署Mysql集群的实现)
- mysqlselect语句汇总(MySQL Select语句是如何执行的)
- mysql密码设置(mysql密码中有特殊字符&在命令行下登录的操作)
- mysql主键自增策略(MySQL的主键命名策略相关)
- mysql存储引擎是什么(详解mysql中的存储引擎)
- mysql left join索引怎么使用(详解mysql 使用left join添加where条件的问题分析)
- mysql 日期和时间处理函数(MySQL日期与时间函数的使用汇总)
- mysql索引建立及应用(MYSQL创建索引,这些知识应该了解)
- 学会这26种姿势,你就可以和兵哥哥切磋了(你就可以和兵哥哥切磋了)
- 吴彦祖陈冠希 恩怨 ,失去曾让他流泪的女友,终遇走过18年真爱(吴彦祖陈冠希恩怨)
- 痴情男神 吴彦祖 与妻子恋爱8年,结婚10年,家庭幸福美满(痴情男神吴彦祖)
- 成功破圈,小牛电动SQi强势开 跨(小牛电动SQi强势开)
- 挑战新国标电自天花板,九号机械师MMAX 110P深度体验(挑战新国标电自天花板)
- 《满江红》不要只当电影看,学生应该这样做(满江红不要只当电影看)
热门推荐
- 织梦dedecms当前栏目页面样式(织梦dedecms二次开发之install安装改动攻略)
- jspromise原理(JavaScript使用promise处理多重复请求)
- canvas怎么画特效(canvas中普通动效与粒子动效的实现代码示例)
- 网页http400错误请求怎么解决(HTTP 错误 403.1禁止访问:禁止执行访问的完美解决方法)
- sqlserver统计表大小(SqlServer 垂直分表减少程序改动)
- 网页502错误怎么解决(502 bad gateway是什么意思?网页打不开出现502 bad gateway怎么解决?)
- 前端技术图解(前端水印的简单实现代码示例)
- sql server附加数据库出错(SQL Server附加数据库报错无法打开物理文件,操作系统错误5的图文解决教程)
- python中什么是迭代器(一篇文章彻底搞懂Python中可迭代Iterable、迭代器Iterator与生成器Generator的概)
- js中Document
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9