MongoDB中有關(guān)數(shù)據(jù)庫連接

本文系個人經(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


mongoDMaxConn.png

連接數(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/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容