I spin up my docker mongodb database locally:
version: "3.8"
services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: name
ports:
- 27017:27017
volumes:
- mongodbdata:/data/db
volumes:
mongodbdata:
I create my connection string with "name" as the initial database:
MONGO_URI = "mongodb://root:password@localhost:27017/name"
I connect to my database:
const mongoose = require("mongoose");
const connectDB = async () => {
try {
mongoose.set("strictQuery", false);
const conn = await mongoose.connect(process.env.MONGO_URI);
console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (error) {
console.log(error);
process.exit(1);
}
};
module.exports = connectDB;
Authentication Failed:
What am I doing wrong? It connects fine when I remove "name" from the connection string, but I need that name to initialize my database name. Without adding "name," the database name initializes to "test."
The flagged question more had to do with the "authentication failed" being the issue, but mine was that it wasn't initializing with the correct name which so happened to give a random authentication failed error. Completely different questions which requires a unique answer. I found the solution myself and it wasn't in the "related" post but the question was closed and now I can't answer my own question...