MongoError:第一次连接时无法连接到服务器[localhost:27017] [MongoError:connect EADDRINUSE 127.0.0.1:27017]

时间:2017-07-21 07:49:08

标签: node.js mongodb

当我运行我的服务器时,经过一些查询后,它会抛出以下错误:

C:\Users\Me\Documents\Project\node_modules\mongodb\lib\mongo_client.js:415
          throw err
          ^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect EADDRINUSE 127.0.0.1:27017]
    at Pool.<anonymous> (C:\Users\Me\Documents\Project\node_modules\mongodb-core\lib\topologies\server.js:329:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:188:7)
    at Connection.<anonymous> (C:\Users\Me\Documents\Project\node_modules\mongodb-core\lib\connection\pool.js:280:12)
    at Connection.g (events.js:292:16)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:191:7)
    at Socket.<anonymous> (C:\Users\Me\Documents\Project\node_modules\mongodb-core\lib\connection\connection.js:187:49)
    at Socket.g (events.js:292:16)
    at emitOne (events.js:96:13)

这完全是随机的,但主要发生在1000-2000次查询之后。

要查询的node.js代码如下:

let mongo = require('mongodb');
let MongoClient = mongo.MongoClient;
let url = "mongodb://localhost:27017/mydb";
function queryMongo(configurations, callback) {
    MongoClient.connect(url, (err, db) => {
        if (err) throw err;
        let query = {
            name: {
                $in: configurations
            }
        };
        db.collection("configurations").find(query).toArray((err, results) => {
            if (err) throw err;
            callback(results);
            db.close();
        });
    });
}

上一次查询完成后立即调用queryMongo函数。我尝试删除锁定文件,但它没有改变任何东西。我将数据库复制到另一台计算机但仍然存在同样的问题。

0 个答案:

没有答案