'MongoDb authentication failing on accessing database while using docker?

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



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

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

Solution 4:[4]

I ran into the same issue and deleting docker volumes solved it. They have been saved with different credentials and I changed it in the meantime, so it was conflicting, therefore it failed my authentication.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimish Agrawal
Solution 2 Ana Lava
Solution 3 Saurabh
Solution 4 gabifromtheblock