MongoDB-Role-Management.md

Overview

MongoDB

MongoDB is a NoSql database which is very easy to install and use.

In fact, mongod has no administrator account by default. So if you build your database in a open public net environment that will be very vulnerable. Others can operation your database data at will.


Role Kinds

The users of MongoDB are divided into two types, one is admin, the other is a specific database user.

Admin users have the highest permissions, while specific database users can only access specific databases


Role Management

Before you create your database user. We need to noted that MondoDB provide a database names admin. If your want to control the access user, firstly have to create a admin user into admin database. Then you can create the user for the database.

1
2
3
4
5
6
7
8
9
10
11
12
13
> use admin
> db.createUser({user: "admin", pwd: "adminPassword", roles: [{role: "root", db: "admin"}]})
> exit
// restart mongo
mongo --port 27017 -u admin -p adminPassword --authenticationDatabase admin
> use testDB
> db.createUser({user: "testUser", pwd: "dbPassword", roles: [{role: "readWrite", db: "testDB"}]})
> db.auth("testUser", "dbPassword")

Start DB Auth

1
> mongod --auth