简体   繁体   English

mongoDB的NodeJS连接错误

[英]NodeJS connection error with mongoDB

I've got a problem to connect my socketIO application (made with nodeJS) with my mongoDB. 将我的socketIO应用程序(由nodeJS制作)与mongoDB连接时出现问题。 I try to connect on an remote server, but its throws me error 我尝试在远程服务器上进行连接,但会抛出错误

Here's my code (there's no user/password set in the 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();
   });

And here's when I launch the server and I tried to launch the app in a navigator : Server listening at port 80: 这是我启动服务器并尝试在导航器中启动应用程序的时间:服务器侦听端口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)

This error is returned for several errors like : 对于以下几种错误,将返回此错误:

  • server is not running 服务器未运行
  • you need to authenticate user 您需要验证用户
  • this database does not exists 该数据库不存在
  • the mongodb port is not the default port mongodb端口不是默认端口

Check this. 检查一下。 Normaly your problem is just one of these causes 通常,您的问题只是这些原因之一

For this to work you have to make the changes in /etc/mongod.conf 为此,您必须在/etc/mongod.conf中进行更改

comment bind_ip=127.0.0.1 As if this line in not commented it Listen to local interface only. comment bind_ip = 127.0.0.1好像该行中没有注释它,仅侦听本地接口。

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

There could be few reasons for ECONNREFUSED error. ECONNREFUSED错误的原因可能很少。 Checkpoints 检查点

  • Check if your port is not serving any other process. 检查您的端口是否没有任何其他进程。
  • Check if your mongod is running. 检查您的mongod是否正在运行。
  • Check if you localHost is configured correctly with your id or not. 检查您的本地主机是否正确配置了您的ID。

for more you can see this 更多,你可以看到这个

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

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