mysql優化連接數防止訪問量過高的方法


這篇文章主要介紹了mysql優化連接數防止訪問量過高的方法,需要的朋友可以參考下

 

很多開發人員都會遇見”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服務即可。

 

 

1.show status

Threads_connected  當前的連接數
  Connections   試圖連接到(不管是否成功)MySQL服務器的連接數。
  Max_used_connections  服務器啟動后已經同時使用的連接的最大數量。


2.set GLOBAL max_connections=連接數;
  flush privileges

 

3.修改/etc/my.cnf中的max_connections


4.show processlist   顯示當前正在執行的mysql連接

 

5.mysqladmin -u<user> -p<pwd> -h<host> status

顯示當前mysql狀態

Uptime: 13131  Threads: 1  Questions: 22  Slow queries: 0  Opens: 16   Flush tables: 1  Open tables: 1  Queries per second avg: 0.1


  mysqladmin -u<user> -p<pwd> -h<host> extended-status

顯示mysql的其他狀態

+-----------------------------------+----------+
| Variable_name                     | Value     |
+-----------------------------------+----------+
| Aborted_clients                   | 0        |
| Aborted_connects               | 1        |
| Binlog_cache_disk_use       | 0        |
| Binlog_cache_use               | 0        |
| Bytes_received                   | 1152   |
| Bytes_sent                         | 10400 |
| Com_admin_commands      | 0        |
| Com_assign_to_keycache  | 0        |

.............................................................

.............................................................

| Threads_cached                 | 2        |
| Threads_connected            | 1        |
| Threads_created                | 3        |
| Threads_running                | 1        |
| Uptime                                | 13509    |
| Uptime_since_flush_status | 13509     |
+-----------------------------------+----------+

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM