1

I am new to node js development, recently tried a basic program to connect node js with mongodb. I have used "mongoose" to connect mongodb from node.

I have the following questions before get into the db connections. There are two error case while starting the node app

case 1 - there is an possibility that mongodb not get started, in that case node server it self will not get started. Sample error message is like

    mongoose/node_modules/mongodb/lib/server.js:242
            process.nextTick(function() { throw err; })
                                                ^
    Error: connect ECONNREFUSED
        at errnoException (net.js:905:11)
        at Object.afterConnect [as oncomplete] (net.js:896:19)
    [nodemon] app crashed - waiting for file changes before starting...



case 2 - mongodb may be started while starting node app, but it may get disconnected/stopped by some unavoidable situations. in that case if we made any db query, node server get killed. sample error message is like

    DisconnectedError: Ran out of retries trying to reconnect to "localhost:27017". Try setting `server.reconnectTries` and `server.reconnectInterval` to something higher.
        at MongooseError.DisconnectedError

Please let me know how to handle these two error cases. (some sample code/links would be more helpful)

PS : I tried to surf solution for this, but could not get the proper solution (or) i may be searched with improper keywords!

9
  • I believe the proper word you're looking for isn't use case but something like error case. A use case is something that benefits the application/system/thing you're trying to build. E.g. the use case of mongo db for your node app would be to store large amounts of data in a specified format, which is more scalable and easier to use than the file system. Commented Jul 30, 2016 at 19:50
  • Thanks for correcting @Rdesmond yes i meant for error case! will edit the post too and i am poor at forming words please bear it :) Commented Jul 30, 2016 at 19:53
  • Also, not going to answer because these questions are difficult to answer and dependent on your application needs (concurrency vs. availability in CAP for example), and I don't have extensive experience architecting/managing large systems. That said, I'd say that several possible solutions would be redundancy/failover systems. Commented Jul 30, 2016 at 20:00
  • Case 1, you can use a load balancer and have multiple node servers runnng. Case 2, a failed db query should never make the web server fail. In fact, some databases like Cassandra have failure tolerance so that if a single instance (instance being a "mongodb" process on a machine) goes down (with a database running on multiple machines), queries can still be made beause the other instances have the data stored on the failed machine. Commented Jul 30, 2016 at 20:00
  • i think you have misunderstood my question. load balancer & multiple nodes will not solve because, my question is db itself not get started while starting server. Say, at some point of time both node server and mongo db got killed. during that time, we are trying to start node server alone. That case, node server will not get start and it will get into loop till mongo db starts. How do we avoid/capture this case instead get into looping is my question! Hope i explained clear! Commented Jul 30, 2016 at 20:04

1 Answer 1

1

I met this connection error when I restart mongodb using mongoose in nodejs.

connection error: { 

    [DisconnectedError: 
        Ran out of retries trying to reconnect to "127.0.0.1:27017". 
        Try setting `server.reconnectTries` and `server.reconnectInterval` 
        to something higher. ]

    message: 'Ran out of retries trying to reconnect to "127.0.0.1:27017". 
              Try setting `server.reconnectTries` and `server.reconnectInterval` 
              to something higher.',

    name: 'DisconnectedError' 

}


From: http://mongoosejs.com/docs/connections.html

Note: If auto_reconnect is on, mongoose will give up trying to reconnect after a certain number of failures. Set the server.reconnectTries and server.reconnectInterval options to increase the number of times mongoose will try to reconnect.

// Good way to make sure mongoose never stops trying to reconnect

mongoose.connect(uri, { server: { reconnectTries: Number.MAX_VALUE } });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Eddy will check

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.