Definition:
Create a database new user with the Db.createuser () method and return a user repeat error if the user exists.
Syntax:
db.createuser (user, Writeconcern)
User This document creates identity authentication and access information about users;
Writeconcern This document describes a successful report that ensures that MongoDB provides write operations.
The User document defines the following form of the user:
{User: "<name>",
pwd: "<cleartext password>",
customData: {<any information>},
roles: [
{role: ' <role> ', db: ' <database> '} | "<role>",
...
]
}
User document Field Description:
User field, the name of the new user;
PWD field, user's password;
Cusomdata field, for any content, for example, can be introduced to the user's full name;
Roles field, specify the user's role, you can use an empty array to set a null role for the new user;
In the Roles field, you can specify built-in roles and user-defined roles.
built-in Roles ( built-in role ):
1. Database user role: Read, readWrite;
2. Database management roles: DbAdmin, Dbowner, useradmin;
3. Cluster Management roles: Clusteradmin, Clustermanager, Clustermonitor, Hostmanager;
4. Backup recovery role: backups, restore;
5. All database roles: Readanydatabase, Readwriteanydatabase, Useradminanydatabase, Dbadminanydatabase
6. Super User role: ROOT&NBSP;&NBSP;
// There are also several roles that indirectly or directly provide access to system super users (Dbowner, Useradmin, useradminanydatabase)
7. Internal role: __system
PS: You can click on the built-in role link above to view details about the permissions that each role has.
writeconcern documentation ( official note )
w Options: Allowed values are 1, 0, values greater than 1, "majority", <tag set>;
J option: Ensure that the Mongod instance writes data to the journal (log) on disk, which ensures that no data is lost outside of the mongd. Set true to enable.
Wtimeout: Specifies a time limit, in milliseconds. The Wtimeout only applies to W values greater than 1.
For example: Create a user AccountAdmin01 in the products database and give the user the Admin database on the clusteradmin and readanydatabase roles, the products database on the ReadWrite role.
Use Products
Db.createuser ({"User": "AccountAdmin01",
"pwd": "Cleartext password",
"CustomData": {employeeid:12345},
"Roles": [{role: ' Clusteradmin ', db: ' admin '},
{role: "Readanydatabase", DB: "Admin"},
"ReadWrite"
] },
{w: "Majority", wtimeout:5000})
Verification:
mongo-u accountadmin01-p yourpassward--authenticationdatabase Products
MongoDB 3.0 Add Users