MySQL中CTRL+C信号处理
欢迎进入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]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

DMA in C refers to DirectMemoryAccess, a direct memory access technology, allowing hardware devices to directly transmit data to memory without CPU intervention. 1) DMA operation is highly dependent on hardware devices and drivers, and the implementation method varies from system to system. 2) Direct access to memory may bring security risks, and the correctness and security of the code must be ensured. 3) DMA can improve performance, but improper use may lead to degradation of system performance. Through practice and learning, we can master the skills of using DMA and maximize its effectiveness in scenarios such as high-speed data transmission and real-time signal processing.

Handling high DPI display in C can be achieved through the following steps: 1) Understand DPI and scaling, use the operating system API to obtain DPI information and adjust the graphics output; 2) Handle cross-platform compatibility, use cross-platform graphics libraries such as SDL or Qt; 3) Perform performance optimization, improve performance through cache, hardware acceleration, and dynamic adjustment of the details level; 4) Solve common problems, such as blurred text and interface elements are too small, and solve by correctly applying DPI scaling.

macOSandLinuxbothofferuniquestrengths:macOSprovidesauser-friendlyexperiencewithexcellenthardwareintegration,whileLinuxexcelsinflexibilityandcommunitysupport.macOS,developedbyApple,isknownforitssleekinterfaceandecosystemintegration,whereasLinux,beingo

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.
