与mongoDB的NodeJS连接错误

我有一个问题,连接我的socketIO应用程序(使用nodeJS)与我的mongoDB。 我尝试在远程服务器上连接,但它会引发错误

这是我的代码(在mongoDB中没有设置用户/密码):

var url = "mongodb://192.168.1.5:27017/DB" MongoClient.connect(url, function(err, db) { console.log("test") if (!err) { console.log("test"); } else { console.dir(err) throw err } // db.close(); }); 

在这里,当我启动服务器,我试图启动导航器中的应用程序:服务器端口80听:

 { [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' } /root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:228 process.nextTick(function() { throw err; }) ^ Error at Error.MongoError (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17) at Server.destroy (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:629:47) at Server.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:344:17) at Db.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:267:19) at /root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:196:12 at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:226:9) at g (events.js:180:16) at emit (events.js:98:17) at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:238:68) at g (events.js:180:16) 

这个错误是返回几个错误,如:

  • 服务器没有运行
  • 你需要authentication用户
  • 这个数据库不存在
  • mongodb端口不是默认端口

检查这个。 通常你的问题只是这些原因之一

为了这个工作,你必须在/etc/mongod.conf中进行修改

注释bind_ip = 127.0.0.1就好像这行在没有评论它只听本地接口。

其中一个解决scheme是将127.0.0.1更改为公共IP或路由器在/ etc中的mongodbconfiguration文件中提供的任何内容

ECONNREFUSED错误可能有几个原因。 检查点

  • 检查您的端口是否没有服务任何其他进程。
  • 检查你的mongod是否在运行。
  • 检查你的localHostconfiguration是否正确与你的ID。

为更多你可以看到这一点