mysql最佳化:串連數

來源:互聯網
上載者:User

標籤:style   blog   io   ar   檔案   sp   div   2014   art   

有時候我們會遇見”MySQL: ERROR 1040: Too many connections”的異常,一種原因是訪問量過高,MySQLserver抗不住,這個時候就要考慮添加從server分散讀壓力,還有一種原因就是MySQL設定檔裡max_connections值過小。

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

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

其次,查看server響應的最大串連數:

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

能夠看到server響應的最大串連數為2,遠遠低於mysqlserver同意的最大串連數值。

對於mysqlserver最大串連數值的設定範圍比較理想的是:server響應的最大串連數值佔server上限串連數值的比例值在10%以上,假設在10%下面,說明mysqlserver最大串連上限值設定過高。

Max_used_connections / max_connections  * 100% = 2/151 *100% ≈ 1%

我們能夠看到佔比遠低於10%(由於這是本地測試server,結果值沒有太大的參考意義,大家能夠依據實際情況設定串連數的上限值)。

再來看一下自己 linode VPS 如今(時間:2013-11-13 23:40:11)的結果值:

mysql> show variables like '%max_connections%';+-----------------+-------+| Variable_name   | Value |+-----------------+-------+| max_connections | 151   |+-----------------+-------+1 row in set (0.19 sec)mysql> show global status like 'Max_used_connections';+----------------------+-------+| Variable_name        | Value |+----------------------+-------+| Max_used_connections | 44    |+----------------------+-------+1 row in set (0.17 sec)

這裡的最大串連數占上限串連數的30%左右。

上面我們知道怎麼查看mysqlserver的最大串連數值,而且知道了怎樣推斷該值是否合理,以下我們就來介紹一下怎樣設定這個最大串連數值。

方法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.