node.js - 无法使用 NodeJS 从 CentOS 连接到 MongoDB Atlas

标签 node.js mongodb centos mongodb-atlas

我在 MongoDB Atlas 上创建了一个集群,并使用 Compass 成功连接到它,还使用本地运行的 Node。
当我尝试在我的 CentOS 机器上运行相同的代码时,我得到一个 ECONNREFUSED错误。这是我的 Node 代码

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://myuser:mypass@tablematic-0-z4nuk.gcp.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  console.log("ERROR: " + err);

  client.close();
});
myusermypass在这里替换为我的实际用户名和密码。

我已经为端口 27015 打开了服务器的防火墙, 2701627017 .
Atlas 设置为接受来自任何传入 IP 的连接。

实在想不出是什么原因造成的...

最佳答案

我不是 100% 确定,但我的连接字符串通常是
mongodb://user:pass@host.app:27017/db_name

关于node.js - 无法使用 NodeJS 从 CentOS 连接到 MongoDB Atlas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60517947/

相关文章:

c++ - 使用 CMake ExternalProject_Add 构建 mongo-cxx-driver

mysql - 如何从 Perl 连接到 MySQL?

java - 通过java运行的执行程序似乎超时?

linux - 为什么 "echo [#10]"等于 1?

javascript - 仅使用服务器端脚本(无浏览器)解释 Javascript 代码

javascript - Async/Await with Request-Promise 返回 Undefined

node.js - Node : loading ES Modules and native addons in the same project

node.js - 如何解决在 TeamCity 中创建 FileSet 时出现的 NAnt 错误?

python - Elasticsearch 没有给出准确的结果python

mongodb - Mongoose 创建多个索引