show processlist命令与kill 线程

本文深入解析MySQL的show processlist命令,展示如何查看和管理正在运行的线程,包括使用kill命令终止特定线程的方法。同时,文章详细解释了processlist中各字段的意义,如ID、user、host、db、command等,以及不同command值所代表的线程状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

show [full] processlist

show processlist显示正在运行的线程。如果有process权限,则可以查看所有正在运行的线程。否则,只能看到自己的线程。如果不使用full关键字,则只在info字段显示每个语句的前100个字符。

show processlist命令是非常有用的,如果你获得到“too many connections”错误信息,并且想知道什么正在运行。MySQL保留了一个额外的连接给超级管理员。

线程能够被kill掉,使用kill语句。

mysql> show processlist;
+----+------+--------------------+-----------+---------+------+-------------+------------------+
| Id | User | Host               | db        | Command | Time | State       | Info             |
+----+------+--------------------+-----------+---------+------+-------------+------------------+
| 36 | root | 172.16.100.19:7954 | tpcc_test | Sleep   |  456 |             | NULL             |
| 37 | root | 172.16.100.19:7969 | tpcc_test | Sleep   |  456 |             | NULL             |
| 42 | root | localhost          | NULL      | Query   |    0 | System lock | show processlist |
| 43 | root | 10.0.102.204:49224 | employees | Sleep   |   12 |             | NULL             |
+----+------+--------------------+-----------+---------+------+-------------+------------------+
4 rows in set (0.00 sec)

这个显示的几个字段还是比较好理解的。

  • ID:连接标识。这个值和INFORMATION_SCHEMA.PROCESSLIST表的ID列,以及PERFORMANCE_SCHEMA中的threads中的process_id值是相同的。
  • user: 发出该语句的MySQL用户。system user是指由服务器生成的用于内部处理任务的非客户端线程。这可以是用于复制的I/O线程,或SQL线程,也可以是延迟的行处理程序。未经身份验证的用户是指已经与客户端连接但尚未对客户端身份进行验证的线程。
  • Host:客户端使用的主机。系统用户没有host值。
  • db:连接的数据库。
  • command:线程执行的命令
    A thread can have any of the following Command values:
    
    Binlog Dump:  This is a thread on a master server for sending binary log contents to a slave server.
    
    Change user: The thread is executing a change-user operation.
    
    Close stmt: The thread is closing a prepared statement.[关闭准备好的声明]
    
    Connect:A replication slave is connected to its master. 【已经连接】
    
    Connect Out:A replication slave is connecting to its master. 【正则连接】
    
    Create DB:The thread is executing a create-database operation.
    
    Daemon:This thread is internal to the server, not a thread that services a client connection. 【这个线程是服务器内部线程,而不是客户端线程】
    
    Debug:The thread is generating debugging information. 【线程正在生成调试信息】
    
    Delayed insert:The thread is a delayed-insert handler.【延迟插入处理】
    
    Drop DB:The thread is executing a drop-database operation.
    
    Error:
    
    Execute:The thread is executing a prepared statement. 【线程正在执行预准备的语句】
    
    Fetch:The thread is fetching the results from executing a prepared statement.【线程正在从预准备语句拉取结果】
    
    Field List:The thread is retrieving information for table columns.【该线程正则检索列表信息】
    
    Init DB:The thread is selecting a default database. 【线程正在选择一个默认数据库】
    
    Kill:The thread is killing another thread. 【线程正则kill掉其他线程】
    
    Long Data:The thread is retrieving long data in the result of executing a prepared statement.
    
    Ping:The thread is handling a server-ping request.
    
    Prepare:The thread is preparing a prepared statement.
    
    Processlist:The thread is producing information about server threads.
    
    Query: The thread is executing a statement.
    
    Quit:The thread is terminating.
    
    Refresh:The thread is flushing table, logs, or caches, or resetting status variable or replication server information.
    
    Register Slave: The thread is registering a slave server.
    
    Reset stmt: The thread is resetting a prepared statement.
    
    Set option: The thread is setting or resetting a client statement-execution option.
    
    Shutdown: The thread is shutting down the server.
    
    Sleep: The thread is waiting for the client to send a new statement to it.
    
    Statistics:The thread is producing server-status information.
    
    Table Dump:The thread is sending table contents to a slave server.
    
    Time:Unused.
    command的取值
  • time: 线程已经在当前状态的时间。
  • state:一个动作,一个事件,标识线程正在做什么。https://blog.youkuaiyun.com/sunqingzhong44/article/details/70570728
  • info:Info contains the text of the statement being executed by the thread, or NULL if it is not executing one. By default, this value contains only the first 100 characters of the statement. To see the complete statements, useSHOW FULL PROCESSLIST.

进程的信息也可以通过mysqladmin processlist命令查看。

相比较之下show processlist和information_schema.processlist需要互斥锁,而performance_schema.threads不需要,threads对服务器性能影响最小,threads表还显示有关后台线程的信息。

kill线程

与MySQL服务器每个链接都在一个单独的线程中运行。可以使用如下语句杀死一个线程。

kill [connection| query] processlist_id

connection: 与kill processlist_id相同;中断连接正在执行的任何语句之后,中断连接。
query: 中断连接正在执行的语句,但是保持本身的连接。

有三种方法可以获得processlist_id,上面已经提到了!

当你使用kill时,一个特殊的kill标记被设置给这个线程。在大多数的情况下,它可能会等一段时间线程再死亡,因为kill标记仅在一些情况下被检测。

  • 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.

 

mysql> show processlist;
+----+------+---------------------+--------------------+---------+------+-------------+------------------+
| Id | User | Host                | db                 | Command | Time | State       | Info             |
+----+------+---------------------+--------------------+---------+------+-------------+------------------+
| 36 | root | 172.16.100.19:7954  | tpcc_test          | Sleep   |    8 |             | NULL             |
| 37 | root | 172.16.100.19:7969  | tpcc_test          | Sleep   |    8 |             | NULL             |
| 42 | root | localhost           | NULL               | Query   |    0 | System lock | show processlist |
| 43 | root | 10.0.102.204:49224  | employees          | Sleep   | 2564 |             | NULL             |
| 44 | root | 172.16.100.19:14529 | NULL               | Sleep   | 2322 |             | NULL             |
| 45 | root | 172.16.100.19:14770 | information_schema | Sleep   | 2315 |             | NULL             |
| 46 | root | 172.16.100.19:1508  | information_schema | Sleep   | 2314 |             | NULL             |
| 47 | root | 172.16.100.19:10572 | performance_schema | Sleep   | 2231 |             | NULL             |
| 48 | root | 172.16.100.19:11392 | performance_schema | Sleep   | 2231 |             | NULL             |
+----+------+---------------------+--------------------+---------+------+-------------+------------------+
9 rows in set (0.00 sec)

mysql> kill 43;              #kill掉43线程
Query OK, 0 rows affected (0.00 sec)

mysql> show processlist;
+----+------+---------------------+--------------------+---------+------+-------------+------------------+
| Id | User | Host                | db                 | Command | Time | State       | Info             |
+----+------+---------------------+--------------------+---------+------+-------------+------------------+
| 36 | root | 172.16.100.19:7954  | tpcc_test          | Sleep   |  386 |             | NULL             |
| 37 | root | 172.16.100.19:7969  | tpcc_test          | Sleep   |  386 |             | NULL             |
| 42 | root | localhost           | NULL               | Query   |    0 | System lock | show processlist |
| 44 | root | 172.16.100.19:14529 | NULL               | Sleep   | 3300 |             | NULL             |
| 45 | root | 172.16.100.19:14770 | information_schema | Sleep   | 3293 |             | NULL             |
| 46 | root | 172.16.100.19:1508  | information_schema | Sleep   | 3292 |             | NULL             |
| 47 | root | 172.16.100.19:10572 | performance_schema | Sleep   | 3209 |             | NULL             |
| 48 | root | 172.16.100.19:11392 | performance_schema | Sleep   | 3209 |             | NULL             |
+----+------+---------------------+--------------------+---------+------+-------------+------------------+
8 rows in set (0.00 sec)


MySQL
[employees]> select * from employees; #连接发现失败 ERROR 2013 (HY000): Lost connection to MySQL server during query

 

转载于:https://www.cnblogs.com/wxzhe/p/10239852.html

<think>好的,我需要帮助用户解决如何查看MySQL服务器上运行的线程和状态的问题。用户提到了使用`SHOW PROCESSLIST`和`EXPLAIN`命令。首先,我需要回忆这两个命令的具体用法和区别。 首先,`SHOW PROCESSLIST`用于显示当前MySQL服务器上的所有活动线程,每个线程对应一个客户端连接。这个命令能展示每个线程的ID、用户、执行状态、时间等信息,帮助识别长时间运行的查询或阻塞的进程。根据引用[2]和[3],用户通常用这个命令来检查服务器压力,找出慢查询或需要终止的线程。此外,引用[5]还提到可以通过查询`information_schema.processlist`来生成Kill语句,处理耗时过长的线程。 接下来是`EXPLAIN`命令,它用于分析查询的执行计划,帮助优化SQL语句。用户可能需要知道如何结合这两个命令,比如先通过`SHOW PROCESSLIST`找到慢查询,再用`EXPLAIN`分析其执行计划。但要注意的是,`EXPLAIN`需要具体的SQL语句,不能直接`SHOW PROCESSLIST`一起使用,所以需要分别执行这两个步骤。 用户的问题中还提到了“mysql show processlist explain”,可能存在误解,认为两者可以结合使用。需要澄清这两个命令的独立用途,并说明如何分步使用它们来诊断问题。例如,先使用`SHOW PROCESSLIST`找到问题查询的SQL,然后单独对其使用`EXPLAIN`进行分析。 另外,根据引用[4],MySQL 5.6之后`SHOW PROFILE`将被弃用,所以可能需要注意版本差异。但用户的问题主要集中在`SHOW PROCESSLIST`和`EXPLAIN`上,这部分不受影响。 在回答结构上,需要分步骤说明如何使用这两个命令,提供示例代码,并引用相关的参考资料。同时,按照用户的要求生成相关问题,确保问题主题相关且有实际意义。 最后,检查是否需要处理权限问题,比如`SHOW PROCESSLIST`可能需要特定权限,而`EXPLAIN`通常对所有用户开放。确保回答中提到这些注意事项,帮助用户避免权限错误。 总结步骤: 1. 解释`SHOW PROCESSLIST`的作用和使用方法,给出示例。 2. 说明如何分析结果,找出问题线程。 3. 介绍`EXPLAIN`的用途,如何结合找到的SQL进行优化。 4. 强调两个命令需分开使用,无法直接结合。 5. 提供相关问题的生成,如权限问题、执行计划解读等。</think>要查看MySQL服务器上运行的线程和状态,可以使用`SHOW PROCESSLIST`命令,结合`EXPLAIN`分析具体查询的执行计划。以下是分步说明: --- ### 1. 使用`SHOW PROCESSLIST`查看当前线程 **作用**:显示所有客户端连接线程的实时状态,包括执行中的SQL语句、耗时、状态等[^2][^3]。 **语法**: ```sql SHOW [FULL] PROCESSLIST; ``` - `FULL`选项会显示完整的SQL语句(默认截断前100字符)。 **示例输出**: | Id | User | Host | db | Command | Time | State | Info | |-----|---------|---------------|------|---------|------|----------|------------------| | 5 | root | localhost:123 | test | Query | 2 | executing| SELECT * FROM ...| **关键字段解释**: - `Id`:线程唯一标识,可用于终止连接(`KILL id`) - `Time`:执行时间(秒),长时间值可能表示慢查询 - `State`:线程状态(如`Locked`、`Sending data`) - `Info`:正在执行的SQL语句 **操作示例**: ```sql -- 查看完整SQL语句 SHOW FULL PROCESSLIST; -- 查找执行时间超过2分钟的线程并生成终止命令(引用[5]) SELECT CONCAT('KILL ', id, ';') FROM information_schema.processlist WHERE command != 'Sleep' AND time > 120; ``` --- ### 2. 使用`EXPLAIN`分析查询性能 **作用**:解析SQL语句的执行计划,帮助优化索引和查询效率。 **使用条件**: - 需先通过`SHOW PROCESSLIST`获取具体的慢查询语句 - 对目标SQL添加`EXPLAIN`前缀 **示例**: ```sql EXPLAIN SELECT * FROM orders WHERE user_id = 100; ``` **输出关键字段**: - `type`:访问类型(如`ALL`全表扫描,`index`索引扫描) - `possible_keys`:可能使用的索引 - `key`:实际使用的索引 - `rows`:预估扫描行数 --- ### 3. 组合使用场景 1. **定位问题线程**:通过`SHOW PROCESSLIST`找到高耗时的SQL 2. **分析执行计划**:将对应的SQL粘贴出来,添加`EXPLAIN`分析 3. **优化措施**:根据`EXPLAIN`结果调整索引或查询逻辑 --- ### 注意事项 1. **权限要求**: - `SHOW PROCESSLIST`需要`PROCESS`权限 - `KILL`命令需要`SUPER`权限[^3] 2. **版本差异**: - MySQL 5.6支持`SHOW PROFILE`(后续版本已弃用,引用[4]) 3. **性能影响**: - `SHOW PROCESSLIST`对性能影响较小,但频繁执行仍需谨慎 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值