-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Document authSource option #8517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The following didn't work either, which I believe is basically the sample given in the document. function connectDB() {
const databaseOption = {
useNewUrlParser: true,
};
return new Promise((resolve, reject) => {
mongoose.connect(
'mongodb://admin:mypassword@ip:27017/db',
databaseOption,
async (err) => {
if (err) {
reject(err);
} else {
resolve();
}
},
);
});
} |
Be sure not to have any special characters in the password that may need escaping. In that case it would be better to provide the password as a parameter inside the above |
Now it works. function connectDB() {
const databaseOption = {
useNewUrlParser: true,
useUnifiedTopology: true
};
return new Promise((resolve, reject) => {
mongoose.connect(
'mongodb://admin:mypassword@ip:27017/db?authSource=admin',
databaseOption,
async (err) => {
if (err) {
reject(err);
} else {
resolve();
}
},
);
});
} It seems that the key point is |
That depends though on the type of user you are using, and where it was created. I think that by default it searches for users on the same database you have specified, sort of a database level user scope. |
We'll add Also, I hate to be pedantic, but your return new Promise((resolve, reject) => {
mongoose.connect(
'mongodb://admin:mypassword@ip:27017/db?authSource=admin',
databaseOption,
async (err) => {
if (err) {
reject(err);
} else {
resolve();
}
},
);
}); Please never make a callback return mongoose.connect(
'mongodb://admin:mypassword@ip:27017/db?authSource=admin',
databaseOption); |
@vkarpov15 |
@vkarpov15 mongoose.connect(
'mongodb://ip:27017/db',
{
useNewUrlParser: true,
useUnifiedTopology: true,
user: 'admin',
password: 'mypassword'
}
); |
@harrisoff try I would usually recommend putting username and password in the connection string, that makes it easier to connect to different mongodb instances for dev/prod. |
Do you want to request a feature or report a bug?
Report a bug.
What is the current behavior?
MongoError: Authentication failed. But I could connect to the server with MongoDB Compass using the same username and password.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
Connected to mongodb server.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node v12.13.1
mongoose v5.8.7
mongodb v4.2.2
The text was updated successfully, but these errors were encountered: