对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

createUser(数据库命令)

createUser

Creates a new user on the database where you run the command. The createUser command returns a duplicate user error if the user exists.

提示

In mongosh, this command can also be run through the db.createUser() helper method.

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

此命令可用于以下环境中托管的部署:

重要

MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

该命令具有以下语法:

提示

您可以将 passwordPrompt() 方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的 mongo shell 一样直接指定密码。

db.runCommand(
{
createUser: "<name>",
pwd: passwordPrompt(), // Or "<cleartext password>"
customData: { <any information> },
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
writeConcern: { <write concern> },
authenticationRestrictions: [
{ clientSource: [ "<IP|CIDR range>", ... ], serverAddress: [ "<IP|CIDR range>", ... ] },
...
],
mechanisms: [ "<scram-mechanism>", ... ],
digestPassword: <boolean>,
comment: <any>
}
)

createUser 有以下字段:

字段
类型
说明

createUser

字符串

新用户的名称。

pwd

字符串

The user's password. The pwd field is not required if you run createUser on the $external database to create users who have credentials stored externally to MongoDB.

该值可为以下任一项:

您可以将 passwordPrompt() 方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的 mongo shell 一样直接指定密码。

customData

文档

可选。任意信息。此字段可用于存储管理员希望与此特定用户关联的任何数据。例如,这可以是用户的全名或员工 ID。

roles

阵列

授予用户的角色。可以指定一个空数组 [],以创建无角色的用户。

digestPassword

布尔

可选。指示对密码进行摘要处理的是服务器还是客户端。

如果为 true,服务器会从客户端接收未加密的密码,然后对此密码进行加密。

如果为 false,客户端将对密码进行摘要处理并将处理后的密码传递到服务器。不兼容于 SCRAM-SHA-256

默认值为 true

writeConcern

文档

可选。操作的写关注级别。请参阅写关注规范

authenticationRestrictions

阵列

可选。服务器对创建的用户实施的身份验证限制。指定 IP 地址和 CIDR 范围列表,允许这些地址和范围的用户连接至服务器或者服务器接受这些用户。

mechanisms

阵列

可选。指定特定的 SCRAM 机制或用于创建 SCRAM 用户档案的机制。如果指定了authenticationMechanisms,则只能指定authenticationMechanisms的子集。

有效值为:

  • "SCRAM-SHA-1"

  • 使用 SHA-1 哈希函数。

  • "SCRAM-SHA-256"

  • 使用 SHA-256 哈希函数。

  • 需将 featureCompatibilityVersion 设为 4.0

  • 要求 DigestPassword 设置为 true

featureCompatibilityVersion 4.0 的默认值为 SCRAM-SHA-1SCRAM-SHA-256

featureCompatibilityVersion 3.6 的默认值为 SCRAM-SHA-1

digestPassword

布尔

可选。指示对密码进行摘要处理的是服务器还是客户端。

如果为 true,服务器会从客户端接收未加密的密码,然后对此密码进行加密。

如果为 false,客户端将对密码进行摘要处理并将处理后的密码传递到服务器。不兼容于 SCRAM-SHA-256

默认值为 true

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

roles 字段中,可以指定内置角色用户自定义角色

To specify a role that exists in the same database where createUser runs, you can either specify the role with the name of the role:

"readWrite"

或者,可以使用文档指定角色,如下所示:

{ role: "<role>", db: "<database>" }

要指定存在于其他数据库中的角色,请使用文档指定该角色。

authenticationRestrictions 文档只能包含以下字段。如果 authenticationRestrictions 文档包含无法识别的字段,服务器会引发错误:

字段名称
说明

clientSource

IP 地址和/或 CIDR 范围的数组

如果存在,则在对用户进行身份验证时,服务器会验证客户端的 IP 地址是否在给定列表中或属于列表中的 CIDR 范围。如果客户端的 IP 地址不存在,服务器不会对用户进行身份验证。

serverAddress

IP 地址和/或 CIDR 范围的数组

客户端可以连接的 IP 地址或 CIDR 范围列表。如果存在,服务器将验证客户端的连接是否已通过给定列表中的 IP 地址接受。如果通过无法识别的 IP 地址接受连接,服务器不会对用户进行身份验证。

重要

如果用户继承的多个角色具有不兼容的身份验证限制,则该用户将不可用。

例如,如果用户继承了一个角色(其中 clientSource 字段为 ["198.51.100.0"])和另一个角色(其中 clientSource 字段为 ["203.0.113.0"]),则服务器无法对该用户进行身份验证。

有关MongoDB中身份身份验证的更多信息,请参阅自托管部署上的身份验证。

MongoDB 会在创建时自动为用户分配唯一的 userId

警告

By default, createUser sends all specified data to the MongoDB instance in cleartext, even if using passwordPrompt(). Use TLS transport encryption to protect communications between clients and the server, including the password sent by createUser. For instructions on enabling TLS transport encryption, see Configure MongoDB Instances for TLS/SSL Encryption.

MongoDB 不以明文形式存储密码。仅当未启用 TLS 传输加密时,密码才会在客户端与服务器之间的传输过程中容易受到攻击。

$external数据库上创建的用户应将档案存储在 MongoDB 外部,例如使用 Kerberos 的 MongoDB Enterprise 安装。

要对 $external 身份验证用户(Kerberos、LDAP 或 X.509 用户)使用客户端会话和因果一致性保证,用户名不能大于 10k 字节。

不能在本地数据库中创建用户。

用户名必须至少包含一个字符,且不能大于 7MB。

userAdminuserAdminAnyDatabase 内置角色提供对资源createUsergrantRole 动作。

The following createUser command creates a user accountAdmin01 on the products database. The command gives accountAdmin01 the clusterAdmin and readAnyDatabase roles on the admin database and the readWrite role on the products database:

提示

您可以将 passwordPrompt() 方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的 mongo shell 一样直接指定密码。

db.getSiblingDB("products").runCommand( {
createUser: "accountAdmin01",
pwd: passwordPrompt(),
customData: { employeeId: 12345 },
roles: [
{ role: "clusterAdmin", db: "admin" },
{ role: "readAnyDatabase", db: "admin" },
"readWrite"
],
writeConcern: { w: "majority" , wtimeout: 5000 }
} )