0

I would like to connect to my mongodb database from node js. I'm new to Mongo, for my project I decided to put my database on an Ubuntu server. I have a user who is "admin" who has the admin role but when I connect from nodeJS creating a new database mongo returns an authentication error.

my NodeJS code:

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

error returned by mongo:

Erreur de connexion : MongoServerError: Invalid database name: 'messio/'
    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: 73,
  codeName: 'InvalidNamespace',
  connectionGeneration: 0,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}
  • 1
    Have you enabled authentication in MongoDB, if that's the case, then which type? Is auto create DB enabled? Just by querying a none-existing DB, it will not be created, you need to add "something" / content, to it, for it to be created. https://www.mongodb.com/basics/create-database#option-2 – Anuga Jan 13 '23 at 12:20
  • @Anuga I think you mix "database" with "instance". A "database" in MongoDB is created automatically. A Instance is created when you start your MongoDB. The link you provided refers MongoDB Atlas, however here it runs on own premises. – Wernfried Domscheit Jan 13 '23 at 16:00
  • Check the [ConnectionString](https://www.mongodb.com/docs/manual/reference/connection-string/). Perhaps your password contains character `@` or `/` which causes in invalid connection string. Maybe use `encodeURIComponent( process.env.DB_USER_PASS)` – Wernfried Domscheit Jan 13 '23 at 16:04
  • @WernfriedDomscheit, nah no matter if its a local installation or cloud hosted, you still need to create the `db`, by putting something into it. His clearly trying to access the `db` before its even been created. If it was an authentication error, it would look like this: https://stackoverflow.com/questions/45576367/mongoose-connection-authentication-failed – Anuga Jan 15 '23 at 23:44
  • Again, the ConnectionString has wrong format! `"mongodb://password@2.56.247.250:27017/dbname/"` is not valid. Apart from `mongodb://` there can be only one slash `/`, you have two. Also error message *MongoServerError: Invalid database name: 'messio/'* does not match to your code. – Wernfried Domscheit Jan 16 '23 at 07:19
  • @Anuga that's definitely wrong (at least for the mongosh). [Create a Database](https://www.mongodb.com/docs/manual/core/databases-and-collections/#create-a-database): "*If a database does not exist, MongoDB creates the database when you first store data for that database. [...] The insertOne() operation creates both the database myNewDB and the collection myNewCollection1 if they do not already exist.*" When you run `mongosh "mongodb://localhost:27017/dummy" --quiet --eval "db.adminCommand( { listDatabases: 1 }).databases.filter( x => x == 'dummy')"` then result is `[]` (no error) – Wernfried Domscheit Jan 16 '23 at 07:26
  • I reinstalled all the Mongoose server and I get a new problem. I can connect to my DB from compass but I can't connect from nodeJS. I have an authentication error... – Arnaud Derison Jan 16 '23 at 08:56

0 Answers0