Skip to content

Instantly share code, notes, and snippets.

@anonymoustafa
Created May 27, 2022 14:09
Show Gist options
  • Save anonymoustafa/a49a24e45f9569e4bdc7057fdcbe3c62 to your computer and use it in GitHub Desktop.
Save anonymoustafa/a49a24e45f9569e4bdc7057fdcbe3c62 to your computer and use it in GitHub Desktop.
Solution to the MongoDB authentication error

You can find the same question here I just encountered this error:

    Server is running on port: 5000
MongoServerError: bad auth : Authentication failed.
    at Connection.onMessage (/home/mostafa/documents/local-repositories/mern-mongodb-tutorial/server/node_modules/mongodb/lib/cmap/connection.js:202:30)
    at MessageStream.<anonymous> (/home/mostafa/documents/local-repositories/mern-mongodb-tutorial/server/node_modules/mongodb/lib/cmap/connection.js:62:60)
    at MessageStream.emit (node:events:527:28)
    at processIncomingData (/home/mostafa/documents/local-repositories/mern-mongodb-tutorial/server/node_modules/mongodb/lib/cmap/message_stream.js:108:16)
    at MessageStream._write (/home/mostafa/documents/local-repositories/mern-mongodb-tutorial/server/node_modules/mongodb/lib/cmap/message_stream.js:28:9)
    at writeOrBuffer (node:internal/streams/writable:390:12)
    at _write (node:internal/streams/writable:331:10)
    at Writable.write (node:internal/streams/writable:335:10)
    at TLSSocket.ondata (node:internal/streams/readable:766:22)
    at TLSSocket.emit (node:events:527:28) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError',
  [Symbol(errorLabels)]: Set(1) { 'HandshakeError' }

Solution: If you've got a config.env file in your project server side directories, where you have put your ATLAS_URI Just login to your https://cloud.mongodb.com account and beside your Cluster name, you would find a green button named Connect, there, click on Connect your application and copy the connection string. Go back to the config.env file and update the ATLAS_URI like below: ATLAS_URI=mongodb+srv://<username>:<password>@sandbox.jadwj.mongodb.net/employees?retryWrites=true&w=majority Now I think you are good to go 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment