Do you want to request a feature or report a bug?
Bug
What is the current behavior?
We have set up a MongoDB docker container and want to connect to it with a custom mongo user. We have tried many connection attempts. Like: await Mongoose.connect('mongodb://db-user:4567@server-db:27017/admin?authSource=admin');
or: await Mongoose.connect('mongodb://admin:1234@server-db:27017/server-db?authMechanism=SCRAM-SHA-1');
or: await Mongoose.connect('mongodb://admin:1234@server-db:27017/server-db');
Then I see in the console:
[conn1] SCRAM-SHA-1 authentication failed for db-user on server-db from client 172.22.0.3:56428 ; UserNotFound: Could not find user db-user@server-db
2018-01-26T14:58:50.918+0000 I NETWORK [conn1] end connection 172.22.0.3:56428 (0 connections now open)
(node:45) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Authentication failed.
So according to the logs I'm 100% sure that my backend server and my mongoDB instance have connection to each other. The failure is really at authenticating.
Connecting to the mongodb without username + password like this actually works fine: await Mongoose.connect('mongodb://server-db:27017/server-db');
-> However of course an exception is thrown as soon as I try to create new collections, etc.
If the current behavior is a bug, please provide the steps to reproduce.
My MongoDB file looks like this:
FROM mongo:3.6.2
COPY ./mongo.config.js /docker-entrypoint-initdb.d/
EXPOSE 27017
CMD ["mongod", "--auth"]
Inside mongo.config.js
:
let res = [
"use admin",
db.createUser({
user: 'admin',
pwd: '1234',
roles: [{
role: "root", db: "admin"
}]
}),
"use server-db",
db.createUser({
user: 'db-user',
pwd: '4567',
roles: [{
role: "readWrite", db: "server-db"
}]
})
]
printjson(res)
And actually I see the successful creation of the user in the console:
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
Successfully added user: {
"user" : "db-user",
"roles" : [
{
"role" : "readWrite",
"db" : "server-db"
}
]
}
And logging on a terminal also works:
root@5a4292713f21:/# mongo -u "db-user" -p "4567"
MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.2
> use server-db
switched to db server-db
> db.createCollection("test")
{ "ok" : 1 }
> show collections
test
What is the expected behavior?
Mongoose should connect successfully with the existing username + password.
I have searched through all possible issues in this repository and also on stackoverflow. I have a feeling that this might be a bug when connecting to a docker MongoDB instance.
Please mention your node.js, mongoose and MongoDB version.
Node: v8.9.1
MongoDB: 3.6.2
Mongoose: 5.0.1
Try this:
await Mongoose.connect('mongodb://admin:1234@server-db:27017/server-db?authSource=admin')
If I'm reading this correctly, you're connecting to 'server-db' and trying to authenticate against the 'admin' user that's defined on the 'admin' db. You need to tell MongoDB what database your user is defined on regardless of whether you're using docker or not.
I've tried this but without any luck.. When i change the name of the database from server-db
to test
everything works without any issue, but i dont really know how to specify the name of the database. Any suggestions?
@vkarpov15 me and @dlipovac012 are working on the same project.
@dlipovac012 try await Mongoose.connect('mongodb://admin:1234@server-db:27017/server-db?authSource=test')
@vkarpov15 Nope.. Didn't work.. I got the same issue as before, saying: MongoError: there are no users authenticated
... When I try to login to DB through terminal, it shows me the users inside admin
collection, but when I try to connect to that database through Robo3T or through the app, it always says that there are no users inside admin
and that they are stored inside some test
db which I have no idea how it gets generated.
@vkarpov15 Do you have more suggestions or is this a bug?
@BorntraegerMarc @dlipovac012 nope I'm pretty baffled. You're certain that:
await Mongoose.connect('mongodb://db-user:4567@server-db:27017/server-db?authSource=test')
Does not work but the below does?
await Mongoose.connect('mongodb://db-user:4567@server-db:27017/test')
@vkarpov15 Actually, no. When we create admin user, it gets created in test
database, which we dont want to happen. So basically, both codes are working, but we would want to store that user not in this test
db, but rather in admin
, and we cant find the way for some reason.
Here's the link to this dummy application that mimics our real application: https://github.com/dlipovac012/nestmongoauth
If it helps somehow, here's the screenshot of the terminal with error:
Turns out there was a misconfiguration in the mongo docker container. We didn't use the env variables specified here: https://github.com/docker-library/mongo/issues/189
Now it works! Thanks for your support!
@vkarpov15 Brother, this is awesome! I have wasted 4 hours on this issue. Thank you very much!
Most helpful comment
Try this:
If I'm reading this correctly, you're connecting to 'server-db' and trying to authenticate against the 'admin' user that's defined on the 'admin' db. You need to tell MongoDB what database your user is defined on regardless of whether you're using docker or not.