【问题标题】:Authentication fail when connecting to Atlas MongoDB连接到 Atlas MongoDB 时身份验证失败
【发布时间】:2021-01-18 20:20:17
【问题描述】:

从旧 MLAB 迁移数据后,连接到 MongoDB 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 中设置环境变量。

我使用此代码连接到 atlas 数据库,是否需要一些特殊选项? (此代码适用于旧的 MLAB 连接)

mongoose.connect(mongoURI, 
  // { config: { autoIndex: true } }, 
  // { options : { ssl: true } }, 
  function (error) {
    if (error) console.error(error);
    else console.log('mongo connected');

     const con = new mongoose.mongo.Admin(mongoose.connection.db)
        con.buildInfo( (err, mongoURI) => {
        if(err){
          throw err
        }
       // see the db version
        // console.log('mongo db.version():  '+ db.version);
      })
});

但是,我仍然得到这个错误,我不知道我做错了什么:

    { 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)

【问题讨论】:

  • 您的凭据错误或未正确转义,或者用户不存在。阅读服务器日志以了解它对这种情况的看法。

标签: mongodb authentication mongodb-atlas mlab


【解决方案1】:

关于从 MLAB 到 ATLAS 的迁移文档,我可以阅读:

Atlas 服务器始终使用 requireSSL 运行,并且只接受 TLS/SSL 加密连接。

我可以看到您的 ssl 选项已被注释。 这是一个问题。

【讨论】:

  • 我已尝试使用此选项,但没有任何区别......还改变了我现在连接的方式以使用 MongoClient,正如它在 Atlas 上所说的那样,这也没有修复它,我完全卡住了: const client = new MongoClient(mongoURI, { useNewUrlParser: true }, { options : { ssl: true } }); client.connect(err => {
【解决方案2】:

问题在于它与用户名和密码有关。我相信密码中有非法字符。所以我用更好的密码创建了另一个用户,服务器经过身份验证并登录允许它连接。

【讨论】:

    猜你喜欢
    • 2015-08-29
    • 2019-05-09
    • 1970-01-01
    • 2019-06-22
    • 2018-09-25
    • 2020-06-07
    • 2019-09-16
    • 2021-04-27
    相关资源
    最近更新 更多