5

刚刚在我的 Mac 上重新安装了 Mongodb(全新安装山狮 10.8),现在我的应用程序需要大约 3 分钟才能连接。

我整理了一个简单的节点脚本来测试它:

var start = (new Date()).getTime();

var mongoose = require('mongoose');

 var db = mongoose.connect('mongodb://localhost/passport-mongox',function(err){
  var stop = (new Date()).getTime();

  console.log('Took this long: ',(stop-start) / 1000 );
 });

两次都是 175.273 和 175.316 秒。

当我连接到外部托管的 mongodb 时,它会在不到一秒的时间内连接,

知道为什么会发生这种情况吗?这是我的 mongo.log:

Fri Feb  1 12:43:25 [initandlisten] MongoDB starting : pid=2262 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=w
Fri Feb  1 12:43:25 [initandlisten] db version v2.2.2, pdfile version 4.5
Fri Feb  1 12:43:25 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
Fri Feb  1 12:43:25 [initandlisten] build info: Darwin bs-osx-106-x86-64-1.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
Fri Feb  1 12:43:25 [initandlisten] options: { bind_ip: "127.0.0.1", config: "/usr/local/etc/mongod.conf", dbpath: "/usr/local/var/mongodb", logappend: "true", logpath: "/usr/local/var/log/mongodb/mongo.log" }
Fri Feb  1 12:43:25 [initandlisten] journal dir=/usr/local/var/mongodb/journal
Fri Feb  1 12:43:25 [initandlisten] recover : no journal files present, no recovery needed
Fri Feb  1 12:43:26 [websvr] admin web console waiting for connections on port 28017
Fri Feb  1 12:43:26 [initandlisten] waiting for connections on port 27017
Fri Feb  1 12:44:05 [initandlisten] connection accepted from 127.0.0.1:52137 #1 (1 connection now open)
Fri Feb  1 12:44:40 [initandlisten] connection accepted from 127.0.0.1:52152 #2 (2 connections now open)
Fri Feb  1 12:45:15 [initandlisten] connection accepted from 127.0.0.1:52201 #3 (3 connections now open)
Fri Feb  1 12:45:50 [initandlisten] connection accepted from 127.0.0.1:52298 #4 (4 connections now open)
Fri Feb  1 12:46:25 [initandlisten] connection accepted from 127.0.0.1:52325 #5 (5 connections now open)
Fri Feb  1 12:51:26 [conn5] end connection 127.0.0.1:52325 (4 connections now open)
Fri Feb  1 12:51:26 [conn3] end connection 127.0.0.1:52201 (4 connections now open)
Fri Feb  1 12:51:26 [conn4] end connection 127.0.0.1:52298 (4 connections now open)
Fri Feb  1 12:51:26 [conn1] end connection 127.0.0.1:52137 (4 connections now open)
Fri Feb  1 12:51:26 [conn2] end connection 127.0.0.1:52152 (4 connections now open)
4

2 回答 2

6

所以答案来自推特上的@AdamMeghji

我的主机文件一直是这样的:

127.0.0.1 localhost
127.0.0.1 test.com
127.0.0.1 wes.dev

我将其切换为:

127.0.0.1 localhost test.com wes.dev

连接又回到了 0.015 秒。

于 2013-02-01T18:58:19.070 回答
3

来自mongoose.js的回答

原因:底层 MongoDB 驱动程序默认查找 IPv6 地址,因此最可能的原因是您的 localhost DNS 映射未配置为处理 IPv6。

解决方案:使用 127.0.0.1 代替 localhost 或使用连接文档中所示的家庭选项。

mongoose.connect(url, {family:4}, function(err, connection) { connection.db(your_db_name); });

于 2019-04-23T12:48:45.077 回答