我注意到PostgreSQL和MySQL默认有100个客户端连接限制。 我想知道是否应该把它关掉,因为Web服务器在同一个盒子上,我只有大约20个PHP进程需要连接。
如果这个设置匹配或者超过了将要尝试连接的进程的数量?
在PostgreSQL(我不知道MySQL)有max_connections属性定义为:
确定到数据库服务器的最大并发连接数。 默认情况下通常是100个连接,但如果你的内核设置不支持它(在initdb期间确定),则可能会更less。 此参数只能在服务器启动时设置。
增加此参数可能会导致PostgreSQL请求比您的操作系统的默认configuration允许的更多的System V共享内存或信号量。 有关如何调整这些参数的信息,请参见第17.4.1节 (如有必要)。
客户端连接的有效限制被定义为:
max_connections - superuser_reserved_connections
superuser_reserved_connections的默认值是3。
你需要采取一些透视图。 今天让我们来说说,40个max_connections对你来说是安全的,它保留了一些操作系统资源( 文档中描述的信号量和共享内存),但明天可能还不够:
psql: FATAL: sorry, too many clients already
让我们来计算你得到的利润:
minSemaphoresSets = ceil((max_connections + autovacuum_max_workers)/16)
autovacuum_max_workers的默认值是3,所以:
prevSets = ceil((100 + 3)/16) = 7 newSets = ceil((40 + 3)/16) = 3
每个(Postgres)集合总是有17个信号量,所以你有68个信号量被安全的存在:
ipcs -s | grep postgres 0x0052e2c1 589824 postgres 600 17 0x0052e2c2 622593 postgres 600 17 0x0052e2c3 655362 postgres 600 17 0x0052e2c4 688131 postgres 600 17 0x0052e2c5 720900 postgres 600 17 0x0052e2c6 753669 postgres 600 17 0x0052e2c7 786438 postgres 600 17 # changing max_connections from 100 to 40 pg_ctlcluster 8.3 main restart ipcs -s | grep postgres 0x0052e2c1 819200 postgres 600 17 0x0052e2c2 851969 postgres 600 17 0x0052e2c3 884738 postgres 600 17
对于共享内存,它是〜1 MiB(更多细节请看表17-2 ):
ipcs -m | grep postgres 0x0052e2c1 0 postgres 600 29368320 4 # changing max_connections from 100 to 40 pg_ctlcluster 8.3 main restart ipcs -m | grep postgres 0x0052e2c1 425984 postgres 600 28270592 4
正如你所看到的,它不是太多,所以你可以使用默认限制,如果你不需要这样的优化。