Closed davStar closed 4 years ago
In your compose file shown the variables are typo'd with the -
connected to the variable names
environment:
-MONGO_INITDB_ROOT_USERNAME: root
-MONGO_INITDB_ROOT_PASSWORD: root
-MONGO_INITDB_DATABASE: admin
Looking at your init-mongo.js
if you change db.auth('root', 'root')
to be db.auth('david', 'root')
then you can connect using those user credentials just fine
@wglambert Thank you for your help!! For the typos on docker-compose I can understand. I ve removed the -
.
However for the db.auth() script if I can read the doc. , the method expect a db.auth (username , password). In my case David and root are two usernames?!?
Thank you for your clarification.
db = db.getSiblingDB('my_db')
db.createUser({
user: 'david',
pwd: 'IloveU',
roles: [
{
role: 'root',
db: 'admin',
},
],
});
This creates the user in the my_db
database and not the admin
database that the root
user is in. So to authenticate as david
you would need to use authentication_source='my_db'
(--authenticationDatabase
on the mongo cli).
Quick test on database without authentication:
$ docker run -d --name mongo mongo
$ docker exec -it mongo mongo
...
> db = db.getSiblingDB('my_db')
my_db
> db.createUser({
... user: 'david',
... pwd: 'IloveU',
... roles: [
... {
... role: 'root',
... db: 'admin',
... },
... ],
... });
Successfully added user: {
"user" : "david",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
> db.getUsers()
[
{
"_id" : "my_db.david",
"userId" : UUID("f3636d05-0f33-4d65-8376-69d329d4c025"),
"user" : "david",
"db" : "my_db",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
]
> db.getSiblingDB('admin').getUsers();
[ ]
I faced this same issue. After a lot of searching online and trying different ways, I was able to resolve it by appending the below to my mongo_uri:
authSource=admin
A complete URI would be something like this:
MONGO_URI= mongodb://username:userpass@localhost:27017/Test_DB?authSource=admin&retryWrites=true&w=majority
Dear Mongo community,
I encounter an issue where I work around since several days without finding a solution. I use MongoEngine module in python. I don't if the problem encountered is coming from my mongo docker config or my MongoEngine usage.
pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed'}
Code Description:
I try to connect via my Flask service:
me.connect('my_db', username='david', password='IloveU', host='mongo', authentication_source='admin', port=27017)
My mongo docker-compose:
My script init-mongo.js :
Output provided:
Return via PostMan when I request my API:
Output.pdf
My mongo Docker log output at the intitialization:
My mongo Docker log output at when I launch the request via Postman:
Thank you for help and support,