Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo db docker image authentication failed

Tags:

docker

mongodb

I'm using https://hub.docker.com/_/mongo mongo image in my local docker environment, but I'm getting Authentication failed error. In docker-compose I add it like:

 my-mongo:
    image: mongo
    restart: always
    container_name:  my-mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: mongo
      MONGO_INITDB_ROOT_PASSWORD: asdfasdf
    networks:
      - mynet

I also tried to run mongo CLI from inside the container but still getting the same error:

root@76e6db78228b:/# mongo
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c87c0f0e-fe83-41a6-96e9-4aa4ede8fa25") }
MongoDB server version: 4.2.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> use translations
switched to db translations  
> db.auth("mongo", "asdfasdf")
Error: Authentication failed.
0

Also, I'm trying to create a separate user:

> use admin
switched to db admin
db.auth("mongo", "asdfasdf")
1
> db.createUser({
    user: "user",
    pwd: "asdfasdf",
    roles: [  {role: "readWrite", db: "translations" }  ]
    })
Successfully added user: {
        "user" : "user",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "translations"
                }
        ]
}
> use translations
switched to db translations  
> db.auth("user", "asdfasdf")
Error: Authentication failed.
0

and the same, what I'm doing wrong???

Updated:

root@8bf81ef1fc4f:/# mongo -u mongo -p asdfasdf --authenticationDatabase admin
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("02231489-eaf4-40be-a108-248cec88257e") }
MongoDB server version: 4.2.3
Server has startup warnings: 
2020-02-26T16:24:12.942+0000 I  STORAGE  [initandlisten] 
2020-02-26T16:24:12.943+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-02-26T16:24:12.943+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> db.createUser({user: "someuser", pwd: "asdfasdf", roles: [{role: "readWrite", db: "translations"}]})
Successfully added user: {
        "user" : "someuser",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "translations"
                }
        ]
}
> use translations
switched to db translations
> db.auth("someuser", "asdfasdf")
Error: Authentication failed.
0
> 
like image 479
Bogdan Dubyk Avatar asked Dec 22 '22 19:12

Bogdan Dubyk


1 Answers

After some time, I figured out.

  • On the same folder, create docker-compose.yml and init-mongo.js

docker-compose.yml

version: '3.7'
services:
  database:
    image: mongo
    container_name : your-cont-name
    command: mongod --auth
    environment:
      - MONGO_INITDB_DATABASE=my_db
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=root
    ports:
      - '27017-27019:27017-27019'
    volumes: 
      - mongodbdata:/data/db
      - ./init-mongo.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      
volumes:
  mongodbdata:
    driver: local

init-mongo.js

db.createUser(
    {
        user: "your_user",
        pwd: "your_password",
        roles: [
            {
                role: "readWrite",
                db: "my_db"
            }
        ]
    }
);
db.createCollection("test"); //MongoDB creates the database when you first store data in that database

Auth

First, execute the bash inside the container

docker exec -it your-cont-name bash

Now we can login. For the admin

mongo -u admin -p root

For the your_user you have to specify the db (with the --authenticationDatabase) otherwise you'll have an auth error

mongo -u your_user -p your_password --authenticationDatabase my_db

After that, you should switch to the right db with

use my_db

If you don't execute this command, you'll be on test db

Note

For being sure of having the right config, i prefer to

docker-compose stop
docker-compose rm
docker volume rm <your-volume>
docker-compose up --build -d
like image 93
Francesco Taioli Avatar answered Jan 03 '23 01:01

Francesco Taioli