Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs application - mongodb connection fails with error "ECONNREFUSED"

Background Information

I'm playing around with my first nodejs test app that is attempting to connect to a remote mongodb.

Problem:

The connection is failing with the following message:

admin@testdev:~/Documents/nodejs_tests$ sudo nodejs index.js
Server has started.
Request for / received
About to route a request for: /
inside db connect method
Request for /favicon.ico received
About to route a request for: /favicon.ico
inside db connect method
MongoError: connect ECONNREFUSED
null
MongoError: connect ECONNREFUSED
null

Here's the logic to connect:

function connect_nimble() {
        console.log("inside db connect method");
        MongoClient.connect("mongodb://10.1.1.1:27017/test", function(err,db) {
                console.log(err);
                console.log(db);
                if(!err) {
                        console.log("booya!  Connected to mongo");
                        return true;
                }
        });
}

What I've checked so far:

  1. I've made sure that the database server is using port 27017. Here's what I see in the mongodb logs when i restart the database:

    admin@mongotest:~$ tail -f /var/log/mongodb/mongod.log 
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] 
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] db version v3.0.4
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] git version: 0481c958daeb2969800511e7475dc66986fa9ed5
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] build info:  Linux ip-10-45-73-23 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] allocator: tcmalloc
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
    **2015-07-21T09:52:41.470-0500 I NETWORK  [initandlisten] waiting for connections on port 27017**
    
  2. I've made sure I have a database called "test" by starting "mongo"... and then switching in and out of the test database using the "use test" / "use admin" commands.

  3. I don't believe I have authorization turned on. This is what I have in the mongo.conf file:

# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true

And to prove it, in order to get into the test database on the server via command line, I just have to type "mongo" and then by default, I'm in the test database. So I think I can safely assume I don't have any authentication going on.

  1. Just in case, I also checked netstat for the port number:

    admin@mongotest:~$ sudo netstat -taupen | grep mongo tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 117 6465150 18792mongod
    admin@mongotest:~$

I'm currently reading the manual to see if there's anything else I should be checking. I'm also reading through the various stackoverflow posts that are similar, but I haven't found me an answer yet.

Any suggestions?

like image 708
dot Avatar asked Jul 21 '15 15:07

dot


1 Answers

I can see in your logs that Mongo is listening on 127.0.0.1. This is likely your problem. You will have to change the bind-ip address configuration setting to allow Mongo to listen to specific or all IP addresses. You can find how to do this here: Cannot connect to mongodb using machine ip

like image 156
tier1 Avatar answered Oct 21 '22 20:10

tier1