1

C:\mynodeprojects\test>node app Sever start on 300 failed to connect to server [localhost:27017] on first connect [MongoError: geta ddrinfo ENOENT localhost:27017]

Package : -

{
  "name": "test",
  "version": "1.0.0",
  "description": "simple test app",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "hukam thakur",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.2",
    "mongodb": "^2.2.33",
    "mongoose": "^4.10.8",
    "sudo": "^1.0.3"
  }
}

DB Connection here: -

var mongoose = require('mongoose');

var mongoURI = "mongodb://localhost:27017/test";
var MongoDB = mongoose.connect(mongoURI).connection;
MongoDB.on('error', function(err) { console.log(err.message); });
MongoDB.once('open', function() {
  console.log("mongodb connection open");
}); 
5
  • Seriously? Show your code, please. Commented Nov 10, 2017 at 12:08
  • Welcome to SO. Please take a minute to read on How to ask a question. Commented Nov 10, 2017 at 12:12
  • Could you please tell me, Where I'm doing wrong? Commented Nov 10, 2017 at 12:14
  • iagnostic data capture with directory 'C:/data/db/diagnostic.data' 2017-11-13T18:27:43.800+0530 I NETWORK [thread1] waiting for connections on por t 27017 Commented Nov 13, 2017 at 13:00
  • I experienced this same issue, in my case the DB didn't exist. I created it previously but it seemed to have not saved to persistent memory somehow. I recreated my database via the command line then I was able to connect. For troubleshooting you can try accessing any members of the database in a more manual way, like through the command line. Commented Mar 20, 2020 at 0:24

2 Answers 2

3

Please Try this code it's work for me :

var database,

ObjectId = require('mongodb').ObjectID,

MongoClient = require('mongodb').MongoClient,

url = 'mongodb://localhost:27017/test',

connection=MongoClient.connect(url, function(err, db) {  
  if (err) throw err;
  database=db;
});

Then write query like (eg for find uniqueId)-

database.collection("users").findOne({_id:new ObjectId(userId)}, function(err, res) { 
 if (err) console.log(err);
   console.log(res);
});  
Sign up to request clarification or add additional context in comments.

7 Comments

have u use this command for ubuntu to allow mongodb access - sudo ufw allow 27017
Server listening on port 3000 ^ MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
Mongo port default 27017. You can access mongo in node when you allow ufw command. It provide access to mongo database
iagnostic data capture with directory 'C:/data/db/diagnostic.data' 2017-11-13T18:27:43.800+0530 I NETWORK [thread1] waiting for connections on por t 27017
which OS you have used?
|
0

use 127.0.0.1 instead of localhost

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.