自己寫的殺mysql執行緒的指令碼

czxin788發表於2018-04-27

功能:該指令碼可以根據使用者和型別殺死mysql執行緒。

[root@MySQL02 ~]# cat kill_sql.py 

點選(此處)摺疊或開啟

  1. #!/usr/bin/env python
  2. #-*-coding:utf8-*-
  3. #python版本2.7
  4. #安裝mysql-connector-python,版本需要是1.1.17的,2.x的版本執行會有問題,下載地址:https://dev.mysql.com/downloads/connector/python/
  5. #在mysql資料庫裡面建立檢視,create view v_process_czxin as select id,user,host,db,info from information_schema.PROCESSLIST where info is not null order by time desc ;


  6. from __future__ import print_function
  7. import os,sys
  8. import subprocess
  9. import mysql.connector as mdb

  10. ###全域性變數##########
  11. username = 'root'
  12. password = 'xxx'
  13. hostname = 'localhost'
  14. ####################

  15. config = {
  16.     'user':username,
  17.     'password':password,
  18.     'host':hostname,
  19.     'database':'information_schema'
  20. }


  21. def GetUserInfo():
  22.     show_processinfo = "SELECT \
  23.     USER, \
  24.     count(*) total_count, \
  25.     ( \
  26.         SELECT \
  27.             count(info) \
  28.         FROM \
  29.             mysql.v_process_czxin \
  30.         WHERE \
  31.             info LIKE 'select%' \
  32.         AND USER = p. USER \
  33.     ) select_count, \
  34. ( \
  35.         SELECT \
  36.             count(info) \
  37.         FROM \
  38.             mysql.v_process_czxin \
  39.         WHERE \
  40.             info LIKE 'update%' \
  41.         AND USER = p. USER \
  42.     ) update_count, \
  43. ( \
  44.         SELECT \
  45.             count(info) \
  46.         FROM \
  47.             mysql.v_process_czxin \
  48.         WHERE \
  49.             info LIKE 'delete%' \
  50.         AND USER = p. USER \
  51.     ) delete_count, \
  52. ( \
  53.         SELECT \
  54.             count(info) \
  55.         FROM \
  56.             mysql.v_process_czxin \
  57.         WHERE \
  58.             info LIKE 'insert%' \
  59.         AND USER = p. USER \
  60.     ) insert_count, \
  61. ( \
  62.         SELECT \
  63.             count(info) \
  64.         FROM \
  65.             mysql.v_process_czxin \
  66.         WHERE \
  67.             info LIKE 'alter%' \
  68.         AND USER = p. USER \
  69.     ) alter_count \
  70. FROM \
  71.     mysql.v_process_czxin p \
  72. GROUP BY \
  73.     USER ORDER BY total_count DESC \
  74. "
  75.     cursor.execute(show_processinfo)
  76.     print('############## 摘要 ###############')
  77.     for i in cursor:
  78.         user = i[0]
  79.         total_count = i[1]
  80.         select_count = i[2]
  81.         update_count = i[3]
  82.         delete_count = i[4]
  83.         insert_count = i[5]
  84.         alter_count = i[6]
  85.         print('{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}'.format('使用者',user,'線上的匯流排程數是',total_count,', 其中select:',select_count,' update:',update_count,' delete:',delete_count,' insert:',insert_count,' alter:',alter_count))

  86. def KillSQL():
  87.     while True:
  88.         print('\n')
  89.         temp = str(raw_input('[1] 請問你要殺死的使用者名稱是: '))
  90.             input_user = temp.strip().lower()    
  91.         temp = str(raw_input('[2] 請問你要殺死的sql型別是:[select%/update%/delete%/insert%/alter%~~注意~~:必須加%]: '))
  92.         input_type = temp.strip().lower()
  93.         show_process = """select id,user,host,db,time,info from information_schema.processlist where user='%s' and info like '%s'""" % (input_user,input_type)
  94.         cursor.execute(show_process)
  95.         #如果日誌檔案out.txt存在,就刪除
  96.                 if os.path.isfile('out.txt'):
  97.                         #subprocess.call(["rm","-rf","out.txt"])
  98.             os.remove('out.txt')
  99.          count =0
  100.         for j in cursor:
  101.             id = j[0]
  102.             user = j[1]
  103.             host = j[2]
  104.             db = j[3]
  105.             time = j [4]
  106.             info = j [5]
  107.             print('{0}{1}{2}{3}'.format('殺死',user,'的執行緒id為:',id))
  108.             #真正的殺死sql id
  109.             #import pdb;pdb.set_trace()
  110.             kill_sql = "kill %s" % id
  111.             subprocess.call(["mysql","-h%s" % hostname,'-u%s' % username,'-p%s' % password,'-e',kill_sql])
  112.             #寫入日誌檔案
  113.             try:
  114.                 count += 1
  115.                 f = open('out.txt','a') #a表示追加寫入
  116.                 f.write('####################'+'The '+ str(count) + ' rows' + '#########################\n')
  117.                 f.write(
  118.                     'id: '+ str(id) + '\n'
  119.                     + 'user: '+user + '\n'
  120.                     + 'host: ' + host + '\n'
  121.                     + 'db: ' + db + '\n'
  122.                     + 'exec_time: ' + str(time) + '\n'
  123.                     + 'killed_sql: ' + info + '\n')
  124.                 f.write('\n')
  125.                 f.write('\n')
  126.             except OSError as reason:
  127.                 print('出錯了:'+str(reason))
  128.             finally:
  129.                 f.close
  130.         break
  131.     print('\n')
  132.     print('\n')
  133.     print('詳細資訊請見當前目錄下的out.txt檔案,裡面有被殺死sql語句的詳細資訊!!!')

  134. #主程式
  135. conn = mdb.connect(**config)
  136. cursor = conn.cursor()
  137. GetUserInfo()
  138. KillSQL()
  139. cursor.close()
  140. conn.close()
執行結果:

點選(此處)摺疊或開啟

  1. [root@MySQL02 ~]# python kill_sql.py
  2. ############## 摘要 ###############
  3. 使用者ygjt_new線上的匯流排程數是3, 其中select:2 update:1 delete:0 insert:0 alter:0
  4. 使用者auod_oms線上的匯流排程數是2, 其中select:2 update:0 delete:0 insert:0 alter:0
  5. 使用者auod線上的匯流排程數是1, 其中select:1 update:0 delete:0 insert:0 alter:0
  6. 使用者root線上的匯流排程數是1, 其中select:1 update:0 delete:0 insert:0 alter:0


  7. [1] 請問你要殺死的使用者名稱是: ygjt_new
  8. [2] 請問你要殺死的sql型別是:[select%/update%/delete%/insert%/alter%~~注意~~:必須加%]: select%
  9. 殺死ygjt_new的執行緒id為:14938801
  10. Warning: Using a password on the command line interface can be insecure.
  11. 殺死ygjt_new的執行緒id為:14938803
  12. Warning: Using a password on the command line interface can be insecure.
  13. 殺死ygjt_new的執行緒id為:14931430
  14. Warning: Using a password on the command line interface can be insecure.


  15. 詳細資訊請見當前目錄下的out.

報告內容:

點選(此處)摺疊或開啟

  1. [root@MySQL02 ~]# cat out.txt
  2. ####################The 1 rows#########################
  3. id: 14938801
  4. user: ygjt_new
  5. host: 172.19.2.48:59170
  6. db: cz_bj_oms
  7. exec_time: 5
  8. killed_sql: SELECT
  9.         STAT,ORDER_ID
  10.     FROM DECLAREBILL
  11.     WHERE ORDER_ID IN
  12.      ( '34933646-765f-44b0-8567-a88f44d6d3d9' )


  13. ####################The 2 rows#########################
  14. id: 14938803
  15. user: ygjt_new
  16. host: 172.19.2.48:59172
  17. db: cz_bj_oms
  18. exec_time: 23
  19. killed_sql: SELECT
  20.         STAT,ORDER_ID
  21.     FROM DECLAREBILL
  22.     WHERE ORDER_ID IN
  23.      ( '34933646-765f-44b0-8567-a88f44d6d3d9' )


  24. ####################The 3 rows#########################




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28916011/viewspace-2153422/,如需轉載,請註明出處,否則將追究法律責任。

相關文章