MongoDB 最大连接数配置

1 相关参数说明
查看MongoDB 的最大连接数:
[root@testdb ~]# mongo --port 27018
MongoDB shell version v4.4.13
connecting to: mongodb://127.0.0.1:27018/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c209ee6a-747b-44e1-a6b5-d50d922aeb68") }
MongoDB server version: 4.4.13
shard1:PRIMARY> db.serverStatus().connections
shard1:PRIMARY> use admin
switched to db admin
shard1:PRIMARY> db.auth('root','root')
1
shard1:PRIMARY> db.serverStatus().connections
{
        "current" : 51,
        "available" : 768,
        "totalCreated" : 945,
        "active" : 19,
        "exhaustIsMaster" : 16,
        "exhaustHello" : 0,
        "awaitingTopologyChanges" : 147
}
shard1:PRIMARY>
这里显示的current + available 就是mongodb 的最大连接数。

我们这里是 51 + 768 = 819个连接。

实际上MongoDB 数据库层面最大的连接数是受net.maxIncomingConnections 参数控制,该参数的默认值是65536。 也远比这里的819 多。

在Linux 的limits.conf 参数中,也有控制每个进程最大能打开的进程数。
[root@testdb ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7268
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 102400
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7268
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@testdb ~]#

[root@testdb ~]# ps -ef|grep mongod
root      1208   855  1 May04 ?        00:18:46 /usr/local/mongodb/bin/mongod -f /data/mongodb/etc/shard1.conf

[root@testdb ~]# cat /proc/1208/limits
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        0                    unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             7268                 7268                 processes
Max open files            1024                 4096                 files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       7268                 7268                 signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us
[root@testdb ~]#
我们可以看到这里能打开的最大文件数是1024。

Linux 处于保护的目的,只能拿出80%的句柄可以使用:

1024 * 0.8 = 819.2

所以我们上文查看mongoDB 的最大连接数就是这里受到了限制。

所以,对于生产环境,我们在安装MongoDB 之前,还需要修改linux 的资源限制策略。


2 单机环境测试
查看2个系统配置参数:
[root@testdb etc]# cat /etc/security/limits.conf|grep -v '^#'

* soft nofile 102400
* hard nofile 102400
[root@testdb etc]#

[root@testdb etc]# cat /etc/sysctl.conf |grep -v '^#'
vm.swappiness=0
fs.file-max=1024000
[root@testdb etc]#


[root@testdb etc]# ulimit -a
……
open files                      (-n) 102400
……

查看mongo 参数:

[root@testdb etc]# ll
total 8
-rw-r--r-- 1 root root  340 May  1 11:47 mongo.conf
-rw------- 1 root root 1020 May  1 11:47 mongo.keyfile
[root@testdb etc]# cat mongo.conf
systemLog:
  destination: file
  path: "/data/mongodb/logs/mongodb.log"
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: "/data/mongodb/data"
net:
  bindIp: 0.0.0.0
  port: 27017
security:
  keyFile: /data/mongodb/etc/mongo.keyfile
  authorization: "enabled"
processManagement:
  fork: true
replication:
  replSetName: rs0
  
我们这里并没有配置连接数限制。 默认也是65535.
[root@testdb etc]# cat /proc/2344/limits
Limit                     Soft Limit           Hard Limit           Units
……
Max open files            102400               102400               files
……

实际上这里已经生效了:

[root@testdb etc]# mongo --port 27017
MongoDB shell version v4.4.13
> use admin
switched to db admin
> db.serverStatus().connections
> db.auth(
... 'root','root')
1
> db.serverStatus().connections
{
        "current" : 1,
        "available" : 81919,
        "totalCreated" : 15,
        "active" : 1,
        "exhaustIsMaster" : 0,
        "exhaustHello" : 0,
        "awaitingTopologyChanges" : 0
}
> exit
bye
[root@testdb etc]#

这里并没有问题:102400 * 0.8 = 81920。 所以可以看出,即使单机环境下DB上没有配置,也是可以生效的,以操作系统上的参数限制为准。


3 副本集环境测试
2个系统级别配置参数一样:

[root@testdb etc]# cat /etc/security/limits.conf|grep -v '^#'

* soft nofile 102400
* hard nofile 102400
[root@testdb etc]#

[root@testdb etc]# cat /etc/sysctl.conf |grep -v '^#'
vm.swappiness=0
fs.file-max=1024000
[root@testdb etc]#

参数也生效了:

[root@testdb etc]# ulimit -a
……
open files                      (-n) 102400
……
但是DB 级别就是没有生效:

[root@testdb ~]# mongo --port 27019
shard2:SECONDARY> use admin
switched to db admin
shard2:SECONDARY> db.auth('root','root')
1
shard2:SECONDARY> db.serverStatus().connections
{
        "current" : 42,
        "available" : 777,
        "totalCreated" : 187,
        "active" : 17,
        "exhaustIsMaster" : 15,
        "exhaustHello" : 0,
        "awaitingTopologyChanges" : 29
}
shard2:SECONDARY>

查看进程限制,确实还是原来的默认值:

[root@testdb etc]# ps -ef|grep mongod
……
root      1218   859  0 May05 ?        00:06:11 /usr/local/mongodb/bin/mongod -f /data/mongodb/etc/shard2.conf
……
[root@testdb etc]# cat /proc/1218/limits
Limit                     Soft Limit           Hard Limit           Units
……
Max open files            1024                 4096                 files
……
这里没有生效的原因还是Centos7系统中,使用Systemd替代了之前的SysV。/etc/security/limits.conf文件的配置作用域缩小了。/etc/security/limits.conf的配置,只适用于通过PAM认证登录用户的资源限制,它对systemd的service的资源限制不生效。

我们之前单机环境测试正常,是因为我们没有使用supervisor 进行管理。 Shard cluster 环境我们配置了supervisor管理进程。

所以我们修改systemd的配置,修改/etc/systemd/system.conf 文件中的配置:

[root@testdb ~]# cat /etc/systemd/system.conf|grep -v '^#'

[Manager]
DefaultLimitNOFILE=102400
[root@testdb ~]#
参数生效的两种方法:

重启主机
执行 systemctl daemon-reexec
daemon-reexec 会重新执行systemd管理器,重新读取系统配置文件,而daemon-reload只会去读service部分的配置,不包含全局配置/systemd/system.conf,相当于重量级的daemon-reload.

然后重启supervisor 进程:

[root@testdb ~]# service supervisor stop
Redirecting to /bin/systemctl stop supervisor.service
[root@testdb ~]# service supervisor start
Redirecting to /bin/systemctl start supervisor.service
修改生效:

[root@testdb ~]# cat /proc/1211/limits
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        0                    unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             7268                 7268                 processes
Max open files            102400               102400               files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks




shard1:SECONDARY> db.auth('root','root')
1
shard1:SECONDARY> db.serverStatus().connections
{
        "current" : 43,
        "available" : 81877,
        "totalCreated" : 43,
        "active" : 17,
        "exhaustIsMaster" : 16,
        "exhaustHello" : 0,
        "awaitingTopologyChanges" : 16
}
shard1:SECONDARY>

 

posted @ 2025-06-17 01:09  屠魔的少年  阅读(102)  评论(0)    收藏  举报