Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError: connect ECONNREFUSED 127.0.0.1:27017

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] 

enter image description here

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.

like image 601
Deepika P. Avatar asked Oct 02 '17 09:10

Deepika P.


People also ask

What is connect Econnrefused?

ECONNREFUSED error means that connection could not be made with the target service (in your case localhost:8080 ). Check your service running on port 8080.

How do I access MongoDB on localhost?

To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.


2 Answers

This happened probably because the MongoDB service isn't started. Follow the below steps to start it:

  1. Go to Control Panel and click on Administrative Tools.
  2. Double click on Services. A new window opens up.
  3. Search MongoDB.exe. Right click on it and select Start.

The server will start. Now execute npm start again and the code might work this time.

like image 126
Sonu Sourav Avatar answered Sep 28 '22 08:09

Sonu Sourav


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.))

like image 29
Farid Ansari Avatar answered Sep 28 '22 08:09

Farid Ansari