为什么连接到Node中的Mongodb需要这么长时间?

时间:2014-03-14 21:54:25

标签: node.js mongodb debugging

它需要大约1.2分钟(76690毫秒)才能连接到我的mongo数据库。我想知道是否有人可以查看这个小小的测试脚本,看看我的配置中是否有某些内容导致这种情况,或者您是否有其他想法知道连接速度如此之慢。

注意记录器用于调试。 Node Mongo驱动程序的未来版本应该清理这个混乱。

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!');
});

输出:

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!

1 个答案:

答案 0 :(得分:0)

我开始在本地开发机器上遇到这种情况,并且使用非常复杂的Node应用程序,需要一段时间才能进行调试。 时间非常相似,大概可能是70秒。

输出Mongo日志文件,我偶尔会看到:

[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") ]

这让我看看我最近编辑的/ etc / hosts文件,因为我可能有一个错字。在检查时,文件被随机字符严重损坏,这破坏了从该文件解析的任何主机名。

如果您遇到类似问题,请检查您需要连接的所有主机名是否可以解决而不会出现问题。 另一个需要考虑的问题可能是IPv6。 如果您仍然遇到问题,请通过IP地址而不是主机名连接到Mongo,即使它是本地的(127.0.0.1)