zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysqlshowprocesslist显示mysql查询进程

mysql进程 查询 显示
2023-06-13 09:14:33 时间
1.进入mysql/bin目录下输入mysqladminprocesslist;
2.启动mysql,输入showprocesslist;
如果有SUPER权限,则可以看到全部的线程,否则,只能看到自己发起的线程(这是指,当前对应的MySQL帐户运行的线程)。
得到数据形式如下(只截取了三条):
mysql>showprocesslist;
+-----+-------------+--------------------+-------+---------+-------+----------------------------------+----------
|Id|User|Host|db|Command|Time|State|Info
+-----+-------------+--------------------+-------+---------+-------+----------------------------------+----------
|207|root|192.168.0.20:51718|mytest|Sleep|5||NULL
|208|root|192.168.0.20:51719|mytest|Sleep|5||NULL
|220|root|192.168.0.20:51731|mytest|Query|84|Locked|
selectbookname,culture,value,typefrombookwhereid=001
先简单说一下各列的含义和用途,第一列,id,不用说了吧,一个标识,你要kill一个语句的时候很有用。user列,显示单前用户,如果不是root,这个命令就只显示你权限范围内的sql语句。host列,显示这个语句是从哪个ip的哪个端口上发出的。呵呵,可以用来追踪出问题语句的用户。db列,显示这个进程目前连接的是哪个数据库。command列,显示当前连接的执行的命令,一般就是休眠(sleep),查询(query),连接(connect)。time列,此这个状态持续的时间,单位是秒。state列,显示使用当前连接的sql语句的状态,很重要的列,后续会有所有的状态的描述,请注意,state只是语句执行中的某一个状态,一个sql语句,已查询为例,可能需要经过copyingtotmptable,Sortingresult,Sendingdata等状态才可以完成,info列,显示这个sql语句

如果你在一个容量大的表中执行增删改字段或执行一个复杂的sql查询导致mysql线程挂起.可用此命令查看出是哪些sql挂起,用kill命令把他K掉

kill命令使用方法

killpid

如上例中我们想kill掉id为207的线程则执行

kill207即可.

mysqlshowprocesslist命令详解


SHOWPROCESSLIST显示哪些线程正在运行。您也可以使用mysqladminprocesslist语句得到此信息。如果您有SUPER权限,您可以看到所有线程。否则,您只能看到您自己的线程(也就是,与您正在使用的MySQL账户相关的线程)。请参见13.5.5.3节,“KILL语法”。如果您不使用FULL关键词,则只显示每个查询的前100个字符。

本语句报告TCP/IP连接的主机名称(采用host_name:client_port格式),以方便地判定哪个客户端正在做什么。

如果您得到“toomanyconnections”错误信息,并且想要了解正在发生的情况,本语句是非常有用的。MySQL保留一个额外的连接,让拥有SUPER权限的账户使用,以确保管理员能够随时连接和检查系统(假设您没有把此权限给予所有的用户)。

这个命令中最关键的就是state列,mysql列出的状态主要有以下几种:

Checkingtable
 正在检查数据表(这是自动的)。
Closingtables
 正在将表中修改的数据刷新到磁盘中,同时正在关闭已经用完的表。这是一个很快的操作,如果不是这样的话,就应该确认磁盘空间是否已经满了或者磁盘是否正处于重负中。
ConnectOut
 复制从服务器正在连接主服务器。
Copyingtotmptableondisk
 由于临时结果集大于tmp_table_size,正在将临时表从内存存储转为磁盘存储以此节省内存。
Creatingtmptable
 正在创建临时表以存放部分查询结果。
deletingfrommaintable
 服务器正在执行多表删除中的第一部分,刚删除第一个表。
deletingfromreferencetables
 服务器正在执行多表删除中的第二部分,正在删除其他表的记录。
Flushingtables
 正在执行FLUSHTABLES,等待其他线程关闭数据表。
Killed
 发送了一个kill请求给某线程,那么这个线程将会检查kill标志位,同时会放弃下一个kill请求。MySQL会在每次的主循环中检查kill标志位,不过有些情况下该线程可能会过一小段才能死掉。如果该线程程被其他线程锁住了,那么kill请求会在锁释放时马上生效。
Locked
 被其他查询锁住了。
Sendingdata
 正在处理SELECT查询的记录,同时正在把结果发送给客户端。
Sortingforgroup
 正在为GROUPBY做排序。
 Sortingfororder
 正在为ORDERBY做排序。
Openingtables
 这个过程应该会很快,除非受到其他因素的干扰。例如,在执ALTERTABLE或LOCKTABLE语句行完以前,数据表无法被其他线程打开。正尝试打开一个表。
Removingduplicates
 正在执行一个SELECTDISTINCT方式的查询,但是MySQL无法在前一个阶段优化掉那些重复的记录。因此,MySQL需要再次去掉重复的记录,然后再把结果发送给客户端。
Reopentable
 获得了对一个表的锁,但是必须在表结构修改之后才能获得这个锁。已经释放锁,关闭数据表,正尝试重新打开数据表。
Repairbysorting
 修复指令正在排序以创建索引。
Repairwithkeycache
 修复指令正在利用索引缓存一个一个地创建新索引。它会比Repairbysorting慢些。
Searchingrowsforupdate
 正在讲符合条件的记录找出来以备更新。它必须在UPDATE要修改相关的记录之前就完成了。
Sleeping
 正在等待客户端发送新请求.
Systemlock
 正在等待取得一个外部的系统锁。如果当前没有运行多个mysqld服务器同时请求同一个表,那么可以通过增加--skip-external-locking参数来禁止外部系统锁。
Upgradinglock
 INSERTDELAYED正在尝试取得一个锁表以插入新记录。
Updating
 正在搜索匹配的记录,并且修改它们。
UserLock
 正在等待GET_LOCK()。
Waitingfortables
 该线程得到通知,数据表结构已经被修改了,需要重新打开数据表以取得新的结构。然后,为了能的重新打开数据表,必须等到所有其他线程关闭这个表。以下几种情况下会产生这个通知:FLUSHTABLEStbl_name,ALTERTABLE,RENAMETABLE,REPAIRTABLE,ANALYZETABLE,或OPTIMIZETABLE。
waitingforhandlerinsert
 INSERTDELAYED已经处理完了所有待处理的插入操作,正在等待新的请求。
 大部分状态对应很快的操作,只要有一个线程保持同一个状态好几秒钟,那么可能是有问题发生了,需要检查一下。
 还有其他的状态没在上面中列出来,不过它们大部分只是在查看服务器是否有存在错误是才用得着。

mysql查看当前连接数

命令:showprocesslist; 
如果是root帐号,你能看到所有用户的当前连接。如果是其它普通帐号,只能看到自己占用的连接。 
showprocesslist;只列出前100条,如果想全列出请使用showfullprocesslist; 
mysql>showprocesslist;

命令:showstatus;

Aborted_clients由于客户没有正确关闭连接已经死掉,已经放弃的连接数量。 
Aborted_connects尝试已经失败的MySQL服务器的连接的次数。 
Connections试图连接MySQL服务器的次数。 
Created_tmp_tables当执行语句时,已经被创造了的隐含临时表的数量。 
Delayed_insert_threads正在使用的延迟插入处理器线程的数量。 
Delayed_writes用INSERTDELAYED写入的行数。 
Delayed_errors用INSERTDELAYED写入的发生某些错误(可能重复键值)的行数。 
Flush_commands执行FLUSH命令的次数。 
Handler_delete请求从一张表中删除行的次数。 
Handler_read_first请求读入表中第一行的次数。 
Handler_read_key请求数字基于键读行。 
Handler_read_next请求读入基于一个键的一行的次数。 
Handler_read_rnd请求读入基于一个固定位置的一行的次数。 
Handler_update请求更新表中一行的次数。 
Handler_write请求向表中插入一行的次数。 
Key_blocks_used用于关键字缓存的块的数量。 
Key_read_requests请求从缓存读入一个键值的次数。 
Key_reads从磁盘物理读入一个键值的次数。 
Key_write_requests请求将一个关键字块写入缓存次数。 
Key_writes将一个键值块物理写入磁盘的次数。 
Max_used_connections同时使用的连接的最大数目。 
Not_flushed_key_blocks在键缓存中已经改变但是还没被清空到磁盘上的键块。 
Not_flushed_delayed_rows在INSERTDELAY队列中等待写入的行的数量。 
Open_tables打开表的数量。 
Open_files打开文件的数量。 
Open_streams打开流的数量(主要用于日志记载) 
Opened_tables已经打开的表的数量。 
Questions发往服务器的查询的数量。 
Slow_queries要花超过long_query_time时间的查询数量。 
Threads_connected当前打开的连接的数量。 
Threads_running不在睡眠的线程数量。 
Uptime服务器工作了多少秒。

Aftercreate

Thisoccurswhenthethreadcreatesatable(includinginternaltemporarytables),attheendofthefunctionthatcreatesthetable.Thisstateisusedevenifthetablecouldnotbecreatedduetosomeerror.

Analyzing

ThethreadiscalculatingaMyISAMtablekeydistributions(forexample,forANALYZETABLE).

checkingpermissions

Thethreadischeckingwhethertheserverhastherequiredprivilegestoexecutethestatement.

Checkingtable

Thethreadisperformingatablecheckoperation.

cleaningup

Thethreadhasprocessedonecommandandispreparingtofreememoryandresetcertainstatevariables.

closingtables

Thethreadisflushingthechangedtabledatatodiskandclosingtheusedtables.Thisshouldbeafastoperation.Ifnot,youshouldverifythatyoudonothaveafulldiskandthatthediskisnotinveryheavyuse.

convertingHEAPtoMyISAM

ThethreadisconvertinganinternaltemporarytablefromaMEMORYtabletoanon-diskMyISAMtable.

copytotmptable

ThethreadisprocessinganALTERTABLEstatement.Thisstateoccursafterthetablewiththenewstructurehasbeencreatedbutbeforerowsarecopiedintoit.

Copyingtogrouptable

IfastatementhasdifferentORDERBYandGROUPBYcriteria,therowsaresortedbygroupandcopiedtoatemporarytable.

Copyingtotmptable

Theserveriscopyingtoatemporarytableinmemory.

Copyingtotmptableondisk

Theserveriscopyingtoatemporarytableondisk.Thetemporaryresultsetwaslargerthantmp_table_sizeandthethreadischangingthetemporarytablefromin-memorytodisk-basedformattosavememory.

Creatingindex

ThethreadisprocessingALTERTABLE...ENABLEKEYSforaMyISAMtable.

Creatingsortindex

ThethreadisprocessingaSELECTthatisresolvedusinganinternaltemporarytable.

creatingtable

Thethreadiscreatingatable.Thisincludescreationoftemporarytables.

Creatingtmptable

Thethreadiscreatingatemporarytableinmemoryorondisk.Ifthetableiscreatedinmemorybutlaterisconvertedtoanon-disktable,thestateduringthatoperationwillbeCopyingtotmptableondisk.

deletingfrommaintable

Theserverisexecutingthefirstpartofamultiple-tabledelete.Itisdeletingonlyfromthefirsttable,andsavingcolumnsandoffsetstobeusedfordeletingfromtheother(reference)tables.

deletingfromreferencetables

Theserverisexecutingthesecondpartofamultiple-tabledeleteanddeletingthematchedrowsfromtheothertables.

discard_or_import_tablespace

ThethreadisprocessinganALTERTABLE...DISCARDTABLESPACEorALTERTABLE...IMPORTTABLESPACEstatement.

end

ThisoccursattheendbutbeforethecleanupofALTERTABLE,CREATEVIEW,DELETE,INSERT,SELECT,orUPDATEstatements.

executing

Thethreadhasbegunexecutingastatement.

Executionofinit_command

Thethreadisexecutingstatementsinthevalueoftheinit_commandsystemvariable.

freeingitems

Thethreadhasexecutedacommand.Thisstateisusuallyfollowedbycleaningup.

Flushingtables

ThethreadisexecutingFLUSHTABLESandiswaitingforallthreadstoclosetheirtables.

FULLTEXTinitialization

Theserverispreparingtoperformanatural-languagefull-textsearch.

init

ThisoccursbeforetheinitializationofALTERTABLE,DELETE,INSERT,SELECT,orUPDATEstatements.

Killed

SomeonehassentaKILLstatementtothethreadanditshouldabortnexttimeitchecksthekillflag.TheflagischeckedineachmajorloopinMySQL,butinsomecasesitmightstilltakeashorttimeforthethreadtodie.Ifthethreadislockedbysomeotherthread,thekilltakeseffectassoonastheotherthreadreleasesitslock.

Locked

Thequeryislockedbyanotherquery.

loggingslowquery

Thethreadiswritingastatementtotheslow-querylog.

NULL

ThisstateisusedfortheSHOWPROCESSLISTstate.

login

Theinitialstateforaconnectionthreaduntiltheclienthasbeenauthenticatedsuccessfully.

Openingtables,Openingtable

Thethreadistryingtoopenatable.Thisisshouldbeveryfastprocedure,unlesssomethingpreventsopening.Forexample,anALTERTABLEoraLOCKTABLEstatementcanpreventopeningatableuntilthestatementisfinished.

preparing

Thisstateoccursduringqueryoptimization.

Purgingoldrelaylogs

Thethreadisremovingunneededrelaylogfiles.

queryend

Thisstateoccursafterprocessingaquerybutbeforethefreeingitemsstate.

Readingfromnet

Theserverisreadingapacketfromthenetwork.

Removingduplicates

ThequerywasusingSELECTDISTINCTinsuchawaythatMySQLcouldnotoptimizeawaythedistinctoperationatanearlystage.Becauseofthis,MySQLrequiresanextrastagetoremoveallduplicatedrowsbeforesendingtheresulttotheclient.

removingtmptable

ThethreadisremovinganinternaltemporarytableafterprocessingaSELECTstatement.Thisstateisnotusedifnotemporarytablewascreated.

rename

Thethreadisrenamingatable.

renameresulttable

ThethreadisprocessinganALTERTABLEstatement,hascreatedthenewtable,andisrenamingittoreplacetheoriginaltable.

Reopentables

Thethreadgotalockforthetable,butnoticedaftergettingthelockthattheunderlyingtablestructurechanged.Ithasfreedthelock,closedthetable,andistryingtoreopenit.

Repairbysorting

Therepaircodeisusingasorttocreateindexes.

Repairdone

Thethreadhascompletedamulti-threadedrepairforaMyISAMtable.

Repairwithkeycache

Therepaircodeisusingcreatingkeysonebyonethroughthekeycache.ThisismuchslowerthanRepairbysorting.

Rollingback

Thethreadisrollingbackatransaction.

Savingstate

ForMyISAMtableoperationssuchasrepairoranalysis,thethreadissavingthenewtablestatetothe.MYIfileheader.Stateincludesinformationsuchasnumberofrows,theAUTO_INCREMENTcounter,andkeydistributions.

Searchingrowsforupdate

Thethreadisdoingafirstphasetofindallmatchingrowsbeforeupdatingthem.ThishastobedoneiftheUPDATEischangingtheindexthatisusedtofindtheinvolvedrows.

Sendingdata

ThethreadisprocessingrowsforaSELECTstatementandalsoissendingdatatotheclient.

setup

ThethreadisbeginninganALTERTABLEoperation.

Sortingforgroup

ThethreadisdoingasorttosatisfyaGROUPBY.

Sortingfororder

ThethreadisdoingasorttosatisfyaORDERBY.

Sortingindex

ThethreadissortingindexpagesformoreefficientaccessduringaMyISAMtableoptimizationoperation.

Sortingresult

ForaSELECTstatement,thisissimilartoCreatingsortindex,butfornontemporarytables.

statistics

Theserveriscalculatingstatisticstodevelopaqueryexecutionplan.

Systemlock

Thethreadisgoingtorequestoriswaitingforaninternalorexternalsystemlockforthetable.Ifthisstateisbeingcausedbyrequestsforexternallocksandyouarenotusingmultiplemysqldserversthatareaccessingthesametables,youcandisableexternalsystemlockswiththe--skip-external-lockingoption.However,externallockingisdisabledbydefault,soitislikelythatthisoptionwillhavenoeffect.ForSHOWPROFILE,thisstatemeansthethreadisrequestingthelock(notwaitingforit).

Tablelock

ThenextthreadstateafterSystemlock.Thethreadhasacquiredanexternallockandisgoingtorequestaninternaltablelock.

Updating

Thethreadissearchingforrowstoupdateandisupdatingthem.

updatingmaintable

Theserverisexecutingthefirstpartofamultiple-tableupdate.Itisupdatingonlythefirsttable,andsavingcolumnsandoffsetstobeusedforupdatingtheother(reference)tables.

updatingreferencetables

Theserverisexecutingthesecondpartofamultiple-tableupdateandupdatingthematchedrowsfromtheothertables.

Userlock

ThethreadisgoingtorequestoriswaitingforanadvisorylockrequestedwithaGET_LOCK()call.ForSHOWPROFILE,thisstatemeansthethreadisrequestingthelock(notwaitingforit).

Waitingfortables,Waitingfortable

Thethreadgotanotificationthattheunderlyingstructureforatablehaschangedanditneedstoreopenthetabletogetthenewstructure.However,toreopenthetable,itmustwaituntilallotherthreadshaveclosedthetableinquestion.

ThisnotificationtakesplaceifanotherthreadhasusedFLUSHTABLESoroneofthefollowingstatementsonthetableinquestion:FLUSHTABLEStbl_name,ALTERTABLE,RENAMETABLE,REPAIRTABLE,ANALYZETABLE,orOPTIMIZETABLE.

Waitingoncond

Agenericstateinwhichthethreadiswaitingforaconditiontobecometrue.Nospecificstateinformationisavailable.

Writingtonet

Theserveriswritingapackettothenetwork.