这个博文,将只是简单的记录一下,咱们的数据库操做和使用中,加索引加不上去,分析的过程,其实比较简单,就是看有没有链接进程还在操做表。有的话,将其停掉(不影响业务的场景下)。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指令里面的输出内容,有几个字段,下面给予解释说明,只有搞清楚了这些字段的含义,才对咱们的实际项目问题分析才有价值:
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.
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.
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.
The default database, if one is selected, otherwise NULL.
The type of command the thread is executing. 例如上面的例子中,Sleep,或者Query
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.
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.
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。