Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

363
Views
MongoServerError: Authentication failed when using mongo and mongo express with docker containers

I am learning docker so I was following one of the video it shown that how to connect your js app with mongoDB when it is running in docker containers . Now little variation I did is that it shown to connect mongo on system port 27017 which is default mongo port but the problem is I don't want to connect to my system installed mongo but to the container mongo , so for that purpose I decided to run mongo container on port 3535. When I did so mongo-express successfully got connected to mongo but when I am trying to connect it with my js app(using mongoose ) it is continuously showing errors with authentication error I have checked password is correct , no brackets issue is their , the command I used to turn up my docker containers are

docker run -d -p 3535:27017 --name mongoDB --network databases -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=admin mongo

and another one for mongo-express is

docker run -d -p 8081:8081 --name mongoExp --network databases -e ME_CONFIG_BASICAUTH_USERNAME=admin -e ME_CONFIG_BASICAUTH_PASSWORD=admin -e ME_CONFIG_MONGODB_SERVER=mongoDB -e ME_CONFIG_MONGODB_AUTH_USERNAME=admin -e ME_CONFIG_MONGODB_AUTH_PASSWORD=admin mongo-express

My node app code looks like

const mongoose = require('mongoose');
const {Humans}= require("./models/humans.js")
mongoose.connect('mongodb://admin:admin@localhost:3535/user', {useNewUrlParser: true, useUnifiedTopology: true }).then(()=>{
    console.log("connected to db");
})

let data={
    name:"Vaibhav Jain",
    mob:"9801",
    skills:"coding"
}

const express = require('express')
const app = express();
app.set('view engine', 'ejs');


app.get("/",async(req,res)=>{
    // console.log(Humans);
    // await Humans.insertMany( );
    // await Humans.insertMany({name:"Mobius" , mob:"5908922",skills:"drawing"});
    const result = await Humans.find();
    res.send(result);   
    
})
app.get("/home",(req,res)=>{
    res.render("home", data);
})

app.listen(3000,()=>{
    console.log("listening port");
})
Also one thing i would like to mention I have created a user database which have a collection human , this all I created from UI but I wanted to connect it to my app using mongoose , so for that Human which is shown to be imported is actually empty schema (read that from somewhere that you can use existing collections in such a way please correct me on this part also if I am wrong ) Just for refrence current my docker is in this state enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You may not have created user for the database user after created it using UI. In your case, you can run this query to create a normal user.
> use user
> db.createUser({user: "user", pwd: "user", roles:["dbOwner"]})
After that, you can connect to user database using this string mongodb://user:user@localhost:3535/user

Check out document db.CreateUser.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!