Root user can't connect to any database with MongoDB
I’m using MongoDB with Docker and want to automate the database creation.
I found that when passing
MONGO_INITDB_ROOT_PASSWORD environment variables to the docker run command I can create a new root user on the admin database.
My issue starts when I’m trying to create a new database and create collections in it.
I wrote a script that selects the admin database, authenticate into the mongo service and creates new db, but for some reasons I can not access the database with my credentials..
use admin; db.auth('myuser', 'mypassword'); use newDatabase; db.createCollection('newCol');
when trying to authenticate to
newDatabase with the given credentials (myuser and mypassword) I found that I do not have the needed permissions to the db.
What did I do wrong? How can I do it automatically anyway?
One Solution collect form web for “Root user can't connect to any database with MongoDB”
I just understood that the root cannot access the other databases for some reasons, so I used
db.createUser and created a new user just for the new database.