在 Node.js 中使用 Mongoose 连接到 MongoDB Atlas 时出现问题

问题描述 投票:0回答:1

我在使用 Mongoose 将 Node.js 应用程序连接到 MongoDB Atlas 时遇到问题。以下是问题的详细信息以及我迄今为止所尝试的内容:

背景: MongoDB Atlas URI(已清理):mongodb+srv://***:***@mycluster.gmoxw.mongodb.net/myTodoApp?retryWrites=true&w=majority Node.js 版本:v18.16.0 猫鼬版本:7.x.x MongoDB Atlas Cluster:我通过 MongoDB Compass 验证了连接是否有效。 白名单 IP 地址:我当前的 IP 已在 MongoDB Atlas 中列入白名单。

收到错误:

[nodemon] starting `node app.js`
Connecting to DB with URL: mongodb+srv://***:***@mycluster.gmoxw.mongodb.net/myTodoApp?
server running on PORT 3000
Failed to connect to db
node:internal/assert:14
    throw new ERR_INTERNAL_ASSERTION(message);
    ^

Error [ERR_INTERNAL_ASSERTION]: This is caused by either a bug in Node.js or incorrect usage of Node.js internals.
Please open an issue with this stack trace at https://github.com/nodejs/node/issues

    at new NodeError (node:internal/errors:399:5)
    at assert (node:internal/assert:14:11)
    at internalConnectMultiple (node:net:1077:3)
    at Timeout.internalConnectMultipleTimeout (node:net:1565:3)
    at listOnTimeout (node:internal/timers:571:11)
    at process.processTimers (node:internal/timers:512:7) {
  code: 'ERR_INTERNAL_ASSERTION'
}

Node.js v18.16.0
[nodemon] app crashed - waiting for file changes before starting...

我也尝试ping MongoDB Atlas集群(mycluster.gmoxw.mongodb.net),但总是找不到主机。

我尝试过的步骤: 将 tlsAllowInvalidCertificates=true 添加到连接字符串。 已验证 MongoDB Atlas 中的 IP 白名单是正确的。 通过 MongoDB Compass 连接确认集群正常工作。 删除了已弃用的选项 useNewUrlParser 和 useUnifiedTopology。 检查 Node.js 和 Mongoose 版本是否兼容。 尽管如此,我仍然遇到同样的错误。

问题: 可能是什么原因导致此错误,如何修复它? 我是否应该尝试任何其他配置或选项才能使连接正常工作? 这可能与我的 Node.js 版本或 MongoDB Atlas 的 SSL 问题有关吗? 预先感谢您的帮助!

我尝试在 Node.js 应用程序中使用 Mongoose 库连接到 MongoDB Atlas。我希望连接成功并允许我的应用程序访问数据库。但是,我不断收到内部断言错误 (ERR_INTERNAL_ASSERTION) 并且连接失败。我还尝试添加 tlsAllowInvalidCertificates=true 并验证集群在 MongoDB Compass 中工作,但问题仍然存在。

node.js mongodb mongoose connection mongodb-atlas
1个回答
0
投票

这里有一种结构化的方法来排查和解决在 Node.js 应用程序中使用 Mongoose 连接到 MongoDB Atlas 的问题。

1。验证连接 URI

确保您的连接 URI 格式正确。它应该看起来像这样:

mongodb+srv://<username>:<password>@mycluster.gmoxw.mongodb.net/myTodoApp?retryWrites=true&w=majority
  • 将 和 替换为您实际的 MongoDB Atlas 凭证。
  • 确保没有多余的空格或不正确的字符。

2。检查 Node.js 和 Mongoose 版本

确保您使用的是 Node.js 和 Mongoose 的兼容版本。由于您使用的是 Node.js v18.16.0,因此使用 Mongoose 7.x.x 应该没问题。但是,请务必检查 Mongoose 文档以获取任何特定于版本的注释。

3.正确使用 mongoose.connect

确保您在代码中正确使用

mongoose.connect
方法。例如:

const mongoose = require('mongoose');

const uri = 'mongodb+srv://<username>:<password>@mycluster.gmoxw.mongodb.net/myTodoApp?retryWrites=true&w=majority';

mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log('Connected to MongoDB');
  })
  .catch((err) => {
    console.error('Failed to connect to db', err);
  });

4。将您的 IP 地址列入白名单

既然您提到您的 IP 已列入白名单,请仔细检查:

  • 转到 MongoDB Atlas 中的网络访问并确保您当前的 IP 是 已列出。
  • 如果您的 IP 经常更改,请考虑将所有 IP 列入白名单 暂时
    (0.0.0.0/0)
    用于测试目的(不建议用于 生产)。

5。检查防火墙和网络问题

如果您所在的网络阻止某些端口或连接,这可能会导致问题。确保您的防火墙设置允许 MongoDB Atlas 端口上的出站连接(默认为 27017)。

6。 SSL/TLS 配置

既然您提到添加

tlsAllowInvalidCertificates
=true,请考虑以下事项:

  • 通常情况下,您不需要添加此参数,除非您 使用自签名证书。
  • 确保您的连接字符串不包含不必要的选项。 如果不需要,请删除
    tlsAllowInvalidCertificates

7。调试步骤

  • Ping 集群: 在终端中使用 ping 来检查连接:
ping mycluster.gmoxw.mongodb.net

如果失败,则可能是网络问题。

  • 检查 Node.js 错误堆栈: 在中查找更具体的错误详细信息 堆栈跟踪,这可能指出潜在的问题。

8。替代连接方法

如果上述步骤未能解决问题,请尝试不使用 mongodb+srv 进行连接:

  1. 从 MongoDB Atlas 获取标准连接字符串。
  2. 从 MongoDB Atlas 获取标准连接字符串。
mongodb://<username>:<password>@host1:port1,host2:port2/myTodoApp?replicaSet=myReplicaSet&ssl=true&authSource=admin

9。进一步排除故障

如果问题仍然存在:

  • 重新安装依赖:有时候,重新安装mongoose就可以解决 隐藏的问题。
npm uninstall mongoose
npm install mongoose
  • 检查 MongoDB Atlas 状态: 访问 MongoDB 状态页面以确保 服务没有持续的问题。
© www.soinside.com 2019 - 2024. All rights reserved.