標籤:time col 啟用 就是 ati glob 介紹 -- 串連數
查看mysql資料庫連接數、並發數相關資訊
1.mysql> show status like ‘Threads%‘;
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 58 |
| Threads_connected | 57 | ###這個數值指的是開啟的串連數
| Threads_created | 3676 |
| Threads_running | 4 | ###這個數值指的是啟用的串連數,這個數值一般遠低於connected數值
+-------------------+-------+
Threads_connected 跟show processlist結果相同,表示當前串連數。準確的來說,Threads_running是代表當前並發數
這是是查詢資料庫當前設定的最大串連數
2.mysql> show variables like ‘%max_connections%‘;
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 1000 |
+-----------------+-------+
可以在/etc/my.cnf裡面設定資料庫的最大串連數
[mysqld]
max_connections = 1000
max_connections 參數可以用於控制資料庫的最大串連數:
3.mysql> show variables like ‘%connect%‘;
+--------------------------+-------------------+
| Variable_name | Value |
+--------------------------+-------------------+
| character_set_connection | latin1 |
| collation_connection | latin1_swedish_ci |
| connect_timeout | 10 |
| init_connect | |
| max_connect_errors | 10 |
| max_connections | 4000 |
| max_user_connections | 0 |
+--------------------------+-------------------+
很多開發人員都會遇見”MySQL: ERROR 1040: Too many connections”的異常情況,造成這種情況的一種原因是訪問量過高,MySQL伺服器抗不住,這個時候就要考慮增加從伺服器分散讀壓力;另一種原因就是MySQL設定檔中max_connections值過小。
首先,我們來查看mysql的最大串連數:
1 2 3 4 5 6 7 |
mysql> show variables like ‘%max_connections%‘; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 151 | +-----------------+-------+ 1 row in set (0.00 sec) |
其次,查看伺服器響應的最大串連數:
1 2 3 4 5 6 7 |
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伺服器最大串連上限值設定過高。
1 |
Max_used_connections / max_connections * 100% = 2/151 *100% ≈ 1% |
我們可以看到佔比遠低於10%(因為這是本地測試伺服器,結果值沒有太大的參考意義,大家可以根據實際情況設定串連數的上限值)。
再來看一下自己 linode VPS 現在(時間:2013-11-13 23:40:11)的結果值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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%左右。
上面我們知道怎麼查看mysql伺服器的最大串連數值,並且知道了如何判斷該值是否合理,下面我們就來介紹一下如何設定這個最大串連數值。
方法1:
1 2 3 4 5 6 7 8 9 |
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資料庫連接數、並發數相關資訊