繁体   English   中英

mongoDB的NodeJS连接错误

[英]NodeJS connection error with mongoDB

将我的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)

对于以下几种错误,将返回此错误:

  • 服务器未运行
  • 您需要验证用户
  • 该数据库不存在
  • mongodb端口不是默认端口

检查一下。 通常,您的问题只是这些原因之一

为此,您必须在/etc/mongod.conf中进行更改

comment bind_ip = 127.0.0.1好像该行中没有注释它,仅侦听本地接口。

解决方案之一是将127.0.0.1更改为公用ip或路由器在/ etc中的mongodb配置文件中为您提供的任何内容

ECONNREFUSED错误的原因可能很少。 检查点

  • 检查您的端口是否没有任何其他进程。
  • 检查您的mongod是否正在运行。
  • 检查您的本地主机是否正确配置了您的ID。

更多,你可以看到这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM