processlist命令的输出结果显示了有哪些线程在运行,可以帮助识别出有问题的查询语句,两种方式使用这个命令。
1. 进入mysql/bin目录下输入mysqladmin processlist;
2. 启动mysql,输入show processlist;
如果有SUPER权限,则可以看到全部的线程,否则,只能看到自己发起的线程(这是指,当前对应的MySQL帐户运行的线程)。
mysql> show processlist
-> ;
+—-+——-+———————-+——+———+——+——-+——————+
| Id | User | Host | db | Command | Time | State | Info |
+—-+——-+———————-+——+———+——+——-+——————+
| 34 | ucjmh | 192.168.56.101:54031 | NULL | Query | 0 | NULL | show processlist |
+—-+——-+———————-+——+———+——+——-+——————+
1 row in set (0.00 sec)
mysql> exit
Bye
[root@ucjmh ~]# mysql -uroot -poracle
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 35
Server version: 5.5.41-log Source distribution
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> show processlist;
+—-+——+———–+——+———+——+——-+——————+
| Id | User | Host | db | Command | Time | State | Info |
+—-+——+———–+——+———+——+——-+——————+
| 35 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+—-+——+———–+——+———+——+——-+——————+
1 row in set (0.00 sec)
各列的含义和用途:
id,标识会话id 类似于oracle中的sid,你要kill一个语句的时候很有用。
user列,显示单前用户,如果不是root,这个命令就只显示你权限范围内的sql语句。
host列,显示这个语句是从哪个ip的哪个端口上发出的。用来追踪出问题语句的用户。
db列,显示这个进程目前连接的是哪个数据库。
command列,显示当前连接的执行的命令,一般就是休眠(sleep),查询(query),连接(connect)。
time列,此这个状态持续的时间,单位是秒。
state列,显示使用当前连接的sql语句的状态,很重要的列,后续会有所有的状态的描述,请注意,state只是语句执行中的某一个状态,一个sql语句,已查询为例,可能需要经过copying to tmp table,Sorting result,Sending data等状态才可以完成,
info列,显示这个sql语句,因为长度有限,所以长的sql语句就显示不全,但是一个判断问题语句的重要依据。Checking table
正在检查数据表(这是自动的)。
Closing tables
正在将表中修改的数据刷新到磁盘中,同时正在关闭已经用完的表。这是一个很快的操作,如果不是这样的话,就应该确认磁盘空间是否已经满了或者磁盘是否正处于重负中。
Connect Out
复制从服务器正在连接主服务器。
Copying to tmp table on disk
由于临时结果集大于tmp_table_size,正在将临时表从内存存储转为磁盘存储以此节省内存。
Creating tmp table
正在创建临时表以存放部分查询结果。
deleting from main table
服务器正在执行多表删除中的第一部分,刚删除第一个表。
deleting from reference tables
服务器正在执行多表删除中的第二部分,正在删除其他表的记录。
Flushing tables
正在执行FLUSH TABLES,等待其他线程关闭数据表。
Killed
发送了一个kill请求给某线程,那么这个线程将会检查kill标志位,同时会放弃下一个kill请求。MySQL会在每次的主循环中检查kill标志位,不过有些情况下该线程可能会过一小段才能死掉。如果该线程程被其他线程锁住了,那么kill请求会在锁释放时马上生效。
Locked
被其他查询锁住了。
Sending data
正在处理Select查询的记录,同时正在把结果发送给客户端。
Sorting for group
正在为GROUP BY做排序。
Sorting for order
正在为ORDER BY做排序。
Opening tables
这个过程应该会很快,除非受到其他因素的干扰。例如,在执Alter TABLE或LOCK TABLE语句行完以前,数据表无法被其他线程打开。正尝试打开一个表。
Removing duplicates
正在执行一个Select DISTINCT方式的查询,但是MySQL无法在前一个阶段优化掉那些重复的记录。因此,MySQL需要再次去掉重复的记录,然后再把结果发送给客户端。
Reopen table
获得了对一个表的锁,但是必须在表结构修改之后才能获得这个锁。已经释放锁,关闭数据表,正尝试重新打开数据表。
Repair by sorting
修复指令正在排序以创建索引。
Repair with keycache
修复指令正在利用索引缓存一个一个地创建新索引。它会比Repair by sorting慢些。
Searching rows for update
正在讲符合条件的记录找出来以备更新。它必须在Update要修改相关的记录之前就完成了。
Sleeping
正在等待客户端发送新请求.
System lock
正在等待取得一个外部的系统锁。如果当前没有运行多个mysqld服务器同时请求同一个表,那么可以通过增加–skip-external-locking参数来禁止外部系统锁。
Upgrading lock
Insert DELAYED正在尝试取得一个锁表以插入新记录。
Updating
正在搜索匹配的记录,并且修改它们。
User Lock
正在等待GET_LOCK()。
Waiting for tables
该线程得到通知,数据表结构已经被修改了,需要重新打开数据表以取得新的结构。然后,为了能的重新打开数据表,必须等到所有其他线程关闭这个表。以下几种情况下会产生这个通知:FLUSH TABLES tbl_name, Alter TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE,或OPTIMIZE TABLE。
waiting for handler insert
Insert DELAYED已经处理完了所有待处理的插入操作,正在等待新的请求。
大部分状态对应很快的操作,只要有一个线程保持同一个状态好几秒钟,那么可能是有问题发生了,需要检查一下。
这个命令中最关键的就是state列,mysql列出的状态主要有以下几种:
Checking table
正在检查数据表(这是自动的)。
Closing tables
正在将表中修改的数据刷新到磁盘中,同时正在关闭已经用完的表。这是一个很快的操作,如果不是这样的话,就应该确认磁盘空间是否已经满了或者磁盘是否正处于重负中。
Connect Out
复制从服务器正在连接主服务器。
Copying to tmp table on disk
由于临时结果集大于tmp_table_size,正在将临时表从内存存储转为磁盘存储以此节省内存。
Creating tmp table
正在创建临时表以存放部分查询结果。
deleting from main table
服务器正在执行多表删除中的第一部分,刚删除第一个表。
deleting from reference tables
服务器正在执行多表删除中的第二部分,正在删除其他表的记录。
Flushing tables
正在执行FLUSH TABLES,等待其他线程关闭数据表。
Killed
发送了一个kill请求给某线程,那么这个线程将会检查kill标志位,同时会放弃下一个kill请求。MySQL会在每次的主循环中检查kill标志位,不过有些情况下该线程可能会过一小段才能死掉。如果该线程程被其他线程锁住了,那么kill请求会在锁释放时马上生效。
Locked
被其他查询锁住了。
Sending data
正在处理Select查询的记录,同时正在把结果发送给客户端。
Sorting for group
正在为GROUP BY做排序。
Sorting for order
正在为ORDER BY做排序。
Opening tables
这个过程应该会很快,除非受到其他因素的干扰。例如,在执Alter TABLE或LOCK TABLE语句行完以前,数据表无法被其他线程打开。正尝试打开一个表。
Removing duplicates
正在执行一个Select DISTINCT方式的查询,但是MySQL无法在前一个阶段优化掉那些重复的记录。因此,MySQL需要再次去掉重复的记录,然后再把结果发送给客户端。
Reopen table
获得了对一个表的锁,但是必须在表结构修改之后才能获得这个锁。已经释放锁,关闭数据表,正尝试重新打开数据表。
Repair by sorting
修复指令正在排序以创建索引。
Repair with keycache
修复指令正在利用索引缓存一个一个地创建新索引。它会比Repair by sorting慢些。
Searching rows for update
正在讲符合条件的记录找出来以备更新。它必须在Update要修改相关的记录之前就完成了。
Sleeping
正在等待客户端发送新请求.
System lock
正在等待取得一个外部的系统锁。如果当前没有运行多个mysqld服务器同时请求同一个表,那么可以通过增加–skip-external-locking参数来禁止外部系统锁。
Upgrading lock
Insert DELAYED正在尝试取得一个锁表以插入新记录。
Updating
正在搜索匹配的记录,并且修改它们。
User Lock
正在等待GET_LOCK()。
Waiting for tables
该线程得到通知,数据表结构已经被修改了,需要重新打开数据表以取得新的结构。然后,为了能的重新打开数据表,必须等到所有其他线程关闭这个表。以下几种情况下会产生这个通知:FLUSH TABLES tbl_name, Alter TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE,或OPTIMIZE TABLE。
waiting for handler insert
Insert DELAYED已经处理完了所有待处理的插入操作,正在等待新的请求。
大部分状态对应很快的操作,只要有一个线程保持同一个状态好几秒钟,那么可能是有问题发生了,需要检查一下。
在mysql的官方文档中列出了所有可能的状态。
8.14.2 General Thread States
-
After create
This occurs when the thread creates a table (including internal temporary tables), at the end of the function that creates the table. This state is used even if the table could not be created due to some error. -
Analyzing
The thread is calculating aMyISAM
table key distributions (for example, forANALYZE TABLE
). -
checking permissions
The thread is checking whether the server has the required privileges to execute the statement. -
Checking table
The thread is performing a table check operation. -
cleaning up
The thread has processed one command and is preparing to free memory and reset certain state variables. -
closing tables
The thread is flushing the changed table data to disk and closing the used tables. This should be a fast operation. If not, you should verify that you do not have a full disk and that the disk is not in very heavy use. -
converting HEAP to MyISAM
The thread is converting an internal temporary table from aMEMORY
table to an on-diskMyISAM
table. -
copy to tmp table
The thread is processing anALTER TABLE
statement. This state occurs after the table with the new structure has been created but before rows are copied into it. -
Copying to group table
If a statement has differentORDER BY
andGROUP BY
criteria, the rows are sorted by group and copied to a temporary table. -
Copying to tmp table
The server is copying to a temporary table in memory. -
Copying to tmp table on disk
The server is copying to a temporary table on disk. The temporary result set has become too large (see Section 8.4.4, “How MySQL Uses Internal Temporary Tables”). Consequently, the thread is changing the temporary table from in-memory to disk-based format to save memory. -
Creating index
The thread is processingALTER TABLE ... ENABLE KEYS
for aMyISAM
table. Creating sort index
The thread is processing aSELECT
that is resolved using an internal temporary table.-
creating table
The thread is creating a table. This includes creation of temporary tables. -
Creating tmp table
The thread is creating a temporary table in memory or on disk. If the table is created in memory but later is converted to an on-disk table, the state during that operation will beCopying to tmp table on disk
. -
deleting from main table
The server is executing the first part of a multiple-table delete. It is deleting only from the first table, and saving columns and offsets to be used for deleting from the other (reference) tables. -
deleting from reference tables
The server is executing the second part of a multiple-table delete and deleting the matched rows from the other tables. -
discard_or_import_tablespace
The thread is processing anALTER TABLE ... DISCARD TABLESPACE
orALTER TABLE ... IMPORT TABLESPACE
statement. -
end
This occurs at the end but before the cleanup ofALTER TABLE
,CREATE VIEW
,DELETE
,INSERT
,SELECT
, orUPDATE
statements. -
executing
The thread has begun executing a statement. -
Execution of init_command
The thread is executing statements in the value of theinit_command
system variable. -
freeing items
The thread has executed a command. Some freeing of items done during this state involves the query cache. This state is usually followed bycleaning up
. -
Flushing tables
The thread is executingFLUSH TABLES
and is waiting for all threads to close their tables. -
FULLTEXT initialization
The server is preparing to perform a natural-language full-text search. -
init
This occurs before the initialization ofALTER TABLE
,DELETE
,INSERT
,SELECT
, orUPDATE
statements. Actions taken by the server in this state include flushing the binary log, theInnoDB
log, and some query cache cleanup operations.For the
end
state, the following operations could be happening:- Removing query cache entries after data in a table is changed
- Writing an event to the binary log
- Freeing memory buffers, including for blobs
-
Killed
Someone has sent aKILL
statement to the thread and it should abort next time it checks the kill flag. The flag is checked in each major loop in MySQL, but in some cases it might still take a short time for the thread to die. If the thread is locked by some other thread, the kill takes effect as soon as the other thread releases its lock. -
Locked
The query is locked by another query. -
logging slow query
The thread is writing a statement to the slow-query log. -
NULL
This state is used for theSHOW PROCESSLIST
state. -
login
The initial state for a connection thread until the client has been authenticated successfully. -
Opening tables
,Opening table
The thread is trying to open a table. This is should be very fast procedure, unless something prevents opening. For example, anALTER TABLE
or aLOCK TABLE
statement can prevent opening a table until the statement is finished. It is also worth checking that yourtable_cache
value is large enough. -
optimizing
The server is performing initial optimizations for a query. -
preparing
This state occurs during query optimization. -
Purging old relay logs
The thread is removing unneeded relay log files. -
query end
This state occurs after processing a query but before thefreeing items
state. -
Reading from net
The server is reading a packet from the network. -
Removing duplicates
The query was usingSELECT DISTINCT
in such a way that MySQL could not optimize away the distinct operation at an early stage. Because of this, MySQL requires an extra stage to remove all duplicated rows before sending the result to the client. -
removing tmp table
The thread is removing an internal temporary table after processing aSELECT
statement. This state is not used if no temporary table was created. -
rename
The thread is renaming a table. -
rename result table
The thread is processing anALTER TABLE
statement, has created the new table, and is renaming it to replace the original table. -
Reopen tables
The thread got a lock for the table, but noticed after getting the lock that the underlying table structure changed. It has freed the lock, closed the table, and is trying to reopen it. -
Repair by sorting
The repair code is using a sort to create indexes. -
Repair done
The thread has completed a multi-threaded repair for aMyISAM
table. -
Repair with keycache
The repair code is using creating keys one by one through the key cache. This is much slower thanRepair by sorting
. -
Rolling back
The thread is rolling back a transaction. -
Saving state
ForMyISAM
table operations such as repair or analysis, the thread is saving the new table state to the.MYI
file header. State includes information such as number of rows, theAUTO_INCREMENT
counter, and key distributions. -
Searching rows for update
The thread is doing a first phase to find all matching rows before updating them. This has to be done if theUPDATE
is changing the index that is used to find the involved rows. Sending data
The thread is reading and processing rows for aSELECT
statement, and sending data to the client. Because operations occurring during this this state tend to perform large amounts of disk access (reads), it is often the longest-running state over the lifetime of a given query.-
setup
The thread is beginning anALTER TABLE
operation. -
Sorting for group
The thread is doing a sort to satisfy aGROUP BY
. -
Sorting for order
The thread is doing a sort to satisfy aORDER BY
. -
Sorting index
The thread is sorting index pages for more efficient access during aMyISAM
table optimization operation. -
Sorting result
For aSELECT
statement, this is similar toCreating sort index
, but for nontemporary tables. -
statistics
The server is calculating statistics to develop a query execution plan. If a thread is in this state for a long time, the server is probably disk-bound performing other work. -
System lock
The thread is going to request or is waiting for an internal or external system lock for the table. If this state is being caused by requests for external locks and you are not using multiple mysqld servers that are accessing the sameMyISAM
tables, you can disable external system locks with the--skip-external-locking
option. However, external locking is disabled by default, so it is likely that this option will have no effect. ForSHOW PROFILE
, this state means the thread is requesting the lock (not waiting for it). -
Table lock
The next thread state afterSystem lock
. The thread has acquired an external lock and is going to request an internal table lock. -
update
The thread is getting ready to start updating the table. -
Updating
The thread is searching for rows to update and is updating them. -
updating main table
The server is executing the first part of a multiple-table update. It is updating only the first table, and saving columns and offsets to be used for updating the other (reference) tables. -
updating reference tables
The server is executing the second part of a multiple-table update and updating the matched rows from the other tables. -
User lock
The thread is going to request or is waiting for an advisory lock requested with aGET_LOCK()
call. ForSHOW PROFILE
, this state means the thread is requesting the lock (not waiting for it). -
Waiting for release of readlock
The thread is waiting for a global read lock obtained by another thread (withFLUSH TABLES WITH READ LOCK
) to be released. -
Waiting for tables
,Waiting for table
The thread got a notification that the underlying structure for a table has changed and it needs to reopen the table to get the new structure. However, to reopen the table, it must wait until all other threads have closed the table in question.This notification takes place if another thread has used
FLUSH TABLES
or one of the following statements on the table in question:FLUSH TABLES
,tbl_name
ALTER TABLE
,RENAME TABLE
,REPAIR TABLE
,ANALYZE TABLE
, orOPTIMIZE TABLE
. -
Waiting on cond
A generic state in which the thread is waiting for a condition to become true. No specific state information is available. -
Waiting to get readlock
The thread has issued aFLUSH TABLES WITH READ LOCK
statement to obtain a global read lock and is waiting to obtain the lock. -
Writing to net
The server is writing a packet to the network.
[root@ucjmh ~]# cat kill_lock.sql
kill 43;
kill 55;
[root@ucjmh ~]# cat locked.txt
43 root localhost hi_db Query 3825 Waiting for table metadata lock insert into t values(4)
55 root localhost NULL Query 0 NULL show processlist
[root@ucjmh ~]# cat ger_lock.sh
#!/bin/bash
rm -rf locked.txt
rm -rf kill_lock.sql
mysql -uroot -poracle -e “show processlist”|grep -i “Query” >> locked.txt;
for line in $(awk ‘{printf $1″\n”}’ locked.txt)
do
echo “kill ${line};”>>kill_lock.sql
done