12

I've got a problem to connect my socketIO application (made with nodeJS) with my mongoDB. I try to connect on an remote server, but its throws me error

Here's my code (there's no user/password set in the mongoDB) :

   var url = "mongodb://192.168.1.5:27017/DB"

   MongoClient.connect(url, function(err, db) {
           console.log("test")
           if (!err) {
                   console.log("test");
           }
           else {
                   console.dir(err)
           throw err
           }
   //  db.close();
   });

And here's when I launch the server and I tried to launch the app in a navigator : Server listening at port 80:

{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:228
        process.nextTick(function() { throw err; })
                                            ^
Error
    at Error.MongoError (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17)
    at Server.destroy (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:629:47)
    at Server.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:344:17)
    at Db.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:267:19)
    at /root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:196:12
    at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:226:9)
    at g (events.js:180:16)
    at emit (events.js:98:17)
    at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:238:68)
    at g (events.js:180:16)
alexwlchan
  • 5,699
  • 7
  • 38
  • 49
Jeflow
  • 188
  • 1
  • 1
  • 7

4 Answers4

14

This error is returned for several errors like :

  • server is not running
  • you need to authenticate user
  • this database does not exists
  • the mongodb port is not the default port

Check this. Normaly your problem is just one of these causes

throrin19
  • 17,796
  • 4
  • 32
  • 52
  • Thank you, how can you catch the error and print something useful? – SSH This Mar 29 '15 at 16:36
  • I use mongoose in my nodeJS project but the error log is normally the same. MongoDB library works with callbacks. In NodeJS, you can't catch callback with a standard try/catch. – throrin19 Mar 30 '15 at 06:39
  • I see, well if someone runs into this error in my app, hopefully they'll google it and find this :) – SSH This Mar 30 '15 at 18:41
  • Actually mongoose for this project, not using express yet. – SSH This Mar 31 '15 at 18:16
  • I recently had an error where the first query my node server made was a promise for +-400 individual pieces of data. This caused a connection failure, lesson learned: make sure the connection is established before you do a batch request. – WouldBeNerd Jun 17 '17 at 04:36
7

For this to work you have to make the changes in /etc/mongod.conf

comment bind_ip=127.0.0.1 As if this line in not commented it Listen to local interface only.

rahulgarg
  • 330
  • 3
  • 9
  • I cannot believe this is not mentioned in an obvious location....ridiculous. Thanks for the answer. – Sam Aug 20 '17 at 00:11
0

One of the solutions is to change 127.0.0.1 to public ip or whatever the router provided you with in mongodb config file located in /etc

smj
  • 416
  • 1
  • 4
  • 10
0

There could be few reasons for ECONNREFUSED error. Checkpoints

  • Check if your port is not serving any other process.
  • Check if your mongod is running.
  • Check if you localHost is configured correctly with your id or not.

for more you can see this

Community
  • 1
  • 1
Anshul
  • 9,312
  • 11
  • 57
  • 74