pt-kill 是一個(gè)非常簡(jiǎn)單的 殺mysql線程和查詢的 工具。 主要是為了防止一些長(zhǎng)的查詢 長(zhǎng)時(shí)間占用 系統(tǒng)資源,而對(duì)線上業(yè)務(wù)造成影響的情況。
主要作用:
從show processlist 中獲取滿足條件的連接或者從包含show processlist的文件中讀取滿足條件的連接并打印或者殺掉或者執(zhí)行其他操作。
我們這里主要用來(lái)防止某些select操作時(shí)間過(guò)長(zhǎng),從而影響其他線上SQL。
安裝:
安裝percona-toolkit即可
使用范例:
pt-kill --log-dsn D=testdb,t=kill_log --create-log-table --host=host2 --user=root --password=root --port=3306 --busy-time=10 --print --kill-query --match-info "SELECT|select" --victims all
也可使用--config寫配置文件:
pt-kill --config tmp.txt --log-dsn D=testdb,t=kill_log --create-log-table --match-info "SELECT|select" --victims all
notes: --match-info 是區(qū)分大小寫的,匹配SELECT也就意味著對(duì)select無(wú)法匹配,因此使用"SELECT|select"包含大小寫
cat tmp.txt
host=host2
user=root
password=root
port=3306
busy-time=10
print
kill-query
該使用范例的作用:
如果不存在testdb.kill_log表,則創(chuàng)建該表,然后將所有pt-kill的操作記錄到該表中。對(duì)所有查詢時(shí)間超過(guò)10秒的SELECT語(yǔ)句進(jìn)行print顯示出來(lái),同時(shí)會(huì)kill該query。
pt-kill 默認(rèn)檢查間隔為5秒
解釋:
--log-dsn D=testdb,t=kill_log --create-log-table 是創(chuàng)建testdb.kill_log表,之后將pt-kill操作的日志記錄在表中
--busy-time=10 執(zhí)行時(shí)間超過(guò)10秒的
--print --kill-query 動(dòng)作是 進(jìn)行print和 kill query,除此之外的動(dòng)作還有kill 連接:--kill
--match-info 'SELECT|select' 只匹配SELECT 語(yǔ)句
其他一些重要的參數(shù)解釋:
--interval 運(yùn)行檢查query的間隔,
--victim
oldest (默認(rèn)值),只殺最老的查詢(最先發(fā)起的)。這是防止被查殺是不是真的長(zhǎng)時(shí)間運(yùn)行的查詢,他們只是長(zhǎng)期等待。這種種匹配按時(shí)間查詢,殺死一個(gè)時(shí)間最高值。
all 殺掉所有滿足的線程
all-but-oldest 殺死所有,但最長(zhǎng)的保留不殺
--busy-time 批次查詢已運(yùn)行的時(shí)間超過(guò)這個(gè)時(shí)間的線程;
--idle-time 殺掉sleep 了多少時(shí)間的連接線程,必須在--match-command sleep時(shí)才有效
接下來(lái)這一組參數(shù),是針對(duì)show processlist中相應(yīng)列的 忽略 或 匹配:
--ignore-command / --match-command
(command有:Query、Sleep、Binlog Dump、Connect、Delayed insert、Execute、Fetch、Init DB、Kill、Prepare、Processlist、Quit、Reset stmt、Table Dump)
--ignore-db / --match-db
--ignore-host / --match-host
--ignore-info / --match-info
(info則可以使用select、update、insert、delete來(lái)進(jìn)行匹配,并可使用"|"進(jìn)行多項(xiàng)匹配,如"select|SELECT|delete|DELETE|update|UPDATE"
--ignore-state / --match-state
(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)
--ignore-user / --match-user
Action:
--kill 殺掉連接并且退出
--kill-query 只殺掉連接執(zhí)行的語(yǔ)句,但是線程不會(huì)被終止
--print 打印滿足條件的語(yǔ)句
使用感受:
本來(lái)是想用該工具 監(jiān)控多個(gè)mysql實(shí)例 ,但是發(fā)現(xiàn)--host 只能填寫一個(gè),多寫了也沒(méi)作用。 所以 這個(gè)工具 起一個(gè)進(jìn)程后,只能監(jiān)控一個(gè)mysql實(shí)例, 這點(diǎn)有點(diǎn)不是太方便。個(gè)人感覺(jué)應(yīng)該有方法能夠?qū)崿F(xiàn) 多實(shí)例監(jiān)控和查殺的。 這里就沒(méi)做深入研究了, 有知道的朋友,請(qǐng)留言哦。