我在使用 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 应用程序中使用 Mongoose 连接到 MongoDB Atlas 的问题。
1。验证连接 URI
确保您的连接 URI 格式正确。它应该看起来像这样:
mongodb+srv://<username>:<password>@mycluster.gmoxw.mongodb.net/myTodoApp?retryWrites=true&w=majority
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 已列入白名单,请仔细检查:
(0.0.0.0/0)
用于测试目的(不建议用于
生产)。5。检查防火墙和网络问题
如果您所在的网络阻止某些端口或连接,这可能会导致问题。确保您的防火墙设置允许 MongoDB Atlas 端口上的出站连接(默认为 27017)。
6。 SSL/TLS 配置
既然您提到添加
tlsAllowInvalidCertificates
=true,请考虑以下事项:
tlsAllowInvalidCertificates
。7。调试步骤
ping mycluster.gmoxw.mongodb.net
如果失败,则可能是网络问题。
8。替代连接方法
如果上述步骤未能解决问题,请尝试不使用 mongodb+srv 进行连接:
mongodb://<username>:<password>@host1:port1,host2:port2/myTodoApp?replicaSet=myReplicaSet&ssl=true&authSource=admin
9。进一步排除故障
如果问题仍然存在:
npm uninstall mongoose
npm install mongoose