本文系個人經(jīng)驗以及官方文檔總結(jié),并非適用于所有版本的MongoDB或者所有語言的MongoDB驅(qū)動。 本人使用MongoDB v3.6.4, Java語言的驅(qū)動。
啟動MongoDB時,限制連接總數(shù)
啟動mongo是在配置文件中,或者在啟動參數(shù)加上maxConns
--maxConns <number>
The maximum number of simultaneous connections that mongod will accept. This setting has no effect if it is higher than your operating system’s configured maximum connection tracking threshold.
Do not assign too low of a value to this option, or you will encounter errors during normal application operation.
為了快速演示效果,我在啟動參數(shù)中加上了maxConns

連接數(shù)據(jù)庫時限制連接總數(shù)
官方文檔中這樣寫到:
Connection Pool Options
Most drivers implement some kind of connection pool handling. Some drivers do not support connection pools. See your driver documentation for more information on the connection pooling implementation. These options allow applications to configure the connection pool when connecting to the MongoDB deployment.
Connection Option Description
maxPoolSize
The maximum number of connections in the connection pool. The default value is 100.
minPoolSize
The minimum number of connections in the connection pool. The default value is 0.
NOTE
The minPoolSize option is not supported by all drivers. For information on your driver, see the drivers documentation.
例如我們在配置MongoDB的連接字符串是可以這樣設(shè)置。 在URI末尾加上&maxPoolSize=<integer>即可設(shè)置連接池的連接數(shù)。
mongodb://root:xxxxxx@a.mongodb.com:27017/admin?maxPoolSize=10
如何查詢及限制連接數(shù)
Mongo Shell登錄MongoDB數(shù)據(jù)庫
通過 Mongo Shell 連接實例,執(zhí)行命令db.serverStatus().connections。
> db.serverStatus().connections;
{ "current" : 17, "available" : 999983, "totalCreated" : 47 }
備注:我啟動的是單機(jī)版mongoDB, 不是replicaSet模式,也不是Sharding模式
查詢當(dāng)前連接來源
db.runCommand() runs the command in the context of the current database. Some commands are only applicable in the context of the admin database, and you must change your db object to before running these commands.
currentOp must run against the admin database, and it can accept several optional fields.
先切換到admin數(shù)據(jù)庫,然后執(zhí)行db.runCommand({currentOp: 1, $all:[{"active" : true}]})
>> use admin
switched to db admin
> db.runCommand({currentOp: 1, $all:[{"active" : true}]})
{
"inprog" : [
{
"host" : "DESKTOP-8S2E5H7:27017",
"desc" : "conn34",
"connectionId" : 34,
"client" : "127.0.0.1:60027",
"active" : false,
"currentOpTime" : "2019-12-15T10:22:07.095+0800"
},
......
{
"host" : "DESKTOP-8S2E5H7:27017",
"desc" : "conn14",
"connectionId" : 14,
"client" : "127.0.0.1:55322",
"active" : false,
"currentOpTime" : "2019-12-15T10:22:07.095+0800"
},
{
"host" : "DESKTOP-8S2E5H7:27017",
"desc" : "conn2",
"connectionId" : 2,
"client" : "127.0.0.1:54588",
"appName" : "MongoDB Shell",
"clientMetadata" : {
"application" : {
"name" : "MongoDB Shell"
},
"driver" : {
"name" : "MongoDB Internal Client",
"version" : "3.6.4"
},
"os" : {
"type" : "Windows",
"name" : "Microsoft Windows 10",
"architecture" : "x86_64",
"version" : "10.0 (build 17134)"
}
},
"active" : true,
"currentOpTime" : "2019-12-15T10:22:07.095+0800",
"opid" : 22102,
"secs_running" : NumberLong(0),
"microsecs_running" : NumberLong(144),
"op" : "command",
"ns" : "admin.$cmd.aggregate",
"command" : {
"currentOp" : 1,
"$all" : [
{
"active" : true
}
],
"$db" : "admin"
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
},
{
"host" : "DESKTOP-8S2E5H7:27017",
"desc" : "conn21",
"connectionId" : 21,
"client" : "127.0.0.1:55398",
"active" : false,
"currentOpTime" : "2019-12-15T10:22:07.095+0800"
},
.......
{
"host" : "DESKTOP-8S2E5H7:27017",
"desc" : "initandlisten",
"active" : false,
"currentOpTime" : "2019-12-15T10:22:07.095+0800"
},
{
"host" : "DESKTOP-8S2E5H7:27017",
"desc" : "WTJournalFlusher",
"active" : false,
"currentOpTime" : "2019-12-15T10:22:07.095+0800"
},
{
"host" : "DESKTOP-8S2E5H7:27017",
"desc" : "conn13",
"connectionId" : 13,
"client" : "127.0.0.1:55321",
"active" : false,
"currentOpTime" : "2019-12-15T10:22:07.095+0800"
}
],
"ok" : 1
}
>
引用:
1, https://docs.mongodb.com/manual/reference/program/mongod/#core-options
2, https://docs.mongodb.com/manual/reference/connection-string/index.html#connections-connection-options
3, https://docs.mongodb.com/manual/reference/method/db.serverStatus/
4, https://docs.mongodb.com/manual/reference/command/serverStatus/#dbcmd.serverStatus
5, https://docs.mongodb.com/manual/reference/command/currentOp/