英文:
Connection is not open when use mongo.Connect instead it does when I do the query
问题
我正在使用mongo-driver编写一个Go应用程序,用于连接到mongo副本集。
我注意到mongo.Connect
实际上并没有连接到数据库。
即使我关闭了mongod
实例,mongo.Connect
仍然能够通过。
然而,当我执行查询时,它会连接到mongod
实例。
现在我的问题是,在同一个mongod
实例中有很多(>100)并发查询到不同的数据库。
驱动程序创建了一大堆连接,mongod
给出了Too many files opened
的错误,因为连接太多,即使我只使用一个mongo.Client
。
这是mongo_driver的正确行为吗?我该如何处理这个问题?
MongoDB是否需要每个数据库都有一个连接?
英文:
I'm writing a Go app using mongo-driver to connect to the mongo replica set.
I've noticed that mongo.Connect
is not actually connect to the database.
Even if I've shutdown the mongod
instance, mongo.Connect
sill able to pass through.
However, when I do the query it will connect to the mongod
instance.
Now my problem is I have a lot (>100) concurrent queries to different databases within the same mongod
instance.
The driver create a whole bunch of connections and mongod
failed me Too many files opened
because there is too many connections, Even I use a single mongo.Client
.
Is this a proper behavior of mongo_driver and how can I deal with this?
Does MongoDB require each connection per each database?
答案1
得分: 3
mongo.Connect()
创建一个新的 mongo.Client
并对其进行初始化,但不会(必要地)创建与数据库服务器的连接。
要实际创建连接并检查服务器是否可达(而不执行查询),可以使用 Client.Ping()
方法。如果服务器不可达,它将返回一个错误。
官方的 MongoDB 驱动程序管理一个内部连接池。连接在使用后不会立即关闭,而是放回连接池中,以便在需要执行操作时可以立即使用空闲连接。这是预期的行为。您可以通过传递给 mongo.Connect()
的 options.ClientOptions
来配置连接池的大小。
请参阅 ClientOptions.SetMaxPoolSize()
:
> SetMaxPoolSize 指定驱动程序连接池对每个服务器允许的最大连接数。如果达到此最大值,对服务器的请求将被阻塞。这也可以通过 "maxPoolSize" URI 选项来设置(例如,"maxPoolSize=100")。默认值为 100。如果设置为 0,则将设置为 math.MaxInt64。
以下是使用有限连接设置和进行连接测试的示例:
ctx := context.Background()
opts := options.Client().
ApplyURI("mongodb://localhost").
SetMaxPoolSize(20) // 每个服务器最多允许 20 个连接
client, err := mongo.Connect(ctx, opts)
if err != nil {
log.Printf("mongo.Connect() 失败:%v", err)
return
}
defer client.Disconnect(ctx)
if err := client.Ping(ctx, nil); err != nil {
log.Printf("无法连接到数据库:%v", err)
return
}
// 使用 client
参考链接:https://stackoverflow.com/questions/67862292/goroutine-create-multiple-mongodb-connection/67862482#67862482
英文:
mongo.Connect()
creates a new mongo.Client
and initializes it, but does not (necessarily) create connections to the DB server.
To actually create a connection and check if the server is reachable (without executing a query), you may use the Client.Ping()
method. This will return an error if the server is not reachable.
The official mongodb driver manages an internal pool of connections. Connections are not closed immediately after use, instead they are put back into the pool so when a connection is needed to carry out an operation, an idle connection can be used from the pool immediately. This is the intended behavior. You may configure its size via the options.ClientOptions
you pass to mongo.Connect()
.
See ClientOptions.SetMaxPoolSize()
:
> SetMaxPoolSize specifies that maximum number of connections allowed in the driver's connection pool to each server. Requests to a server will block if this maximum is reached. This can also be set through the "maxPoolSize" URI option (e.g. "maxPoolSize=100"). The default is 100. If this is 0, it will be set to math.MaxInt64.
Example setting up a client with limited connections, and pinging it:
ctx := context.Background()
opts := options.Client().
ApplyURI("mongodb://localhost").
SetMaxPoolSize(20) // Allow no more than 20 connections per server
client, err := mongo.Connect(ctx, opts)
if err != nil {
log.Printf("mongo.Connect() failed: %v", err)
return
}
defer client.Disconnect(ctx)
if err := client.Ping(ctx, nil); err != nil {
log.Printf("Can't connect to db: %v", err)
return
}
// Use client
See related: https://stackoverflow.com/questions/67862292/goroutine-create-multiple-mongodb-connection/67862482#67862482
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论