与mongoDB的NodeJS连接错误

Jef*_*low 12 mongodb node.js socket.io

我有一个问题,我的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();
   });
Run Code Online (Sandbox Code Playgroud)

这是当我启动服务器并尝试在导航器中启动应用程序时:服务器在端口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)
Run Code Online (Sandbox Code Playgroud)

thr*_*n19 14

多个错误都会返回此错误,例如:

  • 服务器未运行
  • 您需要对用户进行身份验证
  • 这个数据库不存在
  • mongodb端口不是默认端口

检查一下.Normaly你的问题只是其中一个原因


rah*_*arg 7

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

comment bind_ip = 127.0.0.1 好像这行没有注释掉它只听本地接口.