gpt4 book ai didi

node.js - 为什么连接到 Node 中的 Mongo Db 需要这么长时间?

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:53 24 4
gpt4 key购买 nike

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

请注意,记录器用于调试。 Node Mongo 驱动程序的 future 版本应该可以解决这个问题。

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!

最佳答案

我开始在本地开发机器上体验这一点,并且使用非常复杂的 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)

关于node.js - 为什么连接到 Node 中的 Mongo Db 需要这么长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22416428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com