Authentication fail when connecting to Atlas MongoDB

535 views Asked by At

I have an error when connecting to the MongoDB Atlas database after I migrated the data from the old MLAB.

I have definitely setup the username and password correctly as in the documentation (obviously i replaced PASSWORD with my correct MLAB password:

var mongoURI = 'mongodb+srv://heroku_3kcdl3j9:[email protected]/heroku_3kcdl3j9?retryWrites=true&w=majority';

I have migrated my database from MLAB to Atlas successfully set the correct Network access settings to 0.0.0.0 IP address. Setup the environment variable in Heroku.

I connect to the atlas database with this code, do I need some special options? (this code works with the old MLAB connection)

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);
      })
});

However, I still get this error, I don't know what I am doing wrong:

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

There are 2 answers

1
Saxon On

On the migration documentation from MLAB to ATLAS I can read:

Atlas servers always run with requireSSL and only accept TLS/SSL encrypted connections.

I can see that your ssl option is commented. This is one problem.

0
Ben Smith On

The problem was it was something to do with the username and password. I believe there was an illegal character in the password. So I created another user with a better password and the server authenticated and logged in allowing it to connect.