show processlist命令與kill 線程


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.csdn.net/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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM