11

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?

2
  • Your netstat output clearly shows mongod is listening on only 127.0.0.1 interface. You need to change your connect string to mongodb://127.0.0.1:27017/test. Commented Jul 21, 2015 at 15:33
  • see my answer: stackoverflow.com/a/37994794/3166417 Commented Jun 23, 2016 at 14:39

2 Answers 2

5

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

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

1 Comment

Or, as one of the comments suggests, if your Node service is running on the same machine as Mongo, change your connection string to localhost: mongodb://127.0.0.1:27017/test
0

Open a terminal.

Type mongod & and check the stacktrace.

This is an example of a failing start

I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating I NETWORK [initandlisten] shutdown: going to close listening sockets... I NETWORK [initandlisten] shutdown: going to flush diaglog... I CONTROL [initandlisten] now exiting

To fix this, Create the folder /data/db

In the terminal, type mkdir /data/db

Type againmongod

It works !!

[initandlisten] ** WARNING: Access control is not enabled for the database. I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. I CONTROL [initandlisten] I CONTROL [initandlisten] I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

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.