這裏是什麼,我已經嘗試了總結:如何使用Python殺死睡眠的MySQL連接?
您可以在一個shell腳本做到這一點,但如果您的服務器啓用--secure-file-priv
選項運行,您將無法做到這一點。我不想禁用它。
儘管爲了完全公平,可以使用--secure-file-priv導出到某個目錄。你可以做一個
show global variables like '%secure%';
你會得到允許你將MySQL結果集直接寫入文件的路徑。
1.外殼腳本需要--secure-文件PRIV禁用
mysql -e "select concat('KILL ',id,';') from information_schema.processlist where Command = 'Sleep' AND user = 'root' into outfile '/tmp/sleep_processes.txt'"
mysql -e "source /tmp/sleep_processes.txt;"
rm -rf /tmp/sleep_processes.txt
2.的Percona的PT-殺
殺睡連接使用的Percona的pt-kill
pt-kill --match-command Sleep --kill --victims all --interval 10
3.帶腳本的Python腳本as
一個Python腳本,列出所有在熊貓框架中被殺死的查詢。但是,我如何在熊貓框架中執行每個命令。
import MySQLdb as mdb
import pandas as pd
con = mdb.connect('localhost', 'root', 'password', 'mydatabase');
with con:
cur = con.cursor()
cur.execute("select concat('kill ',id,';') sleeping_connections FROM information_schema.processlist isp WHERE Command = 'Sleep' and time > 60")
rows = cur.fetchall()
df = pd.DataFrame([[ij for ij in i] for i in rows])
df.rename(columns={0: 'Sleeping Connections'}, inplace=True);
print(df.head(10))
@jezrael - FYI。 – MontyPython
你有沒有考慮在MySQL系統變量中設置'wait_timeout'? –
@TanHongTat - 是的,但這是永久的。如果我將該設置設置得太低,連接到MySQL的應用程序就會產生池化問題。但是,這個cron工作是一個臨時解決方案。 – MontyPython