Do you want to request a feature or report a bug?
Bug ?
What is the current behavior?
My mongo docker is not connecting anymore, this is the issue :
mongodb | 2018-11-21T14:24:50.356+0000 I ACCESS [conn3] Supported SASL mechanisms requested for unknown user 'testroot@data'
mongodb | 2018-11-21T14:24:50.359+0000 I ACCESS [conn3] SASL SCRAM-SHA-1 authentication failed for testroot on data from client 172.28.0.3:55362 ; UserNotFound: Could not find user testroot@data
mongodb | 2018-11-21T14:24:50.361+0000 I NETWORK [conn3] end connection 172.28.0.3:55362 (0 connections now open)
If the current behavior is a bug, please provide the steps to reproduce.
It was working fine before, I don't understand why it's not finding the user anymore.
What is the expected behavior?
Find the user and create a connection to the DB
Please mention your node.js, mongoose and MongoDB version.
NodeJS : 10
Mongo: 4.0.3
Mongoose: 5.3.13
It was working fine so far with mongoose 5.3.12.... I didn't think that a version bump would break anything :/
Some code for the connect :
mongoose.connect(DB_HOST, //mongodb://mongodb:27017/mydb
{
user: DB_USER, //testroot
pass: DB_PASSWORD, //password
auth: {
authdb: DB_AUTHDB //admin
},
autoIndex: NODE_ENV === 'dev',
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10 // Maintain up to 10 socket connections
}
);
Maybe somehow related to #6512 ?
Please use some client to check if it's really mongoose that is failing here.
Try downloading "MongoDB Compass (community edition)" and try connection with the exact same login data and connection options.
Also rethink if you maybe changed something else and just have forgotten about it ;)
@mhombach I can access the DB via Robo3T, and there is no code change. I literally try with mongoose 5.3.12 and it works, when I 'upgrade' to 5.3.13, it doesn't work with this error.
I would suggest maybe the mongo driver version 'bump' is the culprit here?
fix: bump mongodb driver -> 3.1.10 #7266
I found a workaround, I removed the DB name from the connection string :
DB_HOST=mongodb://mongodb:27017/mydb
-> DB_HOST=mongodb://mongodb:27017
and I set up the optional dbName
attribute in the mongoose connection params to mydb
.
So it seems that the issue comes from the mongodb-core package, the new way they parse the connection string.
Edit: Actually the error comes from the node-mongodb-native package.
Exact same issue here. I was debugging an application for hours, since I had two projects with the exact same MongoDB init code where one was working and the other not. After updating the dependencies on the older project I noticed it suddenly did not work anymore.
TL:DR; With mongoose 5.3.12 I am able to connect to my MongoDB, with 5.3.13 however I am not able to connect to it anymore. This behaviour can be replicated using this docker-compose config:
version: "3.2"
networks:
default:
external:
name: anynetwork
services:
rabbitmq:
image: "bitnami/mongodb:4.1"
ports:
- "27017:27017"
environment:
- MONGODB_USERNAME=xxx
- MONGODB_PASSWORD=anypass
- MONGODB_DATABASE=xxx
- MONGODB_ROOT_PASSWORD=anypass2
restart: always
Ok so from https://jira.mongodb.org/browse/NODE-1772 it looks like they fixed the default auth DB, so now it is actually pulling whatever DB name is after the /
in the connection string.
Since I had /mydb
, it's trying to authenticate using mydb
, but according to the mongo docs, the db after the /
should be the auth DB and not the default selected DB (which I thought it was).
I switched my connection string to ``mongodb://mongodb:27017/admin' and this works again.
Just out of curiosity:
mongodb://mongodb:27017/mydb?authSource=admin
does it work?@NewEraCracker it works with /mydb?authSource=admin
but at this point I'm wondering what's the point of /mydb
.
Some testing (with mongoose 5.3.13) :
/db
and auth: { authdb: 'admin'}
doesn't work/admin
(auth db) works/mydb?authSource=admin
works (what's the point of mydb
in this case?)It doesn't seem that the auth
object is used anywhere, especially the auth: {authdb: 'admintest1'}
?
This is my final setup that works with 5.3.13 (without the auth object) :
mongoose.connect(DB_HOST, // 'mongodb://mongodb:27017/admin' (admin IS THE AUTH DB)
{
user: DB_USER, //testroot
pass: DB_PASSWORD, //password
dbName: DB_NAME, // 'mydb' which is the default selected DB
autoIndex: NODE_ENV === 'dev',
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10 // Maintain up to 10 socket connections
}
);
You don't need the /admin
but I prefer to put it explicitly rather than letting it handled implicitly by the drivers as the default auth db name.
@NewEraCracker it works with
/mydb?authSource=admin
but at this point I'm wondering what's the point of/mydb
.
Maybe selecting the mydb as the database to do read and write operations against...
@NewEraCracker That's what I thought too at the beginning, but here is the link from the mongo docs :
https://docs.mongodb.com/manual/reference/connection-string/
/database | Optional. The name of the database to authenticate if the connection string includes authentication credentials in the form of聽username:password@. If聽/database聽is not specified and the connection string includes credentials, the driver will authenticate to the聽admin聽database.
So it's about the auth db and not the default db to use for collections.
https://docs.mongodb.com/manual/reference/method/connect/
芦 The following example instantiates a new connection to the MongoDB instance running on the localhost interface and returns a reference to myDatabase:
db = connect("localhost:27017/myDatabase")
禄
Something is clearly wrong with the documentation about authentication...
@NewEraCracker It's actually confusing, but I think that even in this piece of doc, /myDatabase
refers to the auth DB.
@HRK44 /myDatabase
is the authentication database, and the database that Mongoose will use for storing documents. That's what the point of it is. Since you specify the dbName
option (https://github.com/Automattic/mongoose/issues/7270#issuecomment-441239121), you don't need to specify the db name in the connection string.
Most helpful comment
I found a workaround, I removed the DB name from the connection string :
DB_HOST=mongodb://mongodb:27017/mydb
->DB_HOST=mongodb://mongodb:27017
and I set up the optionaldbName
attribute in the mongoose connection params tomydb
.So it seems that the issue comes from the mongodb-core package, the new way they parse the connection string.
Edit: Actually the error comes from the node-mongodb-native package.