共計 2652 個字符,預計需要花費 7 分鐘才能閱讀完成。
這篇文章將為大家詳細講解有關 mysql 中 Too many connections 問題怎么處理,丸趣 TV 小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
1、問題展現
應用端登錄出現 Too many connections 報錯
檢查發現 mysql 數據庫服務端已經達到了 max_connections 上限
mysql show variables like max_connections
+—————–+——-+
| Variable_name | Value |
+—————–+——-+
| max_connections | 1900 |
+—————–+——-+
1 row in set (0.00 sec)
mysql show processlist;
已經達到了 1900 會話數。
thread_pool 設置并不能阻止會話數的上升。
mysql show variables like thread_pool%
+————————————–+——-+
| Variable_name | Value |
+————————————–+——-+
| thread_pool_algorithm | 0 |
| thread_pool_high_priority_connection | 0 |
| thread_pool_max_unused_threads | 0 |
| thread_pool_prio_kickup_timer | 1000 |
| thread_pool_size | 16 |
| thread_pool_stall_limit | 6 |
+————————————–+——-+
6 rows in set (0.00 sec)
2、問題處理
重啟 mysql 的服務。重啟完 mysql 服務后,的確 mysql 的 session 數下降了,但是很快會話數又上升到了 1900。
判斷并不是 mysql 的服務器端的會話沒釋放,而是 application 端的會話沒釋放。
重啟 application 的兩臺服務器,mysql 的會話數恢復正常。
3、結論
先來看看 mysql 服務器端的會話保持時間:
mysql show variables like %wait_timeout%
+————————–+———-+
| Variable_name | Value |
+————————–+———-+
| innodb_lock_wait_timeout | 50 |
| lock_wait_timeout | 31536000 |
| wait_timeout | 28800 |
+————————–+———-+
3 rows in set (0.00 sec)
mysql show variables like %interactive_timeout%
+———————+——-+
| Variable_name | Value |
+———————+——-+
| interactive_timeout | 28800 |
+———————+——-+
1 row in set (0.00 sec)
interactive_timeout:服務器關閉交互式連接前等待活動的秒數。交互式客戶端定義為在 mysql_real_connect()中使用 CLIENT_INTERACTIVE 選項的客戶端。又見 wait_timeout
wait_timeout: 服務器關閉非交互連接之前等待活動的秒數。在線程啟動時,根據全局 wait_timeout 值或全局 interactive_timeout 值初始化會話 wait_timeout 值,取決于客戶端類型 (由 mysql_real_connect() 的連接選項 CLIENT_INTERACTIVE 定義),又見 interactive_timeout
如此看來,兩個變量是共同控制的,那么都必須對他們進行修改了。繼續深入這兩個變量 wait_timeout 的取值范圍是 1 -2147483(Windows),1-31536000(linux),interactive_time 取值隨 wait_timeout 變動,它們的默認值都是 28800。
MySQL 的系統變量由配置文件控制,當配置文件中不配置時,系統使用默認值,這個 28800 就是默認值。要修改就只能在配置文件里修改。Windows 下在 %MySQL HOME%/bin 下有 mysql.ini 配置文件,打開后添加兩個變量,賦值。
要解決這個問題:
1、Use connection pooling at client side (in MySQL Connector) to reduce the number of active connections between the client and the server.
是在客戶端安裝 MySQL Connector
2、Improve the application design to reduce the number of active connections needed and to reduce the time the connection has to stay active.
從應用端去降低并發數,減少每個會話的保持時間
3、Increase the number of connections handled by MySQL server by adjusting max_connections (keep in mind that this consumes additional RAM and is still limited)
在 mysql 服務器端增加最大連接數設置,不過會消耗大量內存
建議用第二種方法。因為當前應用會話保持時間是 10 分鐘,建議降低這個數值。
關于“mysql 中 Too many connections 問題怎么處理”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。