I met this in kubernetes/minikube + nodejs + mongoose environment. The problem was that DNS service was up with a kind of latency. Checking DNS is ready solved my problem.
const dns = require('dns');_x000D_
_x000D_
var dnsTimer = setInterval(() => {_x000D_
dns.lookup('mongo-0.mongo', (err, address, family) => {_x000D_
if (err) {_x000D_
console.log('DNS LOOKUP ERR', err.code ? err.code : err);_x000D_
} else {_x000D_
console.log('DNS LOOKUP: %j family: IPv%s', address, family);_x000D_
clearTimeout(dnsTimer);_x000D_
mongoose.connect(mongoURL, db_options);_x000D_
}_x000D_
});_x000D_
}, 3000);_x000D_
_x000D_
_x000D_
var db = mongoose.connection;_x000D_
var db_options = {_x000D_
autoReconnect:true,_x000D_
_x000D_
poolSize: 20,_x000D_
socketTimeoutMS: 480000,_x000D_
keepAlive: 300000,_x000D_
_x000D_
keepAliveInitialDelay : 300000,_x000D_
connectTimeoutMS: 30000,_x000D_
reconnectTries: Number.MAX_VALUE,_x000D_
reconnectInterval: 1000,_x000D_
useNewUrlParser: true_x000D_
};
_x000D_
(the numbers in db_options are arbitrary found on stackoverflow and similar like sites)