1. Create a Super User
Use admin
db.createuser (
{
User: "Adminusername",
pwd: "UserPassword",
roles:
[
{
roles: "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 admin database.
2. Log in with the newly created user
MONGO--host xxx-u adminusername-p userpassword--authenticationdatabase Admin
3. View current user's permissions
Db.runcommand (
{
usersinfo: "UserName",
showprivileges:true
}
)
4. Create a general user, but also with CreateUser
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 subject to access restrictions
Use admin
db.createuser (
{
User: "Superuser",
pwd: "pwd",
roles:["root"
)}
)
6. Change Password
Use admin
db.changeuserpassword ("username", "xxx")
7. View user Information
Db.runcommand ({usersinfo: "UserName"})
8. Modify password and user information
Db.runcommand (
{
updateuser: "username",
pwd: "xxx",
customdata:{title: "xxx"}
)
Note:
1. and user management related operations are basically under the admin database to run, first use admin;
2. If in a single database, it can only operate on the permissions of the current database;
3. The Db.adduser is an older version of the operation and is now still available, and the user created is a super administrator with root role.