1

I host a Mongo database on an ubuntu server. I created an admin user in order to be able to connect with Nodejs to create a database, add tables, etc. I can connect with mongoDB compass without problems but from nodeJS mongo returns an error.

connect function:

const mongoose = require("mongoose");
mongoose.set("strictQuery", true);
//connect to db
mongoose
    .connect("mongodb://" + process.env.DB_USER_PASS + "@2.56.247.250:27017/?authMechanism=DEFAULT")
    .then(() => console.log('Connecté a la base de donné'))
    .catch((err) => console.log("Erreur de connexion :", err));

Here is the error:

Erreur de connexion : MongoServerError: Authentication failed.
    at Connection.onMessage (C:\Users\arnau\Desktop\messIO\node_modules\mongodb\lib\cmap\connection.js:230:30)
    at MessageStream.<anonymous> (C:\Users\arnau\Desktop\messIO\node_modules\mongodb\lib\cmap\connection.js:61:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (C:\Users\arnau\Desktop\messIO\node_modules\mongodb\lib\cmap\message_stream.js:125:16)
    at MessageStream._write (C:\Users\arnau\Desktop\messIO\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (node:internal/streams/readable:766:22)
    at Socket.emit (node:events:513:28) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  connectionGeneration: 0,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
  • Missing some details here: how are you calling the connect function and passing your credentials? (don't copy paste actual credentials here of course, but the code you are using) – TBA Jan 16 '23 at 10:12
  • What is the connection string? – Joe Jan 16 '23 at 10:17
  • Maybe this one: https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Jan 16 '23 at 10:41
  • I modify the question and add my connection function. But I don't think the problem comes from there. No worries for the credentials I use environment variables – Arnaud Derison Jan 16 '23 at 12:48

2 Answers2

1

The error AuthenticationFailed means that there is a problem with your connection string and your driver cannot connect into it. Check for all details here: https://www.mongodb.com/docs/manual/reference/connection-string/

Potential problems:

  1. Special Chars: From the docs: If the username or password includes the following characters: / ? # [ ] @ those characters must be converted using percent encoding.
  2. username:password format check that your environmental variable is on the right format, with : between username and pass, and no spaces.
  3. Check your auth database: When you created the user, you created on default ("admin" db is the default.) or created on a specific db, using the command use dbname. If you created on a specific db, you might need to add the auth db name on the connection string.

You can try all the above solutions, making a connection using the mongosh command, to verify that your connection string is fine.

Gustavo Garcia
  • 1,905
  • 1
  • 15
  • 27
  • la connexion réussie quand je ne specifie pas de db. Mais mongo créer une db test – Arnaud Derison Jan 16 '23 at 15:55
  • I never used mongoose directly on mongodb engine level. Always inside a database level (specified after /). Maybe, if you access the database admin and create a user for your specific database, you might be able to login. – Gustavo Garcia Jan 16 '23 at 18:03
0

Another possible cause of the problem your set IP address. You can fix this by adding you current IP address in the network access tab.

Marnin_ A
  • 1
  • 2
  • 1
    An explanation with code would be good. – Prathamesh More Mar 04 '23 at 05:50
  • In a scenario like this it wouldn't be a code problem but rather one that can be fixed through the MongoDB atlas site. At the bottom left of the page you should see a network access tab. After clicking on it you'll see any IP addresses you've added. Delete any extras leaving only one and on the last one, click the edit button. Click the button that say's include you're current IP address. That's it I hope this was helpful. – Marnin_ A Mar 05 '23 at 16:28