15
events.js:141 throw er; // Unhandled 'error' event MongoError: connection 0 to localhost:27017 timed out at Function.MongoError.create (/home/ubuntu/scripts/node_modules/mongodb-core/lib/error.js:29:11) at Socket.<anonymous> (/home/ubuntu/scripts/node_modules/mongodb-core/lib/connection/connection.js:184:20) at Socket.g (events.js:260:16) at emitNone (events.js:67:13) at Socket.emit (events.js:166:7) at Socket._onTimeout (net.js:318:8) at _runOnTimeout (timers.js:524:11) at _makeTimerTimeout (timers.js:515:3) at Timer.unrefTimeout (timers.js:584:5) 

Well there is no error during connection, but when try to save some models/collections it runs for a while and then it throws this error. BTW I also have another node process connected to the same mongodb server. Any help is highly appreciated.

5
  • Add details about event.js file Commented Dec 30, 2016 at 10:44
  • I think it is the nodejs built in events.js (eventEmitter class). I dont know where it is. Commented Dec 30, 2016 at 10:50
  • follow this stackoverflow.com/questions/8904991/… Commented Dec 30, 2016 at 10:53
  • If your query is taking a long time that is seconds, then there is some serious flaw in your system or you are doing more things an usual API is meant to do. Try using indexing in mongo collection to improve performance. Commented Oct 13, 2017 at 5:12
  • 2
    Some people get this error when the query is “long/heavy” (2s-5s) but not long enough that it should actually trigger the default timeout of 30s. For me, this went away when I switched to useMongoClient: true from ≥mongoose-4.11. Commented Aug 3, 2018 at 3:12

4 Answers 4

18
const mongoose = require('mongoose'); const option = { socketTimeoutMS: 30000, keepAlive: true, reconnectTries: 30000 }; const mongoURI = process.env.MONGODB_URI; mongoose.connect(mongoURI, option).then(function(){ //connected successfully }, function(err) { //err handle }); 
Sign up to request clarification or add additional context in comments.

3 Comments

They are deprecated as in mongoose 5.x, you can now put them at the top level of the option map. const options = { keepAlive: 300000, connectTimeoutMS: 30000 };
For me none of the above worked so had to downgrade and this one worked out: stackoverflow.com/a/46699144/2427266
The options available if you want to check if it stills supported
5

Your query is taking a long time. And mongo itself has a default time out set. So it times out, if the query takes longer than the timeout time.

Comments

3

you have to use this configs on your connection: keepAlive: 300000, connectTimeoutMS: 30000

1 Comment

how do I do it with nodejs driver?like this? MongoClient.connect("mongodb://localhost:27017/articledb", { keepAlive: 30000, connectTimeoutMS: 30000, }, function(err, db) {})
0

If you are sure that you exported port -p 27017:2017 and it still doesn't work.

Check your VPN if it is not blocking Local Network sharing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.