nodejs 应用程序 - mongodb 连接失败,错误为“ECONNREFUSED”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31542990/
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 18:59:51  来源:igfitidea点击:

nodejs application - mongodb connection fails with error "ECONNREFUSED"

node.jsmongodbubuntu-14.04

提问by dot

Background Information

背景资料

I'm playing around with my first nodejs test app that is attempting to connect to a remote mongodb.

我正在尝试连接到远程 mongodb 的第一个 nodejs 测试应用程序。

Problem:

问题:

The connection is failing with the following message:

连接失败并显示以下消息:

admin@testdev:~/Documents/nodejs_tests$ sudo nodejs index.js
Server has started.
Request for / received
About to route a request for: /
inside db connect method
Request for /favicon.ico received
About to route a request for: /favicon.ico
inside db connect method
MongoError: connect ECONNREFUSED
null
MongoError: connect ECONNREFUSED
null

Here's the logic to connect:

这是连接的逻辑:

function connect_nimble() {
        console.log("inside db connect method");
        MongoClient.connect("mongodb://10.1.1.1:27017/test", function(err,db) {
                console.log(err);
                console.log(db);
                if(!err) {
                        console.log("booya!  Connected to mongo");
                        return true;
                }
        });
}

What I've checked so far:

到目前为止我检查过的内容:

  1. I've made sure that the database server is using port 27017. Here's what I see in the mongodb logs when i restart the database:

    admin@mongotest:~$ tail -f /var/log/mongodb/mongod.log 
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] 
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] db version v3.0.4
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] git version: 0481c958daeb2969800511e7475dc66986fa9ed5
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] build info:  Linux ip-10-45-73-23 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] allocator: tcmalloc
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
    **2015-07-21T09:52:41.470-0500 I NETWORK  [initandlisten] waiting for connections on port 27017**
    
  2. I've made sure I have a database called "test" by starting "mongo"... and then switching in and out of the test database using the "use test" / "use admin" commands.

  3. I don't believe I have authorization turned on. This is what I have in the mongo.conf file:

  1. 我已经确定数据库服务器正在使用端口 27017。这是我在重新启动数据库时在 mongodb 日志中看到的内容:

    admin@mongotest:~$ tail -f /var/log/mongodb/mongod.log 
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] 
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] db version v3.0.4
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] git version: 0481c958daeb2969800511e7475dc66986fa9ed5
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] build info:  Linux ip-10-45-73-23 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] allocator: tcmalloc
    2015-07-21T09:52:41.452-0500 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
    **2015-07-21T09:52:41.470-0500 I NETWORK  [initandlisten] waiting for connections on port 27017**
    
  2. 我通过启动“mongo”来确保我有一个名为“test”的数据库......然后使用“use test”/“use admin”命令切换进出测试数据库。

  3. 我不相信我已打开授权。这是我在 mongo.conf 文件中的内容:

# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true
# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true

And to prove it, in order to get into the test database on the server via command line, I just have to type "mongo" and then by default, I'm in the test database. So I think I can safely assume I don't have any authentication going on.

为了证明这一点,为了通过命令行进入服务器上的测试数据库,我只需要输入“mongo”,然后默认情况下,我在测试数据库中。所以我想我可以放心地假设我没有进行任何身份验证。

  1. Just in case, I also checked netstat for the port number:

    admin@mongotest:~$ sudo netstat -taupen | grep mongo tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 117 6465150 18792mongod
    admin@mongotest:~$

  1. 以防万一,我还检查了 netstat 的端口号:

    admin@mongotest:~$ sudo netstat -taupen | grep mongo tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 117 6465150 18792mongod
    admin@mongotest:~$

I'm currently reading the manual to see if there's anything else I should be checking. I'm also reading through the various stackoverflow posts that are similar, but I haven't found me an answer yet.

我目前正在阅读手册,看​​看是否还有其他需要检查的内容。我也在阅读各种类似的 stackoverflow 帖子,但我还没有找到答案。

Any suggestions?

有什么建议?

采纳答案by tier1

I can see in your logs that Mongo is listening on 127.0.0.1. This is likely your problem. You will have to change the bind-ip address configuration setting to allow Mongo to listen to specific or all IP addresses. You can find how to do this here: Cannot connect to mongodb using machine ip

我可以在您的日志中看到 Mongo 正在侦听 127.0.0.1。这很可能是你的问题。您必须更改绑定 IP 地址配置设置以允许 Mongo 侦听特定或所有 IP 地址。您可以在此处找到如何执行此操作: Cannot connect to mongodb using machine ip

回答by Garytech

Open a terminal.

打开一个终端。

Type mongod &and check the stacktrace.

键入mongod &并检查堆栈跟踪。

This is an example of a failing start

这是一个失败的开始的例子

I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating I NETWORK [initandlisten] shutdown: going to close listening sockets... I NETWORK [initandlisten] shutdown: going to flush diaglog... I CONTROL [initandlisten] now exiting

I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating I NETWORK [initandlisten] shutdown: going to close listening sockets... I NETWORK [initandlisten] shutdown: going to flush diaglog... I CONTROL [initandlisten] now exiting

To fix this, Create the folder /data/db

要解决此问题,请创建文件夹 /data/db

In the terminal, type mkdir /data/db

在终端中,输入 mkdir /data/db

Type againmongod

再次输入mongod

It works !!

有用 !!

[initandlisten] ** WARNING: Access control is not enabled for the database. I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. I CONTROL [initandlisten] I CONTROL [initandlisten] I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

[initandlisten] ** WARNING: Access control is not enabled for the database. I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. I CONTROL [initandlisten] I CONTROL [initandlisten] I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000