nodejs应用程序-mongodb连接失败,错误为"ECONNREFUSED"; [英] nodejs application - mongodb connection fails with error "ECONNREFUSED"
问题描述
背景信息
我正在玩我的第一个试图连接到远程mongodb的nodejs测试应用程序.
I'm playing around with my first nodejs test app that is attempting to connect to a remote mongodb.
问题:
连接失败,并显示以下消息:
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
这是连接的逻辑:
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;
}
});
}
到目前为止我已检查的内容:
-
我确保数据库服务器正在使用端口27017.这是我重新启动数据库时在mongodb日志中看到的内容:
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**
我已经通过启动"mongo" ...然后使用"use test"/"use admin"命令切入和切出测试数据库来确保有一个名为"test"的数据库.
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.
我不相信我已打开授权.这是mongo.conf文件中的内容:
I don't believe I have authorization turned on. This is what I have in the mongo.conf file:
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
为了证明这一点,为了通过命令行进入服务器上的测试数据库,我只需要键入"mongo",然后默认情况下,我就在测试数据库中.因此,我认为我可以确定我没有进行任何身份验证.
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.
-
以防万一,我还检查了netstat的端口号:
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:〜$
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:~$
我正在阅读手册,以查看是否还有其他应检查的内容.我还在阅读各种类似的stackoverflow帖子,但是我还没有找到答案.
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.
有什么建议吗?
推荐答案
我在您的日志中看到Mongo正在监听127.0.0.1.这可能是您的问题.您将必须更改bind-ip地址配置设置,以允许Mongo侦听特定或所有IP地址.您可以在此处找到操作方法:无法使用计算机ip连接到mongodb
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
这篇关于nodejs应用程序-mongodb连接失败,错误为"ECONNREFUSED";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!