MongoDB Atlas Cluster 连接总是有上限
MongoDB Atlas Cluster connections are always capped
我正在使用 express 服务器、mongoose 和 MongoDB Atlas 作为后端。我的应用程序托管在 heroku 上。
我只在服务器 bootstrap:
上打开一次连接
db()
.then(() => {
console.log('Connected to the database')
})
.catch((err) => {
console.log(err);
});
db()
看起来像这样:
function db() {
return new Promise(function(resolve, reject) {
mongoose.connect(process.env.ATLAS_URI, {useNewUrlParser: true, poolSize: 25});
mongoose.connection.once('open', function() {
console.log('open')
resolve("Connection has been made.");
});
mongoose.connection.on('error', function(error) {
reject("Connection error: " + error.message);
})
})
}
然后,在短短 2 小时内,我打开了 500 个连接,这导致 Atlas 出现错误。
重新启动测功机有帮助,但我不能每 2 小时重新启动一次。
它可能是什么,我该如何解决这个问题?
@Michael Kutateladze 由于您在使用后没有关闭连接,因此对您的应用程序的并发请求数正在创建更多连接并导致超过 MongoDB Atlas 连接限制。因此,重要的是您要么在使用后关闭逻辑中的连接,要么创建一个连接池来临时使用用例。
同样,超过 Atlas 集群的连接限制可能出于不同的原因而发生,并且 Atlas 层根据集群大小具有不同的连接限制。因此,更大的集群层将有更高的连接限制。因此,如果它影响您的应用程序和生产连接限制,所有这些都值得考虑。
连接池逻辑示例:
var express = require('express');
var mongodb = require('mongodb');
var app = express();
var MongoClient = require('mongodb').MongoClient;
var db; //reusable db instance
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
if(err) throw err;
db = database;
// Make sure you start the application after the database connection is ready
app.listen(3000);
console.log("Listening on port 3000");
});
// Reuse database object in request handlers like below
app.get("/", function(req, res) {
db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
//code here
});
});
我正在使用 express 服务器、mongoose 和 MongoDB Atlas 作为后端。我的应用程序托管在 heroku 上。 我只在服务器 bootstrap:
上打开一次连接db()
.then(() => {
console.log('Connected to the database')
})
.catch((err) => {
console.log(err);
});
db()
看起来像这样:
function db() {
return new Promise(function(resolve, reject) {
mongoose.connect(process.env.ATLAS_URI, {useNewUrlParser: true, poolSize: 25});
mongoose.connection.once('open', function() {
console.log('open')
resolve("Connection has been made.");
});
mongoose.connection.on('error', function(error) {
reject("Connection error: " + error.message);
})
})
}
然后,在短短 2 小时内,我打开了 500 个连接,这导致 Atlas 出现错误。 重新启动测功机有帮助,但我不能每 2 小时重新启动一次。 它可能是什么,我该如何解决这个问题?
@Michael Kutateladze 由于您在使用后没有关闭连接,因此对您的应用程序的并发请求数正在创建更多连接并导致超过 MongoDB Atlas 连接限制。因此,重要的是您要么在使用后关闭逻辑中的连接,要么创建一个连接池来临时使用用例。
同样,超过 Atlas 集群的连接限制可能出于不同的原因而发生,并且 Atlas 层根据集群大小具有不同的连接限制。因此,更大的集群层将有更高的连接限制。因此,如果它影响您的应用程序和生产连接限制,所有这些都值得考虑。
连接池逻辑示例:
var express = require('express');
var mongodb = require('mongodb');
var app = express();
var MongoClient = require('mongodb').MongoClient;
var db; //reusable db instance
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
if(err) throw err;
db = database;
// Make sure you start the application after the database connection is ready
app.listen(3000);
console.log("Listening on port 3000");
});
// Reuse database object in request handlers like below
app.get("/", function(req, res) {
db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
//code here
});
});