MySQL串連數過多處理方法執行個體

來源:互聯網
上載者:User

MySQL串連數過多處理方法執行個體

一、問題描述

        今天突然接到個問題,網頁報錯:503 Service Temporarily Unavailable。經過查詢發現是某個使用者的串連超級多,已經將資料庫連接佔滿。處理方案,即時殺掉堵塞的進程,之後可以擴大max_connections參數。

二、處理方法

1.查詢串連情況

root@localhost > show processlist;

…...
1001 rows in set (0.00 sec)
root@localhost > show variables like '%proces%';
Empty set (0.00 sec)

2.檢查參數

root@localhost > show global status like 'Max_used_connections';

+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 1001 |
+----------------------+-------+
1 row in set (0.00 sec)

3.通過命令產生殺進程指令碼

root@localhost > select concat('KILL ',id,';') from information_schema.processlist where user=’sam' into outfile '/tmp/a.txt
指令碼內容如下:

+------------------------+

| concat('KILL ',id,';') |
+------------------------+
| KILL 31964612; |
| KILL 31964609; |
| KILL 31964611; |
…...
| KILL 31966619; |
| KILL 31966620; |
+------------------------+
991 rows in set (0.02 sec)
root@localhost >

4.執行上面產生的KILL指令碼

root@localhost > source /tmp/a.txt

Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
……
 
5.檢查串連狀況,恢複正常

root@localhost > show processlist;

6.修改Max_used_connections參數(註:記得要修改my.cnf檔案,下次重啟動後仍然有效)

mysql> set GLOBAL max_connections=2000;

Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 2000 |
+-----------------+-------+
1 row in set (0.00 sec)

三、總結
    MySQL的參數學習之max_connections,一個控制串連數的參數。此問題背後肯定存在著某些問題,不要只是一味地調大參數。後來經過對語句的分析,最終此問題定位為安全部門在做安全性測試,導致問題產生。

本文永久更新連結地址:https://www.bkjia.com/Linux/2018-02/150985.htm

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.