how to fix: 'MongoError: authentication fail' @MongoDB Atlas

how to fix: 'MongoError: authentication fail' @MongoDB Atlas

我正在连接到 MongoDB Atlas 并收到身份验证失败错误。

这是我的连接字符串:

mongodb://user:<password>@mongo-cluster-shard-00-00-ixqtu.mongodb.net:27017,mongo-cluster-shard-00-01-ixqtu.mongodb.net:27017,mongo-cluster-shard-00-02-ixqtu.mongodb.net:27017/test?ssl=true&replicaSet=mongo-cluster-shard-0&authSource=admin&retryWrites=true

这就是我得到的:

------------------------------------------------

    Mongoose connection "error" event fired with:

    { MongoError: authentication fail
        at Function.MongoError.create (/mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb-core/lib/error.js:31:11)
        at /mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/replset.js:1245:38
        at /mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:760:7
        at /mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:736:20
        at finish (/mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb-core/lib/auth/scram.js:168:16)
        at handleEnd (/mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb-core/lib/auth/scram.js:178:7)
        at /mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb-core/lib/auth/scram.js:269:11
        at /mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:469:18
        at process._tickCallback (internal/process/next_tick.js:61:11)
      name: 'MongoError',
      message: 'authentication fail',
      errors:
       [ { name: 'mongo-cluster-shard-00-01-ixqtu.mongodb.net:27017',
           err: [Error] },
         { name: 'mongo-cluster-shard-00-00-ixqtu.mongodb.net:27017',
           err: [Error] } ] }
    Error: KeystoneJS (Keystone Demo) failed to start - Check that you are running `mongod` in a separate process.
        at NativeConnection.<anonymous> (/mnt/c/WEB/keystone-md2/node_modules/keystone/lib/core/openDatabaseConnection.js:62:10)
        at NativeConnection.emit (events.js:189:13)
        at /mnt/c/WEB/keystone-md2/node_modules/mongoose/lib/connection.js:824:17
        at connectCallback (/mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:527:5)
        at /mnt/c/WEB/keystone-md2/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:459:13
        at process._tickCallback (internal/process/next_tick.js:61:11)

您必须将 userpassword 放入您的连接 uri 字符串中

 mongodb://***'your user':' here comes password '***@mongo-cluster-shard-00-00-ixqtu.mongodb.net:27017,mongo-cluster-shard-00-01-ixqtu.mongodb.net:27017,mongo-cluster-shard-00-02-ixqtu.mongodb.net:27017/test?ssl=true&replicaSet=mongo-cluster-shard-0&authSource=admin&retryWrites=true

例如

 mongodb://dbuser:dbpassword@mongo-cluster-shard-00-00-ixqtu.mongodb.net:27017,mongo-cluster-shard-00-01-ixqtu.mongodb.net:27017,mongo-cluster-shard-00-02-ixqtu.mongodb.net:27017/test?ssl=true&replicaSet=mongo-cluster-shard-0&authSource=admin&retryWrites=true

问题是我在连接字符串中留下了 <>。您必须删除那些才能进行身份验证。

我有一个类似的错误,但连接到新的 Atlas 数据库。尽管我已经按照此处和文档中的说明正确设置了用户名和密码(显然我用正确的 MLAB 密码替换了 PASSWORD:

var mongoURI = 'mongodb+srv://heroku_3kcdl3j9:PASSWORD@cluster-3kcdl3j9.auof1.mongodb.net/heroku_3kcdl3j9?retryWrites=true&w=majority';

我已将我的数据库从 MLAB 迁移到 Atlas,并将正确的网络访问设置成功设置为 0.0.0.0 IP 地址。在 Heroku 中设置环境变量。但仍然出现此错误:

    { MongoError: authentication fail
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/topologies/replset.js:1462:15
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:868:7
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:844:20
    at finish (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:232:16)
    at handleEnd (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:242:7)
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:351:15
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:531:18
    at process._tickCallback (internal/process/next_tick.js:61:11)
  name: 'MongoError',
  message: 'authentication fail',
  errors:
   [ { name: 'cluster-3kcdl3j9-shard-00-01.auof1.mongodb.net:27017',
       err: [MongoError] },
     { name: 'cluster-3kcdl3j9-shard-00-00.auof1.mongodb.net:27017',
       err: [MongoError] },
     { name: 'cluster-3kcdl3j9-shard-00-02.auof1.mongodb.net:27017',
       err: [MongoError] } ],
  [Symbol(mongoErrorContextSymbol)]: {} }
(node:47015) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 's' of undefined
    at Admin.buildInfo (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb/lib/admin.js:100:37)
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/index.js:95:13
    at $initialConnection.then.err (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongoose/lib/connection.js:556:14)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:47015) 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(). (rejection id: 1)
(node:47015) [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.
(node:47015) UnhandledPromiseRejectionWarning: MongoError: authentication fail
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/topologies/replset.js:1462:15
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:868:7
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:844:20
    at finish (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:232:16)
    at handleEnd (/Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:242:7)
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/auth/scram.js:351:15
    at /Users/bensmith/Downloads/DocumentsDirNew/Scraper and API/diveapi/node_modules/mongodb-core/lib/connection/pool.js:531:18
    at process._tickCallback (internal/process/next_tick.js:61:11)
(node:47015) 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(). (rejection id: 2)

我也遇到了这个问题。但是一个小时后我才知道我使用了 process.env 变量作为用户名和密码。并且还在 .env 文件中声明了它们。但是我忘了在我的代码中配置dotenv包。

所以请确保您没有犯这种愚蠢的错误。

有关更多信息,请查看节点存储库的文档。 https://www.npmjs.com/package/dotenv

我遇到了类似的错误,在我的情况下,问题是我在“安全 -> 网络访问”页面的“IP 访问列表”中添加(列入白名单)错误的 IP 地址。