自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL查看及殺掉鏈接方法大全

數(shù)據(jù)庫 MySQL
在數(shù)據(jù)庫運維過程中,我們時常會關(guān)注數(shù)據(jù)庫的鏈接情況,比如總共有多少鏈接、有多少活躍鏈接、有沒有執(zhí)行時間過長的鏈接等。

 前言:

在數(shù)據(jù)庫運維過程中,我們時常會關(guān)注數(shù)據(jù)庫的鏈接情況,比如總共有多少鏈接、有多少活躍鏈接、有沒有執(zhí)行時間過長的鏈接等。數(shù)據(jù)庫的各種異常也能通過鏈接情況間接反應(yīng)出來,特別是數(shù)據(jù)庫出現(xiàn)死鎖或嚴重卡頓的時候,我們首先應(yīng)該查看數(shù)據(jù)庫是否有異常鏈接,并殺掉這些異常鏈接。本篇文章將主要介紹如何查看數(shù)據(jù)庫鏈接及如何殺掉異常鏈接的方法。

[[381236]]

1.查看數(shù)據(jù)庫鏈接

查看數(shù)據(jù)庫鏈接最常用的語句就是 show processlist 了,這條語句可以查看數(shù)據(jù)庫中存在的線程狀態(tài)。普通用戶只可以查看當前用戶發(fā)起的鏈接,具有 PROCESS 全局權(quán)限的用戶則可以查看所有用戶的鏈接。

show processlist 結(jié)果中的 Info 字段僅顯示每個語句的前 100 個字符,如果需要顯示更多信息,可以使用 show full processlist 。同樣的,查看 information_schema.processlist 表也可以看到數(shù)據(jù)庫鏈接狀態(tài)信息。

 

  1. # 普通用戶只能看到當前用戶發(fā)起的鏈接 
  2. mysql> select user(); 
  3. +--------------------+ 
  4. user()             | 
  5. +--------------------+ 
  6. | testuser@localhost | 
  7. +--------------------+ 
  8. 1 row in set (0.00 sec) 
  9.  
  10. mysql> show grants; 
  11. +----------------------------------------------------------------------+ 
  12. | Grants for testuser@%                                                | 
  13. +----------------------------------------------------------------------+ 
  14. GRANT USAGE ON *.* TO 'testuser'@'%'                                 | 
  15. GRANT SELECTINSERTUPDATEDELETE ON `testdb`.* TO 'testuser'@'%' | 
  16. +----------------------------------------------------------------------+ 
  17. rows in set (0.00 sec) 
  18.  
  19. mysql> show processlist; 
  20. +--------+----------+-----------+--------+---------+------+----------+------------------+ 
  21. | Id     | User     | Host      | db     | Command | Time | State    | Info             | 
  22. +--------+----------+-----------+--------+---------+------+----------+------------------+ 
  23. | 769386 | testuser | localhost | NULL   | Sleep   |  201 |          | NULL             | 
  24. | 769390 | testuser | localhost | testdb | Query   |    0 | starting | show processlist | 
  25. +--------+----------+-----------+--------+---------+------+----------+------------------+ 
  26. rows in set (0.00 sec) 
  27.  
  28. mysql> select * from information_schema.processlist; 
  29. +--------+----------+-----------+--------+---------+------+-----------+----------------------------------------------+ 
  30. | ID     | USER     | HOST      | DB     | COMMAND | TIME | STATE     | INFO                                         | 
  31. +--------+----------+-----------+--------+---------+------+-----------+----------------------------------------------+ 
  32. | 769386 | testuser | localhost | NULL   | Sleep   |  210 |           | NULL                                         | 
  33. | 769390 | testuser | localhost | testdb | Query   |    0 | executing | select * from information_schema.processlist | 
  34. +--------+----------+-----------+--------+---------+------+-----------+----------------------------------------------+ 
  35. rows in set (0.00 sec) 
  36.  
  37. # 授予了PROCESS權(quán)限后,可以看到所有用戶的鏈接 
  38. mysql> grant process on *.* to 'testuser'@'%'
  39. Query OK, 0 rows affected (0.01 sec) 
  40.  
  41. mysql> flush privileges
  42. Query OK, 0 rows affected (0.00 sec) 
  43.  
  44. mysql> show grants; 
  45. +----------------------------------------------------------------------+ 
  46. | Grants for testuser@%                                                | 
  47. +----------------------------------------------------------------------+ 
  48. GRANT PROCESS ON *.* TO 'testuser'@'%'                               | 
  49. GRANT SELECTINSERTUPDATEDELETE ON `testdb`.* TO 'testuser'@'%' | 
  50. +----------------------------------------------------------------------+ 
  51. rows in set (0.00 sec) 
  52.  
  53. mysql> show processlist; 
  54. +--------+----------+--------------------+--------+---------+------+----------+------------------+ 
  55. | Id     | User     | Host               | db     | Command | Time | State    | Info             | 
  56. +--------+----------+--------------------+--------+---------+------+----------+------------------+ 
  57. | 769347 | root     | localhost          | testdb | Sleep   |   53 |          | NULL             | 
  58. | 769357 | root     | 192.168.85.0:61709 | NULL   | Sleep   |  521 |          | NULL             | 
  59. | 769386 | testuser | localhost          | NULL   | Sleep   |  406 |          | NULL             | 
  60. | 769473 | testuser | localhost          | testdb | Query   |    0 | starting | show processlist | 
  61. +--------+----------+--------------------+--------+---------+------+----------+------------------+ 
  62. rows in set (0.00 sec) 

通過 show processlist 所得結(jié)果,我們可以清晰了解各線程鏈接的詳細信息。具體字段含義還是比較容易理解的,下面具體來解釋下各個字段代表的意思:

  • Id:就是這個鏈接的唯一標識,可通過 kill 命令,加上這個Id值將此鏈接殺掉。
  • User:就是指發(fā)起這個鏈接的用戶名。
  • Host:記錄了發(fā)送請求的客戶端的 IP 和 端口號,可以定位到是哪個客戶端的哪個進程發(fā)送的請求。
  • db:當前執(zhí)行的命令是在哪一個數(shù)據(jù)庫上。如果沒有指定數(shù)據(jù)庫,則該值為 NULL 。
  • Command:是指此刻該線程鏈接正在執(zhí)行的命令。
  • Time:表示該線程鏈接處于當前狀態(tài)的時間。
  • State:線程的狀態(tài),和 Command 對應(yīng)。
  • Info:記錄的是線程執(zhí)行的具體語句。

當數(shù)據(jù)庫鏈接數(shù)過多時,篩選有用信息又成了一件麻煩事,比如我們只想查某個用戶或某個狀態(tài)的鏈接。這個時候用 show processlist 則會查找出一些我們不需要的信息,此時使用 information_schema.processlist 進行篩選會變得容易許多,下面展示幾個常見篩選需求:

 

  1. # 只查看某個ID的鏈接信息 
  2. select * from information_schema.processlist where id = 705207; 
  3.  
  4. # 篩選出某個用戶的鏈接 
  5. select * from information_schema.processlist where user = 'testuser'
  6.  
  7. # 篩選出所有非空閑的鏈接 
  8. select * from information_schema.processlist where command != 'Sleep'
  9.  
  10. # 篩選出空閑時間在600秒以上的鏈接 
  11. select * from information_schema.processlist where command = 'Sleep' and time > 600; 
  12.  
  13. # 篩選出處于某個狀態(tài)的鏈接 
  14. select * from information_schema.processlist where state = 'Sending data'
  15.  
  16. # 篩選某個客戶端IP的鏈接 
  17. select * from information_schema.processlist where host like '192.168.85.0%'

2.殺掉數(shù)據(jù)庫鏈接

如果某個數(shù)據(jù)庫鏈接異常,我們可以通過 kill 語句來殺掉該鏈接,kill 標準語法是:KILL [CONNECTION | QUERY] processlist_id;

KILL 允許使用可選的 CONNECTION 或 QUERY 修飾符:

  • KILL CONNECTION 與不含修改符的 KILL 一樣,它會終止該 process 相關(guān)鏈接。
  • KILL QUERY 終止鏈接當前正在執(zhí)行的語句,但保持鏈接本身不變。

殺掉鏈接的能力取決于 SUPER 權(quán)限:

  • 如果沒有 SUPER 權(quán)限,則只能殺掉當前用戶發(fā)起的鏈接。
  • 具有 SUPER 權(quán)限的用戶,可以殺掉所有鏈接。

遇到突發(fā)情況,需要批量殺鏈接時,可以通過拼接 SQL 得到 kill 語句,然后再執(zhí)行,這樣會方便很多,分享幾個可能用到的殺鏈接的 SQL :

 

  1. # 殺掉空閑時間在600秒以上的鏈接,拼接得到kill語句 
  2. select concat('KILL ',id,';'from information_schema.`processlist`  
  3. where command = 'Sleep' and time > 600; 
  4.  
  5. # 殺掉處于某個狀態(tài)的鏈接,拼接得到kill語句 
  6. select concat('KILL ',id,';'from information_schema.`processlist`  
  7. where state = 'Sending data'
  8.  
  9. select concat('KILL ',id,';'from information_schema.`processlist`  
  10. where state = 'Waiting for table metadata lock'
  11.  
  12. # 殺掉某個用戶發(fā)起的鏈接,拼接得到kill語句 
  13. select concat('KILL ',id,';'from information_schema.`processlist`  
  14.  user = 'testuser'

這里提醒下,kill 語句一定要慎用!特別是此鏈接執(zhí)行的是更新語句或表結(jié)構(gòu)變動語句時,殺掉鏈接可能需要比較長時間的回滾操作。

總結(jié):

本篇文章講解了查看及殺掉數(shù)據(jù)庫鏈接的方法,以后懷疑數(shù)據(jù)庫有問題,可以第一時間看下數(shù)據(jù)庫鏈接情況。

責任編輯:華軒 來源: MySQL技術(shù)
相關(guān)推薦

2009-11-13 11:04:46

寬帶路由器故障

2010-05-27 10:00:34

Linux 網(wǎng)卡流量

2016-10-25 14:47:29

Windows隱藏鏈接lynx

2019-11-15 10:01:07

MySQL數(shù)據(jù)庫數(shù)據(jù)

2009-06-04 20:41:27

Eclipse插件介紹Eclipse插件下載

2023-03-05 16:36:14

Linux鏈接目標文件

2009-10-20 12:57:35

綜合布線產(chǎn)品

2010-05-27 09:33:04

SVN沖突

2010-05-31 18:16:48

MySQL數(shù)據(jù)庫備份

2011-07-18 15:59:17

MySQL數(shù)據(jù)庫

2010-05-18 18:51:02

MySQL SELEC

2010-10-13 15:45:23

MySQL二進制日志

2010-05-25 13:34:18

MySQL命令

2010-05-18 13:52:49

MySQL selec

2011-08-24 17:41:16

MySQL死鎖

2009-12-21 18:32:22

關(guān)閉WCF鏈接

2016-09-29 15:31:20

windows 10微軟

2024-04-11 13:41:47

2010-10-09 11:36:30

MySQL字符集

2012-05-08 14:34:19

Nginx
點贊
收藏

51CTO技術(shù)棧公眾號