Search code examples
node.jsmongodbmongoosekeystonejs

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


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)

Solution

  • 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