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)

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, how can you catch the error and print something useful?
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.
I see, well if someone runs into this error in my app, hopefully they'll google it and find this :)
Actually mongoose for this project, not using express yet.
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.
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.

1 Comment

I cannot believe this is not mentioned in an obvious location....ridiculous. Thanks for the answer.
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

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.