Mysql pt工具pt-kill怎么用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

pt-kill

概述

从show processlist 中获取满足条件的连接或者从包含show processlist的文件中读取满足条件的连接并打印或者杀掉或者执行其他操作,这个工具在工作中实用性很高,当服务器连接出现异常后第一想到的就是pt-kill,自己写一个脚本也可以,但是功能还是没有pt-kill强大;

常用功能介绍


[root@hostnfsd :/root]$ pt-kill --defaults-file=/data/mydata/my_3307.cnf -uroot -p`cat /etc/sqlpass` --busy-time 60 --kill --victims all --interval 10 每隔10s 杀掉处于runnning状态超过60s的连接数;

[root@hostnfsd :/root]$ pt-kill --defaults-file=/data/mydata/my_3307.cnf -uroot -p`cat /etc/sqlpass` --match-command Sleep --kill --victims all --interval 10 每隔10s 杀掉处于sleep状态的连接数;

[root@hostnfsd :/root]$ pt-kill --defaults-file=/data/mydata/my_3307.cnf -uroot -p`cat /etc/sqlpass` --idle-time 60 --kill --victims all --interval 10 每隔10s 杀掉已经sleep达到60秒的连接数;

匹配状态stat处于sendind data的
[root@HaoDai_App_DB01 toolsqldir]# pt-kill --defaults-file=/data/mydata/my_3307.cnf -uroot -p`cat /etc/sqlpass` --match-state Sending --kill --victims all --interval 5

参数

–busy-time

运行时间

–idle-time

空闲时间

–victims

所有匹配的连接,对应有最久的连接

–interval

间隔时间,默认30s,有点长,可以根据实际情况来调节

–print

打印出来kill掉的连接


–match-command

匹配当前连接的命令


Query

Sleep

Binlog Dump

Connect

Delayed insert

Execute

Fetch

Init DB

Kill

Prepare

Processlist

Quit

Reset stmt

Table Dump

–match-state

匹配当前连接的状态

Locked

login

copy to tmp table

Copying to tmp table

Copying to tmp table on disk

Creating tmp table

executing

Reading from net

Sending data

Sorting for order

Sorting result

Table lock

Updating

–match-info

使用正则表达式匹配符合的sql

–match-db –match-user –match-host

见名知意

常用用法

杀掉空闲链接

pt-kill –match-command Sleep –idle-time 5 –host –port –interval –print –kill –victims all

杀掉运行时间超过5s的链接

pt-kill –match-command Query –busy-time 5 –host –port –interval –print –kill –victims all

杀掉匹配某个规则的正在运行的sql

pt-kill –match-command Query –busy-time 5 –host –port –interval –print –kill –victims all –match-info

杀掉正在进行filesort的sql

pt-kill –match-command Query –match-state “Sorting result” busy-time 5 –host –port –interval –print –kill –victims all

杀掉正在Copying to tmp table的sql

pt-kill –match-command Query –match-state “Copying to tmp table” busy-time 5 –host –port –interval –print –kill –victims all

几个重要参数

--daemonize放在后台以守护进程的形式运行;--interval多久运行一次,单位可以是s,m,h,d等默认是s--victims默认是oldest,只杀最古老的查询。这是防止被查杀是不是真的长时间运行的查询,他们只是长期等待。这种种匹配按时间查询,杀死一个时间最高值。all杀掉所有满足的线程杀死所有,但最长的保留不杀action--kill杀掉连接并且退出--kill-query只杀掉连接执行的语句,但是线程不会被终止--print打印满足条件的语句

QUERYMATCHES--busy-time批次查询已运行的时间超过这个时间的线程;--idle-time杀掉sleep了多少时间的连接线程,必须在--match-commandsleep时才有效opition查看参数吧Usage:pt-kill[OPTIONS][DSN]Options:--ask-passPromptforapasswordwhenconnectingtoMySQL--charset=s-ADefaultcharacterset--config=AReadthiscomma-separatedlistofconfigfiles;ifspecified,thismustbethefirstoptiononthecommandline--create-log-tableCreatethe--log-dsntableifitdoesnotexist--daemonizeForktothebackgroundanddetachfromtheshell--database=s-DThedatabasetousefortheconnection--defaults-file=s-FOnlyreadmysqloptionsfromthegivenfile--filter=sDiscardeventsforwhichthisPerlcodedoesn'treturntrue--group-by=sApplymatchestoeachclassofqueriesgroupedbythisSHOWPROCESSLISTcolumn--helpShowhelpandexit--host=s-hConnecttohost(defaultlocalhost)--interval=mHowoftentocheckforqueriestokill.Optionalsuffixs=seconds,m=minutes,h=hours,d=days;ifnosuffix,sisused.--log=sPrintalloutputtothisfilewhendaemonized--log-dsn=dStoreeachquerykilledinthisDSN--password=s-pPasswordtousewhenconnecting--pid=sCreatethegivenPIDfile--port=i-PPortnumbertouseforconnection--query-idPrintsanIDofthequerythatwasjustkilled--rdsDenotestheinstanceinquestionisonAmazonRDS--run-time=mHowlongtorunbeforeexiting.Optionalsuffixs=seconds,m=minutes,h=hours,d=days;ifnosuffix,sisused.--sentinel=sExitifthisfileexists(default/tmp/pt-kill-sentinel)--set-vars=ASettheMySQLvariablesinthiscomma-separatedlistofvariable=valuepairs--socket=s-SSocketfiletouseforconnection--stopStoprunninginstancesbycreatingthe--sentinelfile--[no]strip-commentsRemoveSQLcommentsfromqueriesintheInfocolumnofthePROCESSLIST(defaultyes)--user=s-uUserforloginifnotcurrentuser--verbose-vPrintinformationtoSTDOUTaboutwhatisbeingdone--versionShowversionandexit--[no]version-checkCheckforthelatestversionofPerconaToolkit,MySQL,andotherprograms(defaultyes)--victims=sWhichofthematchingqueriesineachclasswillbekilled(defaultoldest)--wait-after-kill=mWaitafterkillingaquery,beforelookingformoretokill.Optionalsuffixs=seconds,m=minutes,h=hours,d=days;ifnosuffix,sisused.--wait-before-kill=mWaitbeforekillingaquery.Optionalsuffixs=seconds,m=minutes,h=hours,d=days;ifnosuffix,sisused.Actions:--execute-command=sExecutethiscommandwhenaquerymatches--killKilltheconnectionformatchingqueries--kill-queryKillmatchingqueries--printPrintaKILLstatementformatchingqueries;doesnotactuallykillqueriesClassMatches:--any-busy-time=mMatchqueryclassifanyqueryhasbeenrunningforlongerthanthistime."Longerthan"meansthatifyouspecify10,forexample,theclasswillonlymatchifthere'satleastonequerythathasbeenrunningforgreaterthan10seconds.Optionalsuffixs=seconds,m=minutes,h=hours,d=days;ifnosuffix,sisused.--each-busy-time=mMatchqueryclassifeachqueryhasbeenrunningforlongerthanthistime."Longerthan"meansthatifyouspecify10,forexample,theclasswillonlymatchifeachandeveryqueryhasbeenrunningforgreaterthan10seconds.Optionalsuffixs=seconds,m=minutes,h=hours,d=days;ifnosuffix,sisused.--query-count=iMatchqueryclassifithasatleastthismanyqueriesQueryMatches:--busy-time=mMatchqueriesthathavebeenrunningforlongerthanthistime.Optionalsuffixs=seconds,m=minutes,h=hours,d=days;ifnosuffix,sisused.--idle-time=mMatchqueriesthathavebeenidle/sleepingforlongerthanthistime.Optionalsuffixs=seconds,m=minutes,h=hours,d=days;ifnosuffix,sisused.--ignore-command=sIgnorequerieswhoseCommandmatchesthisPerlregex--ignore-db=sIgnorequerieswhosedb(database)matchesthisPerlregex--ignore-host=sIgnorequerieswhoseHostmatchesthisPerlregex--ignore-info=sIgnorequerieswhoseInfo(query)matchesthisPerlregex--[no]ignore-selfDon'tkillpt-kill'sownconnection(defaultyes)--ignore-state=sIgnorequerieswhoseStatematchesthisPerlregex(defaultLocked)--ignore-user=sIgnorequerieswhoseusermatchesthisPerlregex--match-allMatchallqueriesthatarenotignored--match-command=sMatchonlyquerieswhoseCommandmatchesthisPerlregex--match-db=sMatchonlyquerieswhosedb(database)matchesthisPerlregex--match-host=sMatchonlyquerieswhoseHostmatchesthisPerlregex--match-info=sMatchonlyquerieswhoseInfo(query)matchesthisPerlregex--match-state=sMatchonlyquerieswhoseStatematchesthisPerlregex--match-user=sMatchonlyquerieswhoseUsermatchesthisPerlregex--replication-threadsAllowmatchingandkillingreplicationthreads--test-matching=aFileswithprocesslistsnapshotstotestmatchingoptionsagainst

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。