Docs 菜单

db.createUser()

db.createUser(user, writeConcern)

Creates a new user for the database on which the method is run. db.createUser() returns a duplicate user error if the user already exists on the database.

重要

mongosh 方法

本页面提供 mongosh 方法的相关信息。这不是数据库命令或特定语言驱动程序(例如 Node.js)的相关文档。

有关数据库命令,请参阅 createUser 命令。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

db.createUser() 方法使用的语法如下:

字段
类型
说明

user

文档

The document with authentication and access information about the user to create.

writeConcern

文档

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

The user document defines the user and has the following form:

提示

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

{
user: "<name>",
pwd: passwordPrompt(), // Or "<cleartext password>"
customData: { <any information> },
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
authenticationRestrictions: [
{
clientSource: ["<IP>" | "<CIDR range>", ...],
serverAddress: ["<IP>" | "<CIDR range>", ...]
},
...
],
mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ],
passwordDigestor: "<server|client>"
}

user 文档包含以下字段:

字段
类型
说明

user

字符串

The name of the new user.

pwd

字符串

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

The value can be either:

  • 以明文字符串表示的用户密码,或

  • passwordPrompt()提示输入用户密码。

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

customData

文档

Optional. Any arbitrary information. This field can be used to store any data an admin wishes to associate with this particular user. For example, this could be the user's full name or employee id.

roles

阵列

The roles granted to the user. Can specify an empty array [] to create users without roles.

阵列

Optional. The authentication restrictions the server enforces on the created user. Specifies a list of IP addresses and CIDR ranges from which the user is allowed to connect to the server or from which the server can accept users.

mechanisms

阵列

Optional. Specify the specific SCRAM mechanism or mechanisms for creating SCRAM user credentials. If authenticationMechanisms is specified, you can only specify a subset of the authenticationMechanisms.

有效值为:

  • "SCRAM-SHA-1"

    • 使用 SHA-1 哈希函数。

  • "SCRAM-SHA-256"

    • 使用 SHA-256 哈希函数。

    • 要求 passwordDigestor 为server

The default is both SCRAM-SHA-1 and SCRAM-SHA-256.

passwordDigestor

字符串

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

可用值为:

  • "server" (默认)
    服务器会从客户端接收未加密的密码,然后对此密码进行加密。
  • "client"(与 SCRAM-SHA-256 不兼容)
    客户端会对该密码进行哈希,并将哈希后的密码传递给服务器。

此方法可用于以下环境中托管的部署:

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

要指定运行 db.createUser() 的同一数据库中存在的角色,可以使用角色名称指定角色:

"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中身份身份验证的更多信息,请参阅自托管部署上的身份验证。

db.createUser() 方法封装了 createUser 命令。

MongoDB automatically assigns a unique userId to the user upon creation.

如果在副本集上运行,则默认情况下使用 "majority" 写关注执行 db.createUser()

警告

默认情况下,db.createUser() 以明文形式将所有指定数据发送到 MongoDB 实例,即使使用 passwordPrompt() 也是如此。使用 TLS 传输加密来保护客户端和服务器之间的通信,包括 db.createUser() 发送的密码。有关启用 TLS 传输加密的操作说明,请参阅为 TLS/SSL 配置 mongodmongos

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

Users created on the $external database should have credentials stored externally to MongoDB, as, for example, with MongoDB Enterprise installations that use Kerberos.

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

You cannot create users on the local database.

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

The following db.createUser() operation creates the accountAdmin01 user on the products database.

提示

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

use products
db.createUser( { user: "accountAdmin01",
pwd: passwordPrompt(), // Or "<cleartext password>"
customData: { employeeId: 12345 },
roles: [ { role: "clusterAdmin", db: "admin" },
{ role: "readAnyDatabase", db: "admin" },
"readWrite"] },
{ w: "majority" , wtimeout: 5000 } )

The operation gives accountAdmin01 the following roles:

  • the clusterAdmin and readAnyDatabase roles on the admin database

  • the readWrite role on the products database

The following operation creates accountUser in the products database and gives the user the readWrite and dbAdmin roles.

提示

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

use products
db.createUser(
{
user: "accountUser",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ "readWrite", "dbAdmin" ]
}
)

The following operation creates a user named reportsUser in the admin database but does not yet assign roles:

提示

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

use admin
db.createUser(
{
user: "reportsUser",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ ]
}
)

The following operation creates a user named appAdmin in the admin database and gives the user readWrite access to the config database, which lets the user change certain settings for sharded clusters, such as to the balancer setting.

提示

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

use admin
db.createUser(
{
user: "appAdmin",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles:
[
{ role: "readWrite", db: "config" },
"clusterAdmin"
]
}
)

The following operation creates a user named restricted in the admin database. This user may only authenticate if connecting from IP address 192.0.2.0 to IP address 198.51.100.0.

提示

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

use admin
db.createUser(
{
user: "restricted",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ { role: "readWrite", db: "reporting" } ],
authenticationRestrictions: [ {
clientSource: ["192.0.2.0"],
serverAddress: ["198.51.100.0"]
} ]
}
)

注意

要使用 SCRAM-SHA-256,必须将 featureCompatibilityVersion 设置为 4.0。有关 featureCompatibilityVersion 的更多信息,请参阅获取 FeatureCompatibilityVersionsetFeatureCompatibilityVersion

The following operation creates a user with only SCRAM-SHA-256 credentials.

提示

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

use reporting
db.createUser(
{
user: "reportUser256",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ { role: "readWrite", db: "reporting" } ],
mechanisms: [ "SCRAM-SHA-256" ]
}
)

If the authenticationMechanisms parameter is set, the mechanisms field can only include values specified in the authenticationMechanisms parameter.