从node.js连接到mongodb时出现ECONNREFUSED错误

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (637)

希望有人可以帮助我在mac上从node.js建立与mongodb的基本数据库连接。

我用自制软件安装了mongodb,似乎运行得很好。我已经启动了服务器(mongod)作为本地登录用户,打开了第二个终端并确认我可以使用mongo连接到它。当我运行mongo时,我收到消息“connect to:localhost:27017 / test”,然后是命令提示符。在mongo shell中运行得很好,两个终端都打开并运行。

我也可以访问localhost:28017的web界面。

我安装了node.js并添加了mongoose包。现在尝试使用超级简单的node.js应用程序进行连接(也作为本地登录用户运行):

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

我收到以下错误

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: connect ECONNREFUSED
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)

哪里错了?

编辑:这是来自mongod的日志。我试过很多次,但它们都是瞬间失败的:

Thu Dec  5 08:19:43.700 [initandlisten] MongoDB starting : pid=14412 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=mobadmins-MacBook-Pro-3.local
           08:19:43.700 [initandlisten] db version v2.4.8
           08:19:43.700 [initandlisten] git version: nogitversion
           08:19:43.700 [initandlisten] build info: Darwin mobadmins-MacBook-Pro-3.local 12.4.0 Darwin Kernel Version 12.4.0: Wed May  1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
           08:19:43.700 [initandlisten] allocator: tcmalloc
           08:19:43.700 [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", rest: true }
           08:19:43.700 [initandlisten] journal dir=/usr/local/var/mongodb/journal
           08:19:43.700 [initandlisten] recover : no journal files present, no recovery needed
           08:19:43.729 [websvr] admin web console waiting for connections on port 28017
           08:19:43.729 [initandlisten] waiting for connections on port 27017
           08:22:34.561 [initandlisten] connection accepted from 127.0.0.1:52160 #3 (1 connection now open)
           08:22:34.563 [conn3] recv(): message len 1124073472 is too large. Max is 48000000
           08:22:34.563 [conn3] end connection 127.0.0.1:52160 (0 connections now open)
           08:24:41.298 [initandlisten] connection accepted from 127.0.0.1:52166 #4 (1 connection now open)
           08:24:41.304 [conn4] end connection 127.0.0.1:52166 (0 connections now open)
           08:25:06.938 [initandlisten] connection accepted from 127.0.0.1:52168 #5 (1 connection now open)
           08:25:06.943 [conn5] end connection 127.0.0.1:52168 (0 connections now open)
           08:25:18.220 [initandlisten] connection accepted from 127.0.0.1:52172 #6 (1 connection now open)
           08:25:18.225 [conn6] end connection 127.0.0.1:52172 (0 connections now open)
           08:25:38.811 [initandlisten] connection accepted from 127.0.0.1:52175 #7 (1 connection now open)
           08:25:38.816 [conn7] end connection 127.0.0.1:52175 (0 connections now open)
提问于
用户回答回答于

使用此代码设置您的mongodb连接:

var mongoose = require('mongoose');

var mongoURI = "mongodb://localhost:27017/test";
var MongoDB = mongoose.connect(mongoURI).connection;
MongoDB.on('error', function(err) { console.log(err.message); });
MongoDB.once('open', function() {
  console.log("mongodb connection open");
});

启动服务器时,确保mongod正在运行。您使用Express还是只是一个简单的node.js服务器?您使用上述代码获得的错误消息是什么?

用户回答回答于

如果您的目标数据库在其他机器上,您需要检查IP,防火墙,端口转发等的正确性。

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动