DanWebb
1/10/2017 - 6:59 AM

Add auth to mongo installation

Add auth to mongo installation

// Go to the mongo shell and type `use admin` then create your new root user...
db.createUser({ user: "root", pwd: "PASSWORD", roles: [{ role: "root", db: "admin" }] })

// stop the mongod process using
db.shutdownServer()

// exit mongo shell using ctrl+c

// start the mongod process again using the auth flag
// mongod --auth --port 27017
// or for production
// mongod --auth --port 27017 --dbpath=/var/lib/mongodb --fork --logpath /var/log/mongod.log
// you will need to specify a --dbpath here if you're not using the default /data/db 
// directory. After doing this any databases you connect to will need a 
// username/password combo.

// reconect to mongo shell using
// mongo localhost:27017 -u "root" -p "PASSWORD" --authenticationDatabase "admin"

// Go to the database you wish to make a user for using `use YOURDB`

// create your user with the privelages you wish them to use
db.createUser({user: "DBUSER", pwd: "DBPASSWORD", roles:["dbOwner"]})

// you may then connect to the database with your user, for example using
// mongoose this would look like...
require('mongoose').connect('mongodb://DBUSER:DBPASSWORD@localhost/YOURDB');

// Users can be edited using the updateUser command 
// https://docs.mongodb.com/manual/reference/method/db.updateUser/
// or if you just wish to grant user roles us db.grantRolesToUser
// https://docs.mongodb.com/manual/reference/method/db.grantRolesToUser/#db.grantRolesToUser