...
- Establish a connection to MongoDB via mongo CLI (see Connect to MongoDB with mongo CLI)
Switch to admin db with
use admin
No Format > use admin switched to db admin
Use
db.getUsers()
to list existing users (see https://docs.mongodb.com/v3.4/reference/method/db.getUsers/)No Format > db.getUsers() [ { "_id" : "admin.bvq", "user" : "bvq", "db" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] } ]
See https://docs.mongodb.com/v3.4/core/security-built-in-roles/ for available roles in MongoDB
Create
...
a user
- Establish a connection to MongoDB via mongo CLI (see Connect to MongoDB with mongo CLI)
- Switch to admin db with
use admin
Use
db.createUser()
to create a new user (see https://docs.mongodb.com/v3.4/reference/method/db.createUser/)No Format > db.createUser({user: 'TEST_USER', pwd: 'TEST_PASSWORD', customData: { name: 'NAME OF TESTUSER' }, roles: ['readWriteAnyDatabase']}) Successfully added user: { "user" : "TEST_USER", "customData" : { "name" : "NAME OF TESTUSER" }, "roles" : [ "readWriteAnyDatabase" ] } >
List users with
db.getUsers()
(see List existing users)No Format > db.getUsers() [ { "_id" : "admin.TEST_USER", "user" : "TEST_USER", "db" : "admin", "customData" : { "name" : "NAME OF TESTUSER" }, "roles" : [ { "role" : "readWriteAnyDatabase", "db" : "admin" } ] }, { "_id" : "admin.bvq", "user" : "bvq", "db" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] } ] >
Remove
...
a user
- Establish a connection to MongoDB via mongo CLI (see Connect to MongoDB with mongo CLI)
Switch to admin db with
use admin
Use
db.dropUser()
to delete an existing user (see https://docs.mongodb.com/v3.4/reference/method/db.dropUser/)No Format > db.dropUser('TEST_USER') true >
List users with
db.getUsers()
(see List existing users)No Format > db.getUsers() [ { "_id" : "admin.bvq", "user" : "bvq", "db" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] } ] >
User
TEST_USER
was deleted successfully (user was created in Create an additional user)