1
version: '3.8' 

services:
  mongodb:
    image: mongo
    container_name: mongodb-server
    environment:
      - MONGO_INITDB_ROOT_USERNAME=athul
      - MONGO_INITDB_ROOT_PASSWORD=athul
    volumes:
      - ~/mongo/data:/data/db
    networks:
      - backend-ecom
  
  backendapi:
    image: athulmekkoth/backend:v3
    container_name: backend-server 
    ports:
      - "5000:5000"  
    networks:
      - backend-ecom  
    env_file:
      - .env
    depends_on:
      - mongodb

networks:
  backend-ecom:
    driver: bridge

Hey all this is my docker compose file when i try to connect to the Mongodb i am getting error as

MongoServerError: not authorized on test to execute command { find: "users", filter: { email: "[email protected]" }, projection: {}, limit: 1, singleBatch: true, batchSize: 1, lsid: { id: UUID("335d544a-5e1c-427e-b6ac-a961a3bf4726") }, $db: "test" }        
backend-server  |     at Connection.onMessage (/app/node_modules/mongodb/lib/cmap/connection.js:227:30)
backend-server  |     at MessageStream.<anonymous> (/app/node_modules/mongodb/lib/cmap/connection.js:60:60)
backend-server  |     at MessageStream.emit (node:events:517:28)
backend-server  |     at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
backend-server  |     at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
backend-server  |     at writeOrBuffer (node:internal/streams/writable:392:12)
backend-server  |     at _write (node:internal/streams/writable:333:10)
backend-server  |     at Writable.write (node:internal/streams/writable:337:10)
backend-server  |     at Socket.ondata (node:internal/streams/readable:809:22)
backend-server  |     at Socket.emit (node:events:517:28)
backend-server  |     at addChunk (node:internal/streams/readable:368:12)
backend-server  |     at readableAddChunk (node:internal/streams/readable:341:9)
backend-server  |     at Readable.push (node:internal/streams/readable:278:10)
backend-server  |     at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {        
backend-server  |   ok: 0,
backend-server  |   code: 13,
backend-server  |   codeName: 'Unauthorized',
backend-server  |   [Symbol(errorLabels)]: Set(0) {}
backend-server  | }

i connect to mongodb via

const connect=()=>{
mongoose.connect(`mongodb://athul:athul@mongodb-server:27017/test`).then(() => {
    console.log("connected to db");
})
.catch((err) => {
    console.log("not connected to db", err);
});

}

Please ignore the way i used the password and username directly in connection string .Any help will be appreciated thanks

0

1 Answer 1

0

By default MongoDB root user has an access to only admin database. So if you need an access to the test database you need create a user and grant permissions to the test database.

So:

  1. Login to the MongoDB

docker exec -it mongodb-server mongo -u athul -p athul --authenticationDatabase admin

  1. Create a user with an access to the test database:
db = db.getSiblingDB("test");
db.createUser({
    user: "athul",
    pwd: "athul",
    roles: [{ role: "readWrite", db: "test" }]
});
  1. Update your docker-compose file (change credentials and add init script)
version: '3.8' 

services:
  mongodb:
    image: mongo
    container_name: mongodb-server
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=<adminpassword>
    volumes:
      - ~/mongo/data:/data/db
      - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js
    networks:
      - backend-ecom
  
  backendapi:
    image: athulmekkoth/backend:v3
    container_name: backend-server 
    ports:
      - "5000:5000"  
    networks:
      - backend-ecom  
    env_file:
      - .env
    depends_on:
      - mongodb

networks:
  backend-ecom:
    driver: bridge
Sign up to request clarification or add additional context in comments.

2 Comments

In general a user has no permissions at all, it depends on the roles you granted. An user with root role has (almost) every permission, not only access to only admin database. Usually it does not make much sense to create users in any other database than admin
Got it, my mistake. Thanks for clarifying!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.