MySQL分析數據運行狀態【SHOW PROCESSLIST】

這個博文,將只是簡單的記錄一下,咱們的數據庫操做和使用中,加索引加不上去,分析的過程,其實比較簡單,就是看有沒有鏈接進程還在操做表。有的話,將其停掉(不影響業務的場景下)。css

 

今天的主角是:mysql

SHOW [FULL] PROCESSLISTsql

官方文檔的描述以下:數據庫

SHOW PROCESSLIST shows you which threads are running. You can also get this information from the
INFORMATION_SCHEMA PROCESSLIST table or the mysqladmin processlist command. If you have
the PROCESS privilege, you can see all threads. Otherwise, you can see only your own threads (that is,
threads associated with the MySQL account that you are using). If you do not use the FULL keyword, only
the first 100 characters of each statement are shown in the Info field.

意思就是說上述指令是用來查看那些線程正在運行,你也能夠獲得這些信息,從INFORMATION_SCHEMA PROCESSLIST這個表,或者經過mysqladmin processlist指令。若是你有PROCESS權限,你能夠查看全部的線程。不然,你只能查看你本身當前帳戶的線程。若是你沒有使用FULL關鍵字,你只能查看每一個記錄中Info字段裏面的前100個字符多線程

 

具體操做:ide

複製代碼
mysql> show processlist;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    46661
Current database: advsql

+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
| Id    | User      | Host              | db     | Command | Time  | State | Info             |
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
| 46586 | tkcssuser | 10.90.13.61:55838 | tkcss  | Sleep   |    42 |       | NULL             |
| 46660 | tkcssuser | 10.90.13.8:52008  | tkcss  | Sleep   | 11041 |       | NULL             |
| 46661 | root      | localhost         | advsql | Query   |     0 | init  | show processlist |
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
3 rows in set (0.01 sec)
複製代碼

 

經過查詢information_schema的processlist表:ui

複製代碼
mysql> select * from information_schema.processlist;
+-------+-----------+-------------------+--------+---------+-------+-----------+----------------------------------------------+
| ID    | USER      | HOST              | DB     | COMMAND | TIME  | STATE     | INFO                                         |
+-------+-----------+-------------------+--------+---------+-------+-----------+----------------------------------------------+
| 46661 | root      | localhost         | advsql | Query   |     0 | executing | select * from information_schema.processlist |
| 46586 | tkcssuser | 10.90.13.61:55838 | tkcss  | Sleep   |   279 |           | NULL                                         |
| 46660 | tkcssuser | 10.90.13.8:52008  | tkcss  | Sleep   | 11578 |           | NULL                                         |
+-------+-----------+-------------------+--------+---------+-------+-----------+----------------------------------------------+
3 rows in set (0.00 sec)
複製代碼

或者mysqladmin processlist指令:this

複製代碼
[root@localhost ~]# mysqladmin processlist -u root -p 
Enter password: 
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
| Id    | User      | Host              | db     | Command | Time  | State | Info             |
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
| 46586 | tkcssuser | 10.90.13.61:55838 | tkcss  | Sleep   | 138   |       |                  |
| 46660 | tkcssuser | 10.90.13.8:52008  | tkcss  | Sleep   | 12037 |       |                  |
| 46661 | root      | localhost         | advsql | Sleep   | 459   |       |                  |
| 46662 | root      | localhost         |        | Query   | 0     | init  | show processlist |
+-------+-----------+-------------------+--------+---------+-------+-------+------------------+
複製代碼

 

關於這個命令的價值,官方的介紹以下:spa

The SHOW PROCESSLIST statement is very useful if you get the 「too many connections」 error message
and want to find out what is going on. MySQL reserves one extra connection to be used by accounts that
have the SUPER privilege, to ensure that administrators should always be able to connect and check the
system (assuming that you are not giving this privilege to all your users).

Threads can be killed with the KILL statement.

運行的線程,能夠經過KILL指令將其殺掉。這個信息很是重要。線程

 

這裏,能夠看到SHOW PROCESSLIST指令裏面的輸出內容,有幾個字段,下面給予解釋說明,只有搞清楚了這些字段的含義,纔對咱們的實際項目問題分析纔有價值:

  • Id: 

    The connection identifier. This is the same type of value displayed in the ID column of the
    INFORMATION_SCHEMA.PROCESSLIST table, the PROCESSLIST_ID column of the Performance
    Schema threads table, and returned by the CONNECTION_ID() function.

  • User:

    The MySQL user who issued the statement. If this is system user, it refers to a nonclient thread
    spawned by the server to handle tasks internally. This could be the I/O or SQL thread used on replication
    slaves or a delayed-row handler. unauthenticated user refers to a thread that has become
    associated with a client connection but for which authentication of the client user has not yet been done.
    event_scheduler refers to the thread that monitors scheduled events. For system user, there is no
    host specified in the Host column.

  • Host:

    The host name of the client issuing the statement (except for system user where there is no host).
    SHOW PROCESSLIST reports the host name for TCP/IP connections in host_name:client_port
    format to make it easier to determine which client is doing what.

  • db:

     The default database, if one is selected, otherwise NULL.

  • Command:

     The type of command the thread is executing. 例如上面的例子中,Sleep,或者Query

  • Time:

    The time in seconds that the thread has been in its current state. For a slave SQL thread, the value is
    the number of seconds between the timestamp of the last replicated event and the real time of the slave
    machine.

  • State:

    Most states correspond to very quick operations. If a thread stays in a given state for many seconds,
    there might be a problem that needs to be investigated.

     An action, event, or state that indicates what the thread is doing.

  • Info:

    The statement the thread is executing, or NULL if it is not executing any statement. The statement might
    be the one sent to the server, or an innermost statement if the statement executes other statements. For
    example, if a CALL statement executes a stored procedure that is executing a SELECT statement, the
    Info value shows the SELECT statement.

 

上述信息的解釋當中,對於咱們分析問題,尤其重要的是Id,Host,Time,State以及Info字段。這幾個當中,第一眼能讓咱們看出問題的是Time,State和Info字段。咱們若想看得更詳細的Info字段,請用SHOW FULL PROCESSLIST指令注意前面的命令解釋內容。

 

咱們線上的一個問題,就是加索引加不上去,咱們將應用停掉仍是加不上去,經過SHOW PROCESSLIST指令,發現有好多線程Command處於Query狀態。最長的Time字段顯示達到230172seconds,換算成小時,都63個小時。

 

爲了解決問題,當即調用KILL指令,前面介紹時提到的。

KILL [CONNECTION | QUERY] processlist_id

官方介紹:

Each connection to mysqld runs in a separate thread. You can kill a thread with the KILL
processlist_id statement.

參數中的processlist_id,來源於show processlist結果列表中的id字段。

kill指令支持兩個可選參數,CONNECTION以及QUERY。

• KILL CONNECTION is the same as KILL with no modifier: It terminates the connection associated with
the given processlist_id, after terminating any statement the connection is executing.
• KILL QUERY terminates the statement the connection is currently executing, but leaves the connection
itself intact.

很簡單,connection選項,kill的時候,將鏈接也斷掉,而query選項,kill的過程只是將該指令殺掉,鏈接還保持。 kill指令不指定connection或者query選項時,默認是connection。

相關文章
相關標籤/搜索