MongoDB 用户 - 身份验证失败

时间:2021-01-16 11:54:57

标签: node.js mongodb

所以我想知道为什么我的所有 MongoDB 用户的身份验证都失败了。一切正常,直到我删除了一个用户,从那时起我的所有用户都被拒绝了。 我使用 Altas 并且我已将我的用户设置为 dbAdmin 并授予了所有权限,经过仔细检查,所以我在密码中没有打字错误,我设置了 IP 地址,确保我正确复制了 url 。 . 我正在尝试与 Nodejs 应用程序连接,这是我的代码:

const express = require("express");
const mongoose = require("mongoose");
const app = express();
// Connect to DB
const mongoURI =
    "mongodb+srv://MyUserName:<MyPassword>@cluster0.lbb8n.mongodb.net/<DbImUsing>?retryWrites=true&w=majority";
mongoose.connect(mongoURI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
// Route
app.set("view engine", "ejs");
app.get("/", (req, res) => {
    res.render("index");
});
// Endpoint
app.post("/shortUrls", (req, res) => {});
app.listen(process.env.PORT || 5000);

我收到的错误消息是:

(node:5058) UnhandledPromiseRejectionWarning: MongoError: bad auth : Authentication failed.
    at MessageStream.messageHandler (/Users/fridavbg/Desktop/MovingWorlds/urlShortv2/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (/Users/fridavbg/Desktop/MovingWorlds/urlShortv2/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/Users/fridavbg/Desktop/MovingWorlds/urlShortv2/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (_stream_writable.js:352:12)
    at MessageStream.Writable.write (_stream_writable.js:303:10)
    at TLSSocket.ondata (_stream_readable.js:719:22)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:284:9)
    at TLSSocket.Readable.push (_stream_readable.js:223:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5058) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5058) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

2 个答案:

答案 0 :(得分:0)

尝试扩展连接

useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,

还要检查您的 MongoDB 中的网络访问。 enter image description here

添加您当前的 IP 地址。

答案 1 :(得分:0)

查看服务器日志以了解身份验证失败的原因。

如果您在免费或共享层 (m0/m2/m5),日志不可用,您必须猜测。

由于您使用的是 SRV URI,请尝试明确指定身份验证源:

    "mongodb+srv://MyUserName:<MyPassword>@cluster0.lbb8n.mongodb.net/<DbImUsing>?retryWrites=true&w=majority&authSource=admin"