search
HomeDatabaseMysql TutorialMySQL中CTRL+C信号处理

MySQL中CTRL+C信号处理

Jun 07, 2016 pm 03:15 PM
ctrl+clinuxmysqlsignal processingEnter

欢迎进入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信号处理

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor