MySQL 修改最大連接數


今天使用ide連接線下MySQL報錯Can not connect to MySQL server. Too many connections,報錯很明確,與MySQL的連接數滿了。想想也是,每起一個服務都會創建MySQL連接池,占用不少的長連接。用ide查看了一下,原來最大連接數才151,看來有必要改大一點了。

上網查了一下,修改方式有兩種

1.命令行修改

進入mysql后,set GLOBAL max_connections=1024; 即可立即生效,但是博主沒有使用這種方式,因為這種方法治標不治本,一點重啟mysql,最大連接數又會變回151

2.修改配置,然后重啟

vi /etc/m.cnf加入max_connections=1024,然后重啟mysql即可。

重啟后,很遺憾,max_connections變成了214,這就很詭異了。我把max_connections又分別設置成500和213,實際的max_connections分別是214和213。也就是說,在這台服務器上,max_connections最大只能是234,猜測是因為操作系統的限制導致max_connections最大只能為213。博主翻了翻MySQl官方文檔(英語不好看文檔真是渾身難受),發現以下幾句話:

The maximum number of connections MySQL can support depends on the quality of the thread library on a given platform, the amount of RAM available, how much RAM is used for each connection, the workload from each connection, and the desired response time. Increasing open-files-limit may be necessary. Also see Section 2.5, “Installing MySQL on Linux”, for how to raise the operating system limit on how many handles can be used by MySQL.

以我的英語水准,把上述語句概括起來就是,max_connections依托於操作系統,Linux系統必要時需要增加open-files-limit。萬萬沒想到啊,修改max_connections竟然要修改操作系統最大文件描述符。

vi /usr/lib/systemd/system/mysqld.service加入

LimitNOFILE=50000

重啟MySQL

嗯,搞定了~

希望對大家有所幫助,祝大家每天開心~


免責聲明!

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



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