events.js:141
throw er; // Unhandled \'error\' event
MongoError: connection 0 to localhost:27017 timed out
at Function.MongoError.create (/home/ubuntu/scripts/
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
});
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.
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.
you have to use this configs on your connection: keepAlive: 300000, connectTimeoutMS: 30000