[错误:无法连接到 [localhost:27017]] 从 NodeJS 到 mongodb

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24710490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:38:14  来源:igfitidea点击:

[Error: failed to connect to [localhost:27017]] from NodeJS to mongodb

node.jsmongodb

提问by user2812866

I am having problem connecting to MongoDB from NodeJS using following sample code. I tried running "mongod" with or without sudo but nodejs code still fail to connect. I am able to successfully connect to database using "mongo".

我在使用以下示例代码从 NodeJS 连接到 MongoDB 时遇到问题。我尝试在有或没有 sudo 的情况下运行“mongod”,但 nodejs 代码仍然无法连接。我能够使用“mongo”成功连接到数据库。

Running on : MAC OS 10.6.8

运行于:MAC OS 10.6.8

   var mongoClient = require('mongodb').MongoClient;
   mongoClient.connect("mongodb://localhost:27017/test", function(error, db) {
        if(!error){
             console.log("We are connected");
        }
        else
           console.dir(error);
    });

get following error running above code : [Error: failed to connect to [localhost:27017]]

运行上面的代码时出现以下错误:[错误:无法连接到 [localhost:27017]]

Also tried mongoose but similar error:

也试过猫鼬但类似的错误:

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/test');
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback () {
          console.log("DB connected");
    // yay!
    });

Output: connection error: [Error: failed to connect to [localhost:27017]]

输出:连接错误:[错误:无法连接到 [localhost:27017]]

Here is the log of mongod

这是mongod的日志

mongod --help for help and startup options
2014-07-11T23:33:47.843-0700 kern.sched unavailable
2014-07-11T23:33:47.849-0700 [initandlisten] MongoDB starting : pid=29942 port=27017 dbpath=/data/db 64-bit host=usc8bcc8a0d0b1
2014-07-11T23:33:47.849-0700 [initandlisten]
2014-07-11T23:33:47.849-0700 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2014-07-11T23:33:47.849-0700 [initandlisten] db version v2.6.3
2014-07-11T23:33:47.849-0700 [initandlisten] git version: nogitversion
2014-07-11T23:33:47.849-0700 [initandlisten] build info: Darwin usc8bcc8a0d0b1 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
2014-07-11T23:33:47.849-0700 [initandlisten] allocator: system
2014-07-11T23:33:47.849-0700 [initandlisten] options: {}
2014-07-11T23:33:47.850-0700 [initandlisten] journal dir=/data/db/journal
2014-07-11T23:33:47.850-0700 [initandlisten] recover : no journal files present, no recovery needed
2014-07-11T23:33:47.901-0700 [initandlisten] waiting for connections on port 27017
2014-07-11T23:34:47.901-0700 [clientcursormon] mem (MB) res:48 virt:2810
2014-07-11T23:34:47.901-0700 [clientcursormon]  mapped (incl journal view):320
2014-07-11T23:34:47.901-0700 [clientcursormon]  connections:0

回答by user2812866

Never mind, I was able to resolve the issue by using 127.0.0.1 instead of localhost, not sure why I have to use ip address. My tomcat, apache, redis, and even node server all works using localhost but not the mongodb. Is there is config I need change to make it work using local host?

没关系,我可以通过使用 127.0.0.1 而不是 localhost 来解决这个问题,不知道为什么我必须使用 ip 地址。我的 tomcat、apache、redis 甚至节点服务器都可以使用 localhost 而不是 mongodb。是否有我需要更改的配置才能使用本地主机工作?

回答by Nicolás Sefercheoglou

Try adding 127.0.0.1 localhostin /private/etc/hostsfile.

尝试添加127.0.0.1 localhost/private/etc/hosts文件。

回答by Chhavi Gangwal

回答by yuanfang wang

yes , I came across this error too, and my hosts is as follow: 127.0.0.1 xxxxxx(the ip of my computer) and when I ran npm startin express project, it got an error like that . After I tried to change the mapping of 127.0.0.1to localhost, it had no error.

是的,我也遇到了这个错误,我的主机如下: 127.0.0.1 xxxxxx(the ip of my computer) 当我npm start在 express 项目中运行时,它得到了这样的错误。之后,我试图改变的映射127.0.0.1localhost,它有没有错误。

回答by Mr Washington

Somebody on Github found a solution: Instead of writing:

Github 上有人找到了解决方案:而不是写:

Mongoose.connect(config.database.url);

Write:

写:

Mongoose.connect(config.database.url, {
  keepAlive: true,
  reconnectTries: Number.MAX_VALUE,
  useMongoClient: true
});

(Source https://github.com/Automattic/mongoose/issues/5399)

(来源https://github.com/Automattic/mongoose/issues/5399

回答by The Red Pea

The first time, I made the obvious mistake that mongod(the mongo Server) was not even running.

第一次,我犯了一个明显的错误,即mongod(mongo 服务器)甚至没有运行。

The second time, I had both the Server (mongod) and the Client (mongo) running on my Windows; in separate Command Prompts (of course mongorunning first). mongodand/or its Command Prompt was "hung up" (i.e. the last line only said this, and the last line remained like this despite attempts to connections:)

第二次,我的 Windows 上同时运行了服务器 ( mongod) 和客户端 ( mongo);在单独的命令提示符中(当然mongo首先运行)。mongod和/或其命令提示符被“挂断”(即最后一行只说了这一点,尽管尝试连接,最后一行仍然如此:)

2017-06-20T12:31:02.937-0600 I NETWORK  [thread1] waiting for connections on port 27017

I clicked in the Command Prompt pressed the space-bar to "kick it" , and the Command Prompt printed lines like these:

我在命令提示符中单击并按空格键“踢它”,命令提示符打印如下所示的行:

2017-06-20T12:31:48.517-0600 I NETWORK  [thread1] connection accepted from 127.0.0.1:50260 #1 (1 connection now open)

Which worked!

哪个有效!