Tuesday, July 2, 2019

MongoNetworkError: failed to connect to server MongoNetworkError: connect ECONNREFUSED


Hi Guys ,

You might have faced an error like MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

We generally face this error when the database server is down , if you bring the database server up you will not face this error .

The full error is as follows :


(node:15564) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
F:\\study\nodejs\node_modules\mongodb\lib\topologies\server.js:240
            throw err;
            ^

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (F:\\study\nodejs\node_modules\mongodb-core\lib\topologies\server.js:431:11)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at connect (F:\\study\nodejs\node_modules\mongodb-core\lib\connection\pool.js:557:14)
    at makeConnection (F:\\study\nodejs\node_modules\mongodb-core\lib\connection\connect.js:39:11)
    at callback (F:\\study\nodejs\node_modules\mongodb-core\lib\connection\connect.js:261:5)
    at Socket.err (F:\\study\nodejs\node_modules\mongodb-core\lib\connection\connect.js:286:7)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)


The code snipped to connect to mongo db is as follows :


var mongo = require('mongodb');
var url = "mongodb://localhost:27017/qanda";
mongo.MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});


No comments:

Post a Comment

ec2-user@ec2 Permission denied