0
votes

I have been trying to setup password to mongodb

My docker-compose file is

version: "3"
services:
    mongo:
        image: mongo
        ports:
            - "27016:27017"
        volumes:
            - ./data:/data/db
        environment:
            - MONGO_INITDB_ROOT_USERNAME=root
            - MONGO_INITDB_ROOT_PASSWORD=password
            - MONG_INITDB_DATABASE=example

Now after running the docker container, when I try after going inside the container

mongo mongodb://root:password@localhost:27017

It connects successfully,

MongoDB shell version v4.2.7 connecting to: mongodb://localhost:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("71ec853d-3a05-4367-aa9f-d0a69217fb7d") } MongoDB server version: 4.2.7 Server has startup warnings: 2020-06-16T17:43:51.837+0000 I STORAGE [initandlisten] 2020-06-16T17:43:51.837+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2020-06-16T17:43:51.837+0000 I STORAGE [initandlisten] ** See

but when I try

mongo mongodb://root:password@localhost:27017/example

authentication fails. MongoDB shell version v4.2.7 connecting to: mongodb://localhost:27017/example?compressors=disabled&gssapiServiceName=mongodb 2020-06-16T17:51:08.226+0000 E QUERY [js] Error: Authentication failed. : connect@src/mongo/shell/mongo.js:341:17

Its the same when I try to access from outside using a node app. (Running node app too using docker-compose)

mongo mongodb://root:password@mongo:27017/example

3
You seem to be running another mongod process on the host system, use port 27016 for the container one. - D. SM
When I am accessing inside container its still running on default 27017, outside when I am accessing in node app, I am using docker-compose, so DNS map mongo to containers IP, hence still 27017. I mapped it outside to 27016 as I had a local instance running on 27017 - Nimish Agrawal
For anyone having problem, take a look at stackoverflow.com/a/67702253/5723524 - Francesco Taioli

3 Answers

9
votes

Not sure why it was not mentioned in any blog or tutorial but according to official docs, https://docs.mongodb.com/manual/reference/connection-string/#components

/defaultauthdb  
Optional. The authentication database to use if the connection string includes username:password@ authentication credentials but the authSource option is unspecified.

If both authSource and defaultauthdb are unspecified, the client will attempt to authenticate the specified user to the admin database.

So what finally worked was adding authSource, without authSource mongod tries to find the creds in /example db thus giving error, after adding authSource

mongodb://root:password@localhost:27016/example?authSource=admin

This worked and successfully added data to example db

1
votes

You should try connecting to mongodb://root:password@localhost:27016/example from outside the container as you mapped the 27017 port of your docker container to your host's 27016 port.

0
votes

Two things to note:

  1. "MONG_INITDB_DATABASE" will not create DB unless it's there in your data volume binding i.e "./data:/data/db". If it's there then you need to use the specific roles which were used to create under "data"
  2. If it's not #1 then you need to provide a file which will create the database under /docker-entrypoint-initdb.d/*.js. Below is an template:

    db.createUser(
        {
            user: "<user for database which shall be created>",
            pwd: "<password of user>",
            roles: [
                {
                    role: "readWrite",
                    db: "<database to create>"
                }
            ]
        }
    

    );

You can refer more at their official docker hub image. Section: MONGO_INITDB_DATABASE and Initializing a fresh instance