mysql最佳化串連數防止訪問量過高的方法

來源:互聯網
上載者:User

標籤:

很多開發人員都會遇見”MySQL: ERROR 1040: Too many connections”的異常情況,造成這種情況的一種原因是訪問量過高,MySQL伺服器抗不住,這個時候就要考慮增加從伺服器分散讀壓力;另一種原因就是MySQL設定檔中max_connections值過小。

首先,我們來查看mysql的最大串連數:

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

其次,查看伺服器響應的最大串連數:

mysql>show global status like ‘Max_used_connections‘;+ ----------------------+-------+| Variable_name    | Value |+ ----------------------+-------+| Max_used_connections | 2   |+ ----------------------+-------+1 row in set (0.00 sec)

可以看到伺服器響應的最大串連數為2,遠遠低於mysql伺服器允許的最大串連數值。
對於mysql伺服器最大串連數值的設定範圍比較理想的是:伺服器響應的最大串連數值占伺服器上限串連數值的比例值在10%以上,如果在10%以下,說明mysql伺服器最大串連上限值設定過高。
Max_used_connections / max_connections * 100% = 2/151 *100% ≈ 1%
上面我們知道怎麼查看mysql伺服器的最大串連數值,並且知道了如何判斷該值是否合理,下面我們就來介紹一下如何設定這個最大串連數值。
方法1:

mysql> set GLOBAL max_connections=256;Query OK, 0 rows affected (0.00 sec)mysql> show variables like ‘%max_connections%‘;+ -----------------+-------+| Variable_name  | Value |+ -----------------+-------+| max_connections | 256  |+ -----------------+-------+1 row in set (0.00 sec)

方法2:
修改mysql設定檔my.cnf,在[mysqld]段中添加或修改max_connections值:
max_connections=128
重啟mysql服務即可。


mysql最佳化串連數防止訪問量過高的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.