MongoDB中有关数据库连接
本文系个人经验以及官方文档总结,并非适用于所有版本的MongoDB或者所有语言的MongoDB驱动。 本人使用MongoDB v3.6.4, Java语言的驱动。
启动MongoDB时,限制连接总数
启动mongo是在配置文件中,或者在启动参数加上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.
为了快速演示效果,我在启动参数中加上了maxConns
mongoDMaxConn.png
连接数据库时限制连接总数
官方文档中这样写到:
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的连接字符串是可以这样设置。 在URI末尾加上&maxPoolSize=<integer>即可设置连接池的连接数。
mongodb://root:xxxxxx@a.mongodb.com:27017/admin?maxPoolSize=10
如何查询及限制连接数
Mongo Shell登录MongoDB数据库
通过 Mongo Shell 连接实例,执行命令db.serverStatus().connections。
> db.serverStatus().connections;
{ "current" : 17, "available" : 999983, "totalCreated" : 47 }
备注:我启动的是单机版mongoDB, 不是replicaSet模式,也不是Sharding模式
查询当前连接来源
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数据库,然后执行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/