Reputation: 15349
It's taking about 1.2mins (76690ms) to connect to my mongo db. I'm wondering if someone could look at this little test script to see if there is something in my config causing this or if you have some other ideas as to why the connects are so slow.
Note the loggers are for debugging. Future versions of the Node Mongo driver are supposed to clean this mess up.
var Mongo = require('mongodb');
var MongoClient = Mongo.MongoClient;
var options = {
uri_decode_auth: true,
db: {
retryMiliSeconds: 3000, numberOfRetries: 1,
logger: {
doDebug:true,
doError:true,
doLog:true,
error: function(message, object) {
console.log('db error', message, object);
},
debug: function(message, object) {
console.log('db debug', message, object);
},
log: function(message, object) {
console.log('db log', message, object);
},
}
},
server: {
readPreference: Mongo.ReadPreference.SECONDARY_PREFERRED,
auto_reconnect: true,
socketOptions: { keepAlive: 1 },
logger: {
doDebug:true,
doError:true,
doLog:true,
error: function(message, object) {
console.log('server error', message);
},
debug: function(message, object) {
console.log('server debug', message);
},
log: function(message, object) {
console.log('server log', message);
},
}
},
replSet: {
rs_name: 'my_repl_set',
retries: 10,
readPreference: Mongo.ReadPreference.SECONDARY_PREFERRED,
socketOptions: { keepAlive: 1 },
strategy: 'ping',
logger: {
doDebug:true,
doError:true,
doLog:true,
error: function(message, object) {
console.log('replSet error', message);
},
debug: function(message, object) {
console.log('replSet debug', message);
},
log: function(message, object) {
console.log('replSet log', message);
}
}
}
};
var connString = 'mongodb://username:' + encodeURIComponent('*******') + '@host1:27017,host2:27017,host3:27017/my_db';
console.time('connect');
MongoClient.connect(connString, options, function(err, conn) {
if(err) {
console.timeEnd('connect');
return console.log('error', err.message);
}
console.timeEnd('connect');
console.log('connected!');
});
This outputs:
server debug opened connection
server debug opened connection
server debug opened connection
server debug opened connection
server debug opened connection
server debug writing command to mongodb
server debug opened connection
server debug opened connection
server debug opened connection
server debug opened connection
server debug opened connection
server debug writing command to mongodb
server debug opened connection
server debug opened connection
server debug opened connection
server debug opened connection
server debug opened connection
server debug writing command to mongodb
replSet debug opened connection
replSet debug closed connection
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
server debug writing command to mongodb
connect: 76690ms
connected!
Upvotes: 2
Views: 2555
Reputation: 926
I started to experience this on a local dev machine and with a very complex Node app, it took a while to debug. The timing was very similar, around maybe 70seconds.
Outputting the Mongo log file, I occasionally saw:
[HostnameCanonicalizationWorker] Failed to obtain name info for: [ 192.168.0.21, "nodename nor servname provided, or not known"), (192.168.0.21, "nodename nor servname provided, or not known") ]
This made me look at my /etc/hosts file which I had recently edited, as I may have had a typo. Upon inspection the file was heavily corrupt with random characters which was breaking any hostname resolving from this file.
If you experience a similar issue, check that all the hostnames you need to connect to can resolve without issues. Another issue to look into could be IPv6. If you're still having issues, connect to Mongo by IP address instead of hostname, even if it's local (127.0.0.1)
Upvotes: 1