MySQL 修改最大鏈接數

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

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

1.命令行修改服務器

進入mysql後,set GLOBAL max_connections=1024; 便可當即生效,可是博主沒有使用這種方式,由於這種方法治標不治本,一點重啓mysql,最大鏈接數又會變回151ide

2.修改配置,而後重啓spa

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.orm

以個人英語水準,把上述語句歸納起來就是,max_connections依託於操做系統,Linux系統必要時須要增長open-files-limit。萬萬沒想到啊,修改max_connections居然要修改操做系統最大文件描述符。server

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

LimitNOFILE=50000

重啓MySQL

嗯,搞定了~

但願對你們有所幫助,祝你們天天開心~

相關文章
相關標籤/搜索