1. Create a Super User
?
1234567891011121314 |
use admin
db.createUser(
{
user
:
"adminUserName"
,
pwd:
"userPassword"
,
roles:
[
{
role:
"userAdminAnyDatabase"
,
db:
"admin"
}
]
}
)
|
There are two types of superuser role, useradmin or useradminanydatabase (more access to all databases than the previous one).
DB is the name of the specified database, and admin is the management database.
2. Log in with the newly created user
?
1 |
mongo --host xxx -u adminUserName -p userPassword --authenticationDatabase admin |
3. View the permissions of the current user
?
123456 |
db.runCommand( { usersInfo:"userName", showPrivileges:true } ) |
4. Create a general user, also use CreateUser
?
123456789101112 |
use db01
db.createUser(
{
user:"oneUser",
pwd:"12345",
roles:[
{role:"read",db:"db01"},
{role:"read",db:"db02"},
{role:"read",db:"db03"}
]
}
)
|
5. Create a superuser who is not restricted by access
?
12345678 |
use admin db.createUser( { user:"superuser", pwd:"pwd", roles:["root"] } ) |
6. Change the password
?
12 |
use admin db.changeUserPassword( "username" , "xxx" ) |
7. View user Information
?
1 |
db.runCommand({usersInfo: "userName" }) |
8. Change Password and user information
?
1234567 |
db.runCommand( { updateUser:"username", pwd:"xxx", customData:{title:"xxx"} } ) |
Note:
1. and user management related operations are basically to run under the Admin database, to use the admin first;
2. If under a single database, it can only operate on the permissions of the current database;
3. Db.adduser is an older version of the operation, now the version can also continue to use, created by the user is a root role of the Super administrator.
MongoDB User Management