mongo
use admin
db.createUser(
{
user: "AdminSammy",
pwd: "AdminSammy'sSecurePassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
sudo nano /etc/mongod.conf
. . .
security:
authorization: "enabled"
. . .
and then restart by following command
sudo systemctl restart mongod
mongo -u AdminSammy -p --authenticationDatabase admin
use products
db.createUser( { user: "accountAdmin01",
pwd: "changeMe",
customData: { employeeId: 12345 },
roles: [ { role: "clusterAdmin", db: "admin" },
{ role: "readAnyDatabase", db: "admin" },
"readWrite"] },
{ w: "majority" , wtimeout: 5000 } )
use products
db.createUser(
{
user: "accountUser",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
)
Finally You can connect to your db like this if you are using mongoose for node.
mongoose.connect('mongodb://username:password@host:port/database?options...');