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 😉