查看mysql資料庫連接數、並發數相關資訊

來源:互聯網
上載者:User

標籤: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資料庫連接數、並發數相關資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.