gpt4 book ai didi

node.js - NodeJS与mongoDB的连接错误

转载 作者:IT老高 更新时间:2023-10-28 13:11:31 26 4
gpt4 key购买 nike

我在将我的 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 端口不是默认端口

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

关于node.js - NodeJS与mongoDB的连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29303482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com