一尘不染

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

node.js

我知道我正在做一些非常愚蠢和愚蠢的事情,但是我希望有人可以帮助我建立从Mac上的node.js到mongodb的基本数据库连接。

我已经使用自制软件安装了mongodb,看起来效果很好。我已经以本地登录用户身份启动了服务器(mongod),并打开了第二个终端,并确认可以使用mongo连接到该服务器。当我运行mongo时,出现消息“正在连接:localhost:27017
/ test”,然后显示命令提示符。在mongo shell中运行一些命令,一切似乎都在那儿工作。左两个端子均打开并运行。

我还确认可以访问localhost:28017的Web界面。

我安装了node.js并添加了猫鼬包。现在尝试使用超级简单的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)

阅读 312

收藏
2020-07-07

共1个答案

一尘不染

好的,这是我上面发布的信息中没有真正出现的另一种情况。我的Node.js应用程序
非常简单的,但我包括我的Node.js代码另一对夫妇线,显然导致出现此问题。

具体来说,我声明了另一个变量,该变量正在调用其他代码,这些代码使用不正确的数据库信息进行了单独的数据库调用。这就是为什么在使用Xinzz的代码时,控制台日志错误似乎没有改变的原因。引发错误的实际上不是mongoose.connect命令!

获得的经验教训,定位问题并注释掉不相关的代码!对不起,我知道这是我的愚蠢。

2020-07-07