Reputation: 6449
I need some help with setting up my NodeJS application to connect with the MongoDB with credentials.
I'm confused with creating user on the admin
database, or on the database I will store my documents, and then connecting to it from my application.
So, let's say I have this MongoDB service running on localhost
port 27017
and my database is called myAmazingApp
. I want to create a user with Read/Write permissions on this now empty database and set user userApp
and password passwordApp
.
I'm using Mongoose
on NodeJS.
My question includes where and how I must create this User/Role/whatever on MongoDB.
By the way, right now this is my connection string
mongodb://userApp:passwordApp@localhost:27017/myAmazingApp
and I am connecting with mongoose.connect
.
It gives me Authentication failed
Upvotes: 0
Views: 3102
Reputation: 393
user@ubuntu:~$ mongo
and switch to admin DB
> use admin
> db.createUser(
{
user: "adminUser",
pwd: "adminPwd",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
security:
authorization: "disabled"
change it to
security:
authorization: "enabled"
user@ubuntu:~$ sudo service mongodb restart
or
user@ubuntu:~$ sudo service mongod restart
user@ubuntu:~$ mongo admin
> db.auth("adminUser", "adminPwd")
1
> use myAmazingApp
> db.createUser(
{
user: "userApp",
pwd: "passwordApp",
roles: [ { role: "readWrite", db: "myAmazingApp" } ]
}
)
mongoose.connect('mongodb://userApp:passwordApp@localhost:27017/myAmazingApp');
or
mongoose.connect('mongodb://localhost:27017/myAmazingApp', {useNewUrlParser: true, user: "userApp", pass: "user:passwordApp"});
Upvotes: 2