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


前言:

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

1.查看數據庫鏈接

查看數據庫鏈接最常用的語句就是 show processlist 了,這條語句可以查看數據庫中存在的線程狀態。普通用戶只可以查看當前用戶發起的鏈接,具有 PROCESS 全局權限的用戶則可以查看所有用戶的鏈接。

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

# 普通用戶只能看到當前用戶發起的鏈接
mysql> select user();
+--------------------+
| user()             |
+--------------------+
| testuser@localhost |
+--------------------+
1 row in set (0.00 sec)

mysql> show grants;
+----------------------------------------------------------------------+
| Grants for testuser@%                                                |
+----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'testuser'@'%'                                 |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `testdb`.* TO 'testuser'@'%' |
+----------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show processlist;
+--------+----------+-----------+--------+---------+------+----------+------------------+
| Id     | User     | Host      | db     | Command | Time | State    | Info             |
+--------+----------+-----------+--------+---------+------+----------+------------------+
| 769386 | testuser | localhost | NULL   | Sleep   |  201 |          | NULL             |
| 769390 | testuser | localhost | testdb | Query   |    0 | starting | show processlist |
+--------+----------+-----------+--------+---------+------+----------+------------------+
2 rows in set (0.00 sec)

mysql> select * from information_schema.processlist;
+--------+----------+-----------+--------+---------+------+-----------+----------------------------------------------+
| ID     | USER     | HOST      | DB     | COMMAND | TIME | STATE     | INFO                                         |
+--------+----------+-----------+--------+---------+------+-----------+----------------------------------------------+
| 769386 | testuser | localhost | NULL   | Sleep   |  210 |           | NULL                                         |
| 769390 | testuser | localhost | testdb | Query   |    0 | executing | select * from information_schema.processlist |
+--------+----------+-----------+--------+---------+------+-----------+----------------------------------------------+
2 rows in set (0.00 sec)

# 授予了PROCESS權限后,可以看到所有用戶的鏈接
mysql> grant process on *.* to 'testuser'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants;
+----------------------------------------------------------------------+
| Grants for testuser@%                                                |
+----------------------------------------------------------------------+
| GRANT PROCESS ON *.* TO 'testuser'@'%'                               |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `testdb`.* TO 'testuser'@'%' |
+----------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show processlist;
+--------+----------+--------------------+--------+---------+------+----------+------------------+
| Id     | User     | Host               | db     | Command | Time | State    | Info             |
+--------+----------+--------------------+--------+---------+------+----------+------------------+
| 769347 | root     | localhost          | testdb | Sleep   |   53 |          | NULL             |
| 769357 | root     | 192.168.85.0:61709 | NULL   | Sleep   |  521 |          | NULL             |
| 769386 | testuser | localhost          | NULL   | Sleep   |  406 |          | NULL             |
| 769473 | testuser | localhost          | testdb | Query   |    0 | starting | show processlist |
+--------+----------+--------------------+--------+---------+------+----------+------------------+
4 rows in set (0.00 sec)

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

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

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

# 只查看某個ID的鏈接信息
select * from information_schema.processlist where id = 705207;

# 篩選出某個用戶的鏈接
select * from information_schema.processlist where user = 'testuser';

# 篩選出所有非空閑的鏈接
select * from information_schema.processlist where command != 'Sleep';

# 篩選出空閑時間在600秒以上的鏈接
select * from information_schema.processlist where command = 'Sleep' and time > 600;

# 篩選出處於某個狀態的鏈接
select * from information_schema.processlist where state = 'Sending data';

# 篩選某個客戶端IP的鏈接
select * from information_schema.processlist where host like '192.168.85.0%';

2.殺掉數據庫鏈接

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

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

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

殺掉鏈接的能力取決於 SUPER 權限:

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

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

# 殺掉空閑時間在600秒以上的鏈接,拼接得到kill語句
select concat('KILL ',id,';') from information_schema.`processlist` 
where command = 'Sleep' and time > 600;

# 殺掉處於某個狀態的鏈接,拼接得到kill語句
select concat('KILL ',id,';') from information_schema.`processlist` 
where state = 'Sending data';

select concat('KILL ',id,';') from information_schema.`processlist` 
where state = 'Waiting for table metadata lock';

# 殺掉某個用戶發起的鏈接,拼接得到kill語句
select concat('KILL ',id,';') from information_schema.`processlist` 
 user = 'testuser';

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

總結:

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

wx_blog.png


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM