Cannot connect to MongoDB atlas when following the Next.js MongoDB tutorial

Hi all, I’m new to MongoDB and Next.js and am following the MongoDB Next.js integration tutorial

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.

I also get this error in the terminal:

ok: 0,
  code: 8000,
  codeName: 'AtlasError',
  [Symbol(errorLabels)]: Set(1) { 'HandshakeError' }
}

I have disabled my firewall as I thought it might be affecting the connection, but no success.

Do you know what is the problem?

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:

  1. Are you using the right username and password attached to the connection staring?
  2. Are you able to connect outside of the application using mongosh or compass ?
  3. 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.

I believe the documentation on Configuring user on Atlas and Whitelisting the IPs would be a good starting point to read.

Warm Regards
Aasawari

Hi Aaasawari, answering your question:

  1. yes i am using the right username and password. I double checked.
  2. 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.
  3. 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.

Note that I am using windows 11.

Hi @Remy_Ghalayini and thank you for getting back.

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.

{ ok: 0,
  code: 8000,
  codeName: 'AtlasError',
  connectionGeneration: 0,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}

Also, I see the error logs as:

 ⚠ 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.

Regards,
Aasawari

hi Aasawari,

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' }```

Hi @Remy_Ghalayini
Thank you for sharing the logs

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).

Regards
Aasawari

Hi Aasawari,

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.

Thank you for your help and assistance.

2 Likes