By default, mongod also has no administrator account. So unless you add an administrator account to the admin database using the db.createuser () command and start mongod with the -auth parameter, anyone in the database can execute all commands without authentication.

CREATE DATABASE ADMINISTRATOR USER CREATE DATABASE ADMINISTRATOR USER

1. Run cmd.exe as administrator, CD to bin directory of Mongodb installation directory,

Enter the mongo.exe command to enter the MongoDB command interface:

Create database test1

Insert a piece of data, and then use the command: show

3. Enter the admin database:

Command:

4. Create a management account:

First look at the MongoDB built-in roles:

1. Database user roles: Read, ReadWrite; 2. The database management roles: DBADMIN, DBOWNER, USEeradmin; 3. Cluster management roles: Clustereradmin, ClusterManager, ClusterMonitor, HostManager; Backup: backup, restore; 5. All database roles: ReadAnyDatabase, ReadWriteAnyDatabase, USADMINANYDATABASE, EADMINANYDATABASE. 7. Internal role: __system

Use the command:

db.createUser({user:"testAdmin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})

5. Restart the MongoDB service.

Open CMD again, in the bin directory of the MongoDB path, execute mongod –dbpath d:\ mongoDB \data –auth (data is the database folder created by installing MongoDB)

Verify that the user added successfully:

The result is 1, which means success.

7. Use Robomongo tool to connect:

You can see that the administrator user you have just created, TestAdmin (role USEeradmin Database), has permission to access all databases.

Set user and password for a single database

Create database test2 in the same way as above

Db.createuser ({user:’test2admin’, PWD :’123456′,roles:[{role:’readWrite’,db:’test2′}]})

Use the Robomongo tool to connect:

User test2admin can only see database test2.