时间:2021-07-01 10:21:17 帮助过:81人阅读
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 目的 为了理解MySQL在执行大SQL时,对执行CTRL+C产生的疑惑,本文通过实验测试和源码分析两个方面,对MySQL处理CTRL+C的详细过程进行分析和讲解,从而解除DBA及开发人员对CTRL+C的误解。 测试 首先,
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
目的
为了理解MySQL在执行大SQL时,对执行CTRL+C产生的疑惑,本文通过实验测试和源码分析两个方面,对MySQL处理CTRL+C的详细过程进行分析和讲解,从而解除DBA及开发人员对CTRL+C的误解。
测试
首先,基于线上数据库版本,分别使用MySQL客户端版本5.5.20和5.0.77进行实验测试,一方面排除不同数据库客户端版本造成的差异,另一方面,深入了解不同版本执行CTRL+C产生的差异。
MySQL客户端5.5.20
使用MySQL客户端5.5.20在Session1中执行select sleep(100)语句,在Session2中执行show processlist语句;然后在Session1中执行CTRL+C,在Session中执行show processlist语句,查看当前连接的线程。执行的图如下所示:
Session1:
mysql> select sleep(100);
Ctrl-C -- sending "KILL QUERY 153779" to server ...
Ctrl-C -- query aborted.
ERROR 2013 (HY000): Lost connection to MySQL server during query
Session2:
mysql> show processlist;
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+-------------------+-----------+---------------+-----------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined | Rows_read |
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+-------------------+-----------+---------------+-----------+
| 153779 | heng.wang | 127.0.0.1:39882 | NULL | Query | 8 | User sleep | select sleep(100) | 0 | 0 | 1 |
| 153780 | heng.wang | 127.0.0.1:39883 | NULL | Query | 0 | NULL | show processlist | 0 | 0 | 1 |
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+-------------------+-----------+---------------+-----------+
2 rows in set (0.00 sec)
mysql> show processlist;
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+------------------+-----------+---------------+-----------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined | Rows_read |
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+------------------+-----------+---------------+-----------+
| 153780 | heng.wang | 127.0.0.1:39883 | NULL | Query | 0 | NULL | show processlist | 0 | 0 | 1 |
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+------------------+-----------+---------------+-----------+
1 rows in set (0.00 sec)
从以上结果来看,Session1中执行select操作时,Session2中可以查看该连接正在执行,在Session1中执行CTRL+C时,客户端向服务器端发送KILL QUERY
MySQL客户端5.0.77
使用客户端5.0.77执行同样的操作,执行CTRL+C后,观察执行的差异性。具体如下表中所示:
Session1:
mysql> select sleep(100);
Query aborted by Ctrl+C
+------------+
| sleep(100) |
+------------+
| 1 |
+------------+
Session2:
mysql> show processlist;
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+-------------------+-----------+---------------+-----------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined | Rows_read |
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+-------------------+-----------+---------------+-----------+
| 153783 | heng.wang | 127.0.0.1:45807 | NULL | Query | 3 | User sleep | select sleep(100) | 0 | 0 | 1 |
| 153784 | heng.wang | 127.0.0.1:45809 | NULL | Query | 0 | NULL | show processlist | 0 | 0 | 1 |
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+-------------------+-----------+---------------+-----------+
2 rows in set (0.00 sec)
mysql> show processlist;
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+------------------+-----------+---------------+-----------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined | Rows_read |
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+------------------+-----------+---------------+-----------+
| 153783 | heng.wang | 127.0.0.1:45807 | NULL | Sleep | 10 | | NULL | 1 | 0 | 1 |
| 153784 | heng.wang | 127.0.0.1:45809 | NULL | Query | 0 | NULL | show processlist | 0 | 0 | 1 |
+--------+-------------+-----------------+------+-------------+---------+-----------------------------------------------------------------------------+------------------+-----------+---------------+-----------+
2 rows in set (0.00 sec)
从以上结果可知,Session1上执行select时,Session2中建立查询连接;在Session1中执行CTRL+C时,显示Query被终止,并且返回执行的错误结果。在Session2中可知,连接的线程仍然存在,但是Query被终止,只是保持连接。
源码分析
为了更进一步对以上测试进行确认,查看MySQL源码进行进一步的求证,同样基于MySQL的5.5.20和5.0.77两个版本进行验证。具体如下:
MySQL 5.5.20源码
MySQL客户端主函数main中,信号函数在源码文件client/mysql.cc:1163,源码如下所示:
signal(SIGINT, handle_sigint); // Catch SIGINT to clean up
[1] [2] [3]