1

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
VAIBHAV JAIN
  • 29
  • 1
  • 5

1 Answers1

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.

Viettel Solutions
  • 1,519
  • 11
  • 22
  • Why would I use to create a new document in "User" collection if I have already created it from UI ? doesn't both of them will be connected to same? even if I accept what are you saying than do I need to go inside the container from their would be require to create the user if I am not wrong ? – VAIBHAV JAIN Dec 08 '21 at 19:29
  • 1
    I think you may mistaken record (document) with `user` of a database. Everytime you create a new database, you will have to create user who can access that database with associated permissions. The account you provided by `MONGO_INITDB_ROOT_USERNAME` and `MONGO_INITDB_ROOT_PASSWORD` is called superuser account (root), it let you connect as admin permission to manage overall databases, like listing database, creating new database, creating users who can access that database... And it's not the same as `user` of a sepecific database – Viettel Solutions Dec 09 '21 at 04:28