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.

Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51
Prata
  • 1,250
  • 2
  • 16
  • 31
  • Add details about event.js file – Niroshan Ranapathi Dec 30 '16 at 10:44
  • I think it is the nodejs built in events.js (eventEmitter class). I dont know where it is. – Prata Dec 30 '16 at 10:50
  • follow this http://stackoverflow.com/questions/8904991/mongodb-cant-connect-to-localhost-but-can-connect-to-localhosts-ip-address – Niroshan Ranapathi Dec 30 '16 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. – shijin Oct 13 '17 at 05: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. – binki Aug 03 '18 at 03:12

4 Answers4

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
});
Prata
  • 1,250
  • 2
  • 16
  • 31
  • 3
    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 };` – Maxim Mar 26 '18 at 06:18
  • For me none of the above worked so had to downgrade and this one worked out: https://stackoverflow.com/a/46699144/2427266 – Rishul Matta Apr 23 '18 at 14:31
  • The [options](https://mongoosejs.com/docs/connections.html#options) available if you want to check if it stills supported –  Aug 04 '20 at 22:41
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.

Shreya Batra
  • 730
  • 1
  • 6
  • 15
3

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

Lucas Assmann
  • 41
  • 1
  • 8
  • how do I do it with nodejs driver?like this? MongoClient.connect("mongodb://localhost:27017/articledb", { keepAlive: 30000, connectTimeoutMS: 30000, }, function(err, db) {}) – harryfeng Jun 04 '17 at 22:32
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.

Daniel Staleiny
  • 424
  • 4
  • 6