Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker MongoDB Cannot sign in as root

Tags:

docker

mongodb

I'm trying to set up mongo using docker but I don't know the correct way to log in to mongo. It doesn't seem to have created the default root user as I have specified:

version: '3'
services:
  location-mongo-db:
    container_name: location-mongo-db
    image: mongo
    ports:
      - "27017:27017"
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
networks:
  default:
    external:
      name: environment_location-mongo

I run

docker-compose -f docker-compose/db.yml -p location-mongo-db up -d --build 

I then do

docker exec -it location-mongo-db sh 

to get in to the shell and run # mongo from inside the container. I get the following:

# mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0
> show dbs
2018-06-28T15:45:36.337+0000 E QUERY    [js] Error: listDatabases failed:{
 "ok" : 0,
 "errmsg" : "command listDatabases requires authentication",
 "code" : 13,
 "codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:865:19
shellHelper@src/mongo/shell/utils.js:755:15
@(shellhelp2):1:1

I don't know what the issue is. Here is my output from checking for the list of users:

> db.runCommand({connectionStatus : 1})
{
 "authInfo" : {
 "authenticatedUsers" : [ ],
 "authenticatedUserRoles" : [ ]
 },
 "ok" : 1
}

What is missing? I've tried signing in a few different ways with -u root and -p etc but nothing so far works

like image 753
mikelovelyuk Avatar asked Dec 17 '22 22:12

mikelovelyuk


1 Answers

When you start mongo client without params on mongo command, you connect yourself as unauthenticated user, with very limited privileges. The right command to connect from your docker tty (and from your host terminal since you expose mongodb port) with your root user credentials is :

mongo -u root -p example --authenticationDatabase admin

To fully disable connection without authetication, you need to start mongod process with the --auth option.

EDIT : According to the doc, it seems that with the two env variables MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD set, mongod in docker container must start with --auth option automatically... but this behavior is not working for me.

like image 88
matthPen Avatar answered Jan 02 '23 10:01

matthPen