5

I am connecting to MongoDB Atlas and getting authentication fail error.

that is my connection string:

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

That is what I get:

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

    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)
evil-cat
  • 111
  • 1
  • 1
  • 9

5 Answers5

6

You have to put your user and password in your connection uri string

 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

e.g

 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
Muhammad Shareyar
  • 772
  • 2
  • 7
  • 21
5

The problem is I was leaving <> in connection string. You have to remove those for authentication to work.

evil-cat
  • 111
  • 1
  • 1
  • 9
3

I was facing this issue too. But after an hour I came to know that I used the process.env variables for username and password. And also declared them in the .env file. But I forgot to configure dotenv package in my code.

So make sure you didn't make this kind of silly mistake.

For more info go through this documentation of node repositories. https://www.npmjs.com/package/dotenv

1

I have a similar error but with connecting to the new Atlas database. Even though I have definitely setup the username and password correctly as stated here and in the documentation (obviously i replaced PASSWORD with my correct MLAB password:

var mongoURI = 'mongodb+srv://heroku_3kcdl3j9:PASSWORD@cluster-3kcdl3j9.auof1.mongodb.net/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 addresss. Setup the environment variable in Heroku. But still get this error:

    { 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)
Ben Smith
  • 521
  • 3
  • 15
  • 2
    I have the exact same issue. I have multiple components, running on Google Kubernetes, all using the same environment variable to connect to the MongoDB Atlas M10 database (migration from mLab). All components are connecting successfully except one that is throwing the MongoError: Authentication failed. Did you figure out how to resolve this? – Sherif Ali Dec 03 '20 at 20:04
  • I have exactly the same issue. One app connects the other does not, even though I copy and paste URL. – fruitloaf Jun 10 '21 at 00:24
1

I had a similar error and in my case, the problem was I have added (whitelisted) a wrong IP address in "IP Access List" on "Security -> Network Access" page.