MongoDB教程
MongoDB运算符
MongoDB命令
MongoDB数据库
MongoDB Shell
MongoDB云和工具

MongoDB 用户管理命令

mongo DB用户管理命令包含与用户相关的命令。我们可以使用以下用户管理命令创建,删除和更新用户。

MongoDB createUser命令

MongoDB createUser命令为数据库创建一个新用户从我们运行命令的位置开始。如果用户已经存在,它将返回重复的用户错误。
语法:
 {
  createUser: "<user_name>",
  pwd: "<cleartext password>"
  customData: { <any info.> },
  roles: [
    { role: "<role>", db: "<database>" } | "<role>",
    ...
  ],
  writeConcern: { <write concern> },
  authenticationRestrictions: [
     { clientSource: [ "<IP|CIDR range>", ... ], serverAddress: [ "<IP|CIDR range>", ... ] },
     ...
  ],
  mechanisms: [ "<scram-mechanism>", ...], 
  digestPassword: <boolean>
}
createUser命令具有以下字段:
字段 类型 说明
createUser string 此字段包含新用户的名称。
pwd string 此字段包含用户密码。该值可以是明文字符串中的用户密码,也可以是passwordPrompt()以提示输入用户密码。
customData document 此字段包含管理员希望与特定用户关联的数据。
roles array 该字段将任何角色授予用户。
digestPassword boolean digestPassword表示摘要是密码的服务器还是客户端。
writeConcern document 该字段包含创建操作的写关注点。
authentication
Restrictions
array 它对创建的用户执行身份验证规则。它提供了允许用户连接的IP地址和CIDR范围的列表。
mechanism array 此字段指定SCRAM机制。有效的SCRAM值为SCRAM-SHA-1和SCRAM-SHA-256。
示例:
db.getSiblingDB("student").runCommand( {
       createUser: "admin@lidihuo",
       pwd: passwordPrompt(),
       customData: { empId: 101 },
       roles: [
                { role: "clusterAdmin", db: "admin" },
                { role: "readAnyDatabase", db: "admin" },
                "readWrite"
              ],
       writeConcern: { w: "majority" , wtimeout: 5000 }
} )
上面的示例在学生数据库上创建用户admin @ lidihuo。该命令为admin @ lidihuo提供admin数据库上的clusterAdmin和readAnyDatabase角色以及学生数据库上的readwrite角色。

MongoDB dropUser命令

MongoDB dropUser命令从运行命令的数据库中删除用户。
语法:
{
  dropUser: "<user>",
  writeConcern: { <write concern> }
}
dropUser命令字段:
字段 类型 说明
dropUser string dropUser字段包含要删除的用户的名称。
writeConcern document 此字段包含删除操作的写关注级别。
示例:
use products
db.runCommand( {
   dropUser: " admin@lidihuo ",
   writeConcern: { w: "majority", wtimeout: 5000 }
} )

MongoDB updateUser命令

MongoDB updateUser命令更新运行该命令的数据库中的用户详细信息。使用该命令时,它将完全替换先前字段的值,包括分配的角色和authenticationRestrictions数组。
语法:
{
  updateUser: "<user_name>",
  pwd: "<cleartext password>"
  customData: { <any information> },
  roles: [
    { role: "<role>", db: "<database>" } | "<role>",
    ...
  ],
  authenticationRestrictions: [
     {
       clientSource: ["<IP>" | "<CIDR range>", ...],
       serverAddress: ["<IP>", | "<CIDR range>", ...]
     },
     ...
  ],
  mechanisms: [ "<scram-mechanism>", ... ],
  digestPassword: <boolean>,
  writeConcern: { <write concern> }
}

字段 类型 说明
updateUser string 它包含我们需要更新的用户名。
pwd string 它包含用户的密码,或者您可以使用密码提示来提示输入密码。
customData document 此字段包含管理员希望在特定用户中更新的数据。
roles array 此字段将角色授予用户。
digestPassword boolean 它指示服务器或客户端是否将提取密码。
writeConcern document 该字段包含创建操作的写关注点。
authentication
Restrictions
array 它对创建的用户执行身份验证规则。它提供了允许用户连接的IP地址和CIDR范围的列表。
mechanism array 此字段指定SCRAM机制。有效的SCRAM值为SCRAM-SHA-1和SCRAM-SHA-256。
示例:
{
   "_id" : "products.appClient01",
   "userId" : UUID("c5d88855-3f1e-46cb-9c8b-269bef957986"), // Starting in MongoDB 4.0.9
   "user" : "appClient01",
   "db" : "products",
   "customData" : { "empID" : "12345", "badge" : "9156" },
   "roles" : [
       { "role" : "readWrite",
         "db" : "products"
       },
       { "role" : "read",
         "db" : "inventory"
       }
   ],
   "mechanisms" : [   
      "SCRAM-SHA-1",
      "SCRAM-SHA-256"
   ]
}
以下更新用户命令完全替换了用户的customData和角色数据:
use products
db.runCommand( {
   updateUser : "appClient01",
   customData : { employeeId : "0x3039" },
   roles : [ { role : "read", db : "assets" } ]
} )

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4