1
resposta

Dúvida sobre Autenticação do Mongo Atlas

No vídeo "Instalando o projeto" da Primeira aula, ao seguir o passo a passo da professora e executar o comando "npm run dev", aparece o seguinte erro no terminal:

PS D:\Projetos\api-express-mongo> npm run dev

> alura-node@1.0.0 dev
> nodemon server.js

[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
(node:4464) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:4464) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning.    
Servidor escutando em http://localhost:3000
Erro de conexão MongoServerError: bad auth : authentication failed
    at Connection.onMessage (D:\Projetos\api-express-mongo\node_modules\mongodb\lib\cmap\connection.js:231:30)
    at MessageStream.<anonymous> (D:\Projetos\api-express-mongo\node_modules\mongodb\lib\cmap\connection.js:61:60)
    at MessageStream.emit (node:events:524:28)
    at processIncomingData (D:\Projetos\api-express-mongo\node_modules\mongodb\lib\cmap\message_stream.js:125:16)
    at MessageStream._write (D:\Projetos\api-express-mongo\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:572:12)
    at _write (node:internal/streams/writable:501:10)
    at Writable.write (node:internal/streams/writable:510:10)
    at TLSSocket.ondata (node:internal/streams/readable:1009:22)
    at TLSSocket.emit (node:events:524:28) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError',
  connectionGeneration: 0,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}
node:internal/process/promises:394
    triggerUncaughtException(err, true /* fromPromise */);
    ^

MongoServerError: bad auth : authentication failed
    at Connection.onMessage (D:\Projetos\api-express-mongo\node_modules\mongodb\lib\cmap\connection.js:231:30)
    at MessageStream.<anonymous> (D:\Projetos\api-express-mongo\node_modules\mongodb\lib\cmap\connection.js:61:60)
    at MessageStream.emit (node:events:524:28)
    at processIncomingData (D:\Projetos\api-express-mongo\node_modules\mongodb\lib\cmap\message_stream.js:125:16)
    at MessageStream._write (D:\Projetos\api-express-mongo\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:572:12)
    at _write (node:internal/streams/writable:501:10)
    at Writable.write (node:internal/streams/writable:510:10)
    at TLSSocket.ondata (node:internal/streams/readable:1009:22)
    at TLSSocket.emit (node:events:524:28) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError',
  connectionGeneration: 0,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}

Node.js v22.13.0
[nodemon] app crashed - waiting for file changes before starting...

O código está certo e verifiquei meu nome de usuário e senha usados para a criação do banco de dados, mas mesmo assim o erro continua. Segue a seguir o dbConnect.js como está:

import mongoose from "mongoose"

mongoose.connect("mongodb+srv://pauloarthur1998:<aqui a senha está correta>@clusterarthur.kvzg2.mongodb.net/ClusterArthur?retryWrites=true&w=majority&appName=ClusterArthur");

let db = mongoose.connection;

export default db;

Qual será o motivo do erro? Quem puder me ajudar agradeço muito!

1 resposta

Olá Paulo pelo erro é uma falha na autenticação que pode estar relacionado a url do banco que você está passando.

Também estou fazendo esse curso e comparando com a minha url após o mongodb.net/ não tem o nome do app e ficaria só com os parâmetros ?retryWrites=true&w=majority&appName=ClusterArthur");

Na sua url tem um ClusterArthur após mongodb.net/ :

  • mongodb+srv://pauloarthur1998:<aqui a senha está correta>clusterarthur.kvzg2.mongodb.net/ClusterArthur?retryWrites=true&w=majority&appName=ClusterArthur

Tenta remover esse nome para ver se dá certo ficando assim:

  • mongodb+srv://pauloarthur1998:<aqui a senha está correta>clusterarthur.kvzg2.mongodb.net/?retryWrites=true&w=majority&appName=ClusterArthur

Espero ter ajudado ;)