尝试连接时 MongoDB Atlas 数据库中的身份验证错误

Authetification erro in MongoDB Atlas database when trying to connect

我尝试使用 Mozila Express 教程中 设置 Mongo 数据库 部分的设置步骤来设置和使用 Mongo Atlas你可以找到 here,然后我在 运行 该教程中提到的 populatedb.js 文件时遇到身份验证错误:

This script populates some test books, authors, genres and bookinstances to your database. Specified database as argument - e.g.: populatedb mongodb+srv://cooluser:coolpassword@cluster0.a9azn.mongodb.net/local_library?retryWrites=true
MongoDB connection error: MongoError: Authentication failed.
    at MessageStream.messageHandler (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:314:20)
    at processIncomingData (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at MessageStream.Writable.write (_stream_writable.js:318:11)
    at TLSSocket.ondata (_stream_readable.js:718:22)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 11, high_: 1620398123 },
    signature: { hash: [Binary], keyId: [Long] }
  },
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 11, high_: 1620398123 }
}
(node:12916) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.
    at MessageStream.messageHandler (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:314:20)
    at processIncomingData (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at MessageStream.Writable.write (_stream_writable.js:318:11)
    at TLSSocket.ondata (_stream_readable.js:718:22)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
(node:12916) 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:12916) [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.
FINAL ERR: MongooseError: Operation `authors.insertOne()` buffering timed out after 10000ms
node populatedb mongodb+srv://notmyfault
okok:%40mongoDB1@cluster1.zbmx1.mongodb.net/local_library?retryWrites=true
This script populates some test books, authors, genres and bookinstances to your database. Specified database as argument - e.g.: populatedb mongodb+srv://cooluser:coolpassword@cluster0.a9azn.mongodb.net/local_library?retryWrites=true
MongoDB connection error: MongoError: Authentication failed.
    at MessageStream.messageHandler (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:314:20)
    at processIncomingData (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at MessageStream.Writable.write (_stream_writable.js:318:11)
    at TLSSocket.ondata (_stream_readable.js:718:22)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1620398181 },
    signature: { hash: [Binary], keyId: [Long] }
  },
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1620398181 }
}
(node:12935) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.
    at MessageStream.messageHandler (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:314:20)
    at processIncomingData (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/pc/Documents/node_express_tutorial/local_library_app_tutorial/express-locallibrary-tutorial/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at MessageStream.Writable.write (_stream_writable.js:318:11)
    at TLSSocket.ondata (_stream_readable.js:718:22)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
(node:12935) 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:12935) [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.
FINAL ERR: MongooseError: Operation `authors.insertOne()` buffering timed out after 10000ms
[1]+  Done                    node populatedb mongodb+srv://notmyfaultokok:%40mongoDB1@cluster1.zbmx1.mongodb.net/local_library?retryWrites=true

我所做的是在 mongoDB Atlas 的创建集群中单击连接按钮,复制 url 以连接到应用程序。将passworld字段和数据库名称字段更改为mongoDB-Atlas密码和集群中创建的数据库名称。

所以我没有得到身份验证错误,因为我认为我在数据库中输入了正确的密码 url 而且我还检查 url 编码是否正确。

请帮忙

我现在解决了这个错误,其实就是可以为创建的数据库创建用户和密码。所以我使用的是 Atlas 网站的用户名和密码,而不是数据库本身的用户名和密码。新的是,我为该数据库创建了一个新数据库以及用户名和密码,我可以将该数据库用于我的应用程序并且运行良好!

但是我用来得出解决方案的步骤并不是直截了当的。实际上,我开始研究如何为 mongoDB 数据库实施搜索引擎,并找到了一个教程,要求我通过 mongoDB 罗盘进行连接。我用mongoDB指南针连接图集用户名和密码,没用。但后来我想,这不可能。然后我完全删除了密码,它似乎可以连接!

后来我又试了一遍速成教程,把密码去掉,还是报cannot have empty string password的错误。但后来我想我只是要创建一个新帐户。并尝试一下。当我创建新帐户和新数据库时,他们实际上要求我为新数据库创建用户名和密码!这是我发现您也有创建数据库的用户名和密码的地方。

我不知道之前发生了什么,可能是因为我很久以前创建数据库而忘记了数据库也使用用户名和密码。

所以它已解决,我将继续教程的第 4 部分。