从 node.js 连接到 mongodb 时出现 ECONNREFUSED 错误 [英] ECONNREFUSED error when connecting to mongodb from node.js

查看:28
本文介绍了从 node.js 连接到 mongodb 时出现 ECONNREFUSED 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know I'm doing some very stupid and noobish, but I'm hoping someone can help me set up a basic database connection to mongodb from node.js on a mac.

我已经使用自制软件安装了 mongodb,似乎效果很好.我已经以本地登录用户的身份启动了服务器(mongod),并打开了第二个终端并确认我可以使用 mongo 连接到它.当我运行 mongo 时,我收到消息connecting to: localhost:27017/test",然后是命令提示符.在 mongo shell 中运行了一些命令,一切似乎都在那里工作.让两个终端都打开并运行.

I've installed mongodb using homebrew, seems to have worked quite well. I have started the server (mongod) as the locally logged in user, and opened a second terminal and confirmed that I can connect to it by using mongo. When I run mongo I get the message "connecting to: localhost:27017/test" followed by a command prompt. Ran a few commands in the mongo shell everything seems to be working there. Left both terminals open and running.

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

I've also confirmed that I can reach the web interface at localhost:28017.

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

I installed node.js and added the mongoose package. Now attempting to connect using a super simple node.js app (also running as locally logged in user):

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)

我的头撞在墙上,试图让一些如此简单的事情起作用.我错过了什么?

Banging my head against the wall trying to get something so simple to work. What am I missing?

这是来自 mongod 的日志.如您所见,我尝试了多次,但它们都立即失败了:

Here are the logs from mongod. As you can see I tried multiple times and they're all failing rather instantaneously:

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)

推荐答案

好的,这是我在上面发布的信息中没有真正公开的另一个案例.我的 node.js 应用程序 非常简单,但我在 node.js 代码中包含了另外几行,这显然导致了这个问题.

OK, this was another case of not being truly forthcoming in the info I posted above. My node.js app was very simple, but I was including another couple lines in my node.js code that apparently caused this issue.

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

Specifically, I had another variable declared which was calling some other code that made a separate database call using incorrect db info. This is why, when using Xinzz's code, the console log error seemed not to change. It wasn't actually the mongoose.connect command that was throwing the error!

吸取教训,定位问题并注释掉不相关的代码!对不起,伙计们,我知道这是我愚蠢.

Lesson learned, localize the problem and comment out unrelated code! Sorry guys, I knew this was me being dumb.

这篇关于从 node.js 连接到 mongodb 时出现 ECONNREFUSED 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆