With this help, I created a super user in the mongo shell: Create Superuser in mongo
user: "try1"
passw: "hello"
In mongo cmd, I have 3 databases: 'admin', 'myDatabase' and 'local'.
Now I try to use this authorized connection to the database called 'myDatabase'.
mongoose.connect('mongodb://try1:hello@localhost:27017/myDatabase');
But this is the error I get:
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
errmsg: 'Authentication failed.',
code: 18,
codeName: 'AuthenticationFailed' }
Mongoose disconnected
Mongoose disconnected through ${msg}
Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.
If you receive this error message, that means that the username and/or password that you have entered is incorrect. The error message states “Authentication failed! Try again.” You may have locked your account after too many attempts and your account will need to be reset. Contact the Help Desk if this is the case.
I had the same problem many hours ago, and after all I solve it. My code is:
mongoose.createConnection(
"mongodb://localhost:27017/dbName",
{
"auth": {
"authSource": "admin"
},
"user": "admin",
"pass": "password"
}
);
Further to @kartGIS, I've added one more option to make the connection code perfect as possible.
mongoose.connect("mongodb://localhost:27017/databaseName", {
"auth": { "authSource": "admin" },
"user": "username",
"pass": "password",
"useMongoClient": true
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With