By default Mongo is accepting connections without authentication. If, you want to enable authentication, you can add the users to the admin database.

Example: Create a user named root with password test

  1. Connect to Mongo
  2. Execute command use admin
  3. Execute code
    	user: "root", 
    	pwd: "test", 
    	roles: ['clusterAdmin', 'userAdminAnyDatabase', 'readWriteAnyDatabase'] 
  4. Start the server with the command bin/mongod --auth
  5. Now you can connect with the command bin/mongo -username root -p
  6. You can check the users of current database with the command

    • show users

    Add user

    You can add users to current database with the command
    • db.createUser({user:"username",pwd:"password", roles["define the privileges"]})
    • remember that you should first select the database (use dbname)


    1. 	db.createUser( 
      	{ user: "testuser_1",
      	  pwd: "testpass",
      	  roles: [ "read" ] 
      	} )
    2. 	db.createUser( 
      	{ user: "testuser_2",
      	  pwd: "testpass",
      	  roles: [ "readWrite" ] 
      	} )
    3. 	db.createUser( 
      	{ user: "someAdmin",
          pwd: "verysecretpassword",
      	roles: [ "readWrite","dbAdmin" ] 
      	} )
      And if you want to create a admin user which has priveleges to all databases
    4. 	use admin	  
      	{ user: "theAdmin", 
      	  pwd: "verysecretpassword", 
      	  roles: ['clusterAdmin', 'userAdminAnyDatabase', 'readWriteAnyDatabase'] 
      	} )

    Delete user

    You can remove users from the current database with the command
    • db.dropUser("username")
    You can read more about user roles from

Toggle Menu