I have changed the env file name to .env.local and I am replacing the MONGODB_URI with the same I got from MongoDB atlas, connect to your application, and clicking on Drivers.
I have tried with this URI: MONGODB_URI = mongodb+srv://<user>:<password>@rgcluster.x7ixk.mongodb.net/?retryWrites=true&w=majority
I also tried with this URI to mention the the collection name: MONGODB_URI = mongodb+srv://<user>:<password>@rgcluster.x7ixk.mongodb.net/sample_mflix?retryWrites=true&w=majority
both are not working and showing that I am not connected to the database.
Hi @Remy_Ghalayini and welcome to MongoDB community forums!!
Based on the error message you have posted in the above thread, it seems there could be an authentication error.
It would be helpful if you could clarify more on the following:
Are you using the right username and password attached to the connection staring?
Are you able to connect outside of the application using mongosh or compass ?
Could you help me with the steps you have followed to create user and whitelisting the IPs on Atlas?
Below is an example that shows the correct connection string to use while making the connection with the database:
mongodb+srv://test:test@cluster0.4zdj5vb.mongodb.net/?retryWrites=true&w=majority
where test and test are my username and password.
yes i am using the right username and password. I double checked.
Yes i am able to connect outside the application. I have a similar project in react using the classical server client architecture and the connection to MongoDB works perfectly.
I have created a user and a database just like the tutorial. I have added a snapshot to show the whitelisted IP in Network Access.
I tried to follow the same documentation and with correct username and password I was successfully able to make the connection.
However, when I mis spelled my username in the .env. I see the same error as yours.
⚠ Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/messages/fast-refresh-reload
MongoServerError: bad auth : Authentication failed.
Could you share the error logs that you are observing while you perform the npm start.
Also, could you share the node js version that you are using.
Thanks for the reply. I am using node v20.7.0. I am definitely sure of my username and password and double-checked them multiple times. I have no console errors, but I get this error in the terminal when i navigate to localhost.
MongoServerError: bad auth : authentication failed
at Connection.onMessage (C:\Users\remyg\Programming\githubRepositories\ShoppingList_Next_mongodb\node_modules\mongodb\lib\cmap\connection.js:207:30)
at MessageStream.<anonymous> (C:\Users\remyg\Programming\githubRepositories\ShoppingList_Next_mongodb\node_modules\mongodb\lib\cmap\connection.js:60:60)
at MessageStream.emit (node:events:514:28)
at processIncomingData (C:\Users\remyg\Programming\githubRepositories\ShoppingList_Next_mongodb\node_modules\mongodb\lib\cmap\message_stream.js:132:20)
at MessageStream._write (C:\Users\remyg\Programming\githubRepositories\ShoppingList_Next_mongodb\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:399:12)
at _write (node:internal/streams/writable:340:10)
at Writable.write (node:internal/streams/writable:344:10)
at TLSSocket.ondata (node:internal/streams/readable:785:22)
at TLSSocket.emit (node:events:514:28)
at addChunk (node:internal/streams/readable:343:12)
at readableAddChunk (node:internal/streams/readable:316:9)
at Readable.push (node:internal/streams/readable:253:10)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
ok: 0,
code: 8000,
codeName: 'AtlasError',
[Symbol(errorLabels)]: Set(1) { 'HandshakeError' }```
Based on the error logs, it seems there is an authentication error in the connection URI.
Can you help me with the code snippet where you are trying to make the connection (possibly the .env file).
I found the issue. I hardcoded the uri in server/index.js file and I was successfully connected. When I tried to debug the issue, I noticed that my password was not retrieved properly from .env.local file. The reason is that my password contains a special character “$” and this was not read properly. So I changed my password and it is now working.