0

From the documentation:

The localhost exception allows you to enable access control and then create the first user in the system. With the localhost exception, after you enable access control, connect to the localhost interface and create the first user in the admin database. The first user must have privileges to create other users, such as a user with the userAdmin or userAdminAnyDatabase role.

So I run the latest mongo with enabled access control (--auth):

docker run -p 27017:27017 mongo --auth

connect with my shell and try to create the admin user:

mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
> use admin
switched to db admin
> db.createUser(
...   {
...     user: "admin",
...     pwd: "password",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
...   }
... )
2018-10-03T15:29:30.234+0200 E QUERY    [js] Error: couldn't add user: command createUser requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1491:15
@(shell):1:1

What am I doing wrong?

Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146

1 Answers1

1

You are not connecting to localhost but to the exported port. For the exception to work you need to connect to localhost from within the container. E.g.:

docker exec -it `docker ps --filter ancestor=mongo --format "{{.ID}}"` mongo
Alex Blex
  • 34,704
  • 7
  • 48
  • 75