> 데이터 베이스 > MySQL 튜토리얼 > MySQL中CTRL+C信号处理

MySQL中CTRL+C信号处理

WBOY
풀어 주다: 2016-06-07 15:15:19
원래의
1259명이 탐색했습니다.

欢迎进入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 命令,并且连接关闭。在Session2中可以看到执行select的连接已经关闭。

  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] 

MySQL中CTRL+C信号处理

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿