天天看點

MySQL參數最大連接配接數max_connections

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是代表目前并發數
 
這是是查詢資料庫目前設定的最大連接配接數
mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 1000  |
+-----------------+-------+
 
可以在/etc/my.cnf裡面設定資料庫的最大連接配接數
[mysqld]
max_connections = 1000      

2、show status

   Threads_connected  目前的連接配接數

   Connections  試圖連接配接到(不管是否成功)MySQL伺服器的連接配接數。

   Max_used_connections  伺服器啟動後已經同時使用的連接配接的最大數量。

3、設定最大連接配接數

set GLOBAL max_connections=2000;
set GLOBAL max_user_connections=1500;

 mysql> show global variables like "max%connections";
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| max_connections      | 2000  |
| max_user_connections | 1500  |
+----------------------+-------+
2 rows in set (0.08 sec)      

說明,

系統支援的最大連接配接max_connections

使用者能最大連接配接進來的數量max_user_connections

别忘記在配置檔案裡添加否則重新開機失效

max_connections=1000;

max_user_connection=600

以上設定,為自己和其他使用者預留幾個連接配接空閑

4、修改/etc/my.cnf中的max_connections

5、show processlist   顯示目前正在執行的MySQL連接配接

6、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    |
+-----------------------------------+----------+      

7、遇到最大連接配接數處理

[推薦] 使用 mysqladmin flush-hosts 指令清理一下hosts檔案(不知道mysqladmin在哪個目錄下可以使用指令查找:whereis mysqladmin);

mysqladmin flush-hosts -h192.168.x.x -Pxxxx -uroot -pxxxx;      

8、OS ulimit參數

[root@emsc ~]# ulimit -n
65535