## Prerequisites 

Required Access You must have the createUser (page 420) action (page 419) on a database to create a new user on that database. 

You must have the grantRole (page 420) action (page 419) on a role’s database to grant the role to another user. 

If you have the userAdmin (page 407) or userAdminAnyDatabase (page 412) role, you have those actions. 

First User Restrictions If your MongoDB deployment has no users, you must connect to mongod using the local- host exception (page 324) or use the --noauth option when starting mongod to gain full access the system. Once you have access, you can skip to Creating the system user administrator in this procedure. 

If users exist in the MongoDB database, but none of them has the appropriate prerequisites to create a new user or you do not have access to them, you must restart mongod with the --noauth option. 

## Procedure 

Step 1: Connect to MongoDB with the appropriate privileges. Connect to mongod or mongos either through the localhost exception (page 324) or as a user with the privileges indicated in the prerequisites section. 

In the following example, manager has the required privileges specified in Prerequisites (page 385). 

mongo --port 27017 -u manager -p 123456 --authenticationDatabase admin 

Step 2: Create the system user administrator. Add the user with the userAdminAnyDatabase (page 412) role, and only that role. 

The following example creates the user siteUserAdmin user on the admin database: 

use admin db.createUser( { user: "siteUserAdmin", pwd: "password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) 

Step 3: Create a user administrator for a single database. Optionally, you may want to create user administrators that only have access to administer users in a specific database by way of the userAdmin (page 407) role. 

The following example creates the user recordsUserAdmin on the records database: 

use records db.createUser( { user: "recordsUserAdmin", pwd: "password", roles: [ { role: "userAdmin", db: "records" } ] } ) 

