MongoError: connect ECONNREFUSED 127.0.0.1:27017

匿名 (未验证) 提交于 2019-12-03 07:47:04

问题:

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.

回答1:

For windows - just go to Mongodb folder (ex : C:\ProgramFiles\MongoDB\Server\3.4\bin) and open cmd in the folder and type "mongod.exe --dbpath c:\data\db"

if c:\data\db folder doesn't exist then create it by yourself and run above command again.

All should work fine by now.))



回答2:

Try to start mongoDB server by giving the --dbpath with mongod.

sudo mongod --dbpath /var/lib/mongo/data/db & 

'&' in the last will start the mongodb server as service on your server.

Hope it Works.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!