9

I have been trying to setup password to mongodb

My docker-compose file is

version: "3"
services:
    mongo:
        image: mongo
        ports:
            - "27017: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,

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

3
  • You seem to be running another mongod process on the host system, use port 27016 for the container one. Commented Jun 16, 2020 at 17:58
  • 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 Commented Jun 16, 2020 at 18:05
  • For anyone having problem, take a look at stackoverflow.com/a/67702253/5723524 Commented May 26, 2021 at 9:35

5 Answers 5

32

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:27017/example?authSource=admin

This worked and successfully added data to example db

Sign up to request clarification or add additional context in comments.

1 Comment

Holy WOW! I was trying everything and authSource=admin fixed it! Thank you!
2

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.

Comments

1

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.

7 Comments

Same it connects when mongo mongodb://root:password@localhost:27016 but fails when mongo mongodb://root:password@localhost:27016/example
Not sure but docs.mongodb.com/manual/reference/connection-string/#components states that /defaultdb is the database where auth details are checked, so when I tried appending /admin it worked but I want to specify new db
Yeah! if you want to connect with example, you should define MONGODB_DATABASE=example in your docker-compose environment variables
mongo mongodb://root:password@localhost:27016/example?authSource=admin This also works
The connection string would be this: mongodb://root:password@localhost:27016/example?authSource=example if you set that env variable
|
1

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

Comments

0

Just wanted to add (for anyone else facing this issue) that I took the following steps:

  1. Deleted the mongo image
  2. Deleted the associated container(s)
  3. Deleted the associated volumes.
  4. Pulled the mongo image again

This worked for me.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.