So I'm trying to setup a MongoDB using the official mongo Docker image, version 4.2
.
What I want to achieve is to use the server with authentication enabled, and I want to have a custom database with a custom user and password.
So, I'm setting the following environment variables:
MONGO_INITDB_DATABASE: mydb
MONGO_INITDB_ROOT_USERNAME: jane
MONGO_INITDB_ROOT_PASSWORD: secret
In the documentation it states that if you provide the latter two environment variables, authentication is enabled automatically.
However, when I then try to access the mydb
database using the credentials jane
and secret
, all I get is an error:
Supported SASL mechanisms requested for unknown user 'jane@mydb'
SASL SCRAM-SHA-1 authentication failed for jane on mydb from client 172.17.0.1:54702 ; UserNotFound: Could not find user "jane" for db "mydb"
Why is that? What am I missing?
My guess is that the user created only has access to the admin
database, and I need to grant access for the user jane
to the database mydb
. I tried to do that using the following command:
mongo admin -u jane -p secret --eval "db.grantRolesToUser('jane', [{role: 'dbOwner', db: 'mydb'}])"
But this didn't work either. What am I missing?