I'm using NodeJS wih MongoDB using mongodb package. When I run mongod command it works fine and gives "waiting for connection on port 27017". So, mongod seems to be working. But MongoClient does not work and gives error when I run node index.js command-
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
I have install mongo db 3.4 and my code is-
var MongoClient = require('mongodb').MongoClient;
var dburl = "mongodb://localhost:27017/test";
MongoClient.connect(dburl, function(err, db) {
if (err) {
throw err;
}
console.log('db connected');
db.close();
});
I have created data/db directories on root and given write permissions.
mongod.conf file takes db path as-
storage:
dbPath: /var/lib/mongo
But it seems that it is actually taking db path as data/db and not var/lib/mongo
It working earlier but suddenly stopped.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…