Definition定义
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.db.createUser()将返回重复用户错误。Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.mongosh方法。这不是数据库命令或特定语言驱动程序(如Node.js)的文档。For the database command, see the关于数据库命令,请参阅createUsercommand.createUser命令。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。Thedb.createUser()method has the following syntax:db.createUser()方法具有以下语法:Field字段Type类型Description描述userdocument文档The document with authentication and access information about the user to create.要创建的包含用户身份验证和访问信息的文档。writeConcerndocument文档Optional.可选。The level of write concern for the operation. See Write Concern Specification.操作的写入关注级别。请参阅写入关注规范。Theuserdocument defines the user and has the following form:user文档定义了用户,并具有以下形式:Tip
You can use the您可以将passwordPrompt()method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call.passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the但是,您仍然可以像使用早期版本的mongoshell.mongoshell一样直接指定密码。{
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>"
}Theuserdocument has the following fields:user文档包含以下字段:Field字段Type类型Description描述userstring字符串The name of the new user.新用户的名称。pwdstring字符串The user's password. The用户的密码。如果在pwdfield is not required if you rundb.createUser()on the$externaldatabase to create users who have credentials stored externally to MongoDB.$external数据库上运行db.createUser()以创建具有存储在MongoDB外部的凭据的用户,则不需要pwd字段。The value can be either:该值可以是:the user's password in cleartext string, or明文字符串形式的用户密码,或passwordPrompt()to prompt for the user's password.提示输入用户密码。
You can use the您可以将passwordPrompt()method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. However, you can still specify the password directly as you would with earlier versions of themongoshell.passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。但是,您仍然可以像使用早期版本的mongoshell一样直接指定密码。customDatadocument文档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.任何任意信息。此字段可用于存储管理员希望与此特定用户关联的任何数据。例如,这可能是用户的全名或员工id。rolesarray数组The roles granted to the user. Can specify an empty array授予用户的角色。可以指定一个空数组[]to create users without roles.[]来创建没有角色的用户。authenticationRestrictionsarray数组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.服务器对创建的用户实施的身份验证限制。指定允许用户连接到服务器或服务器可以接受用户的IP地址和CIDR范围的列表。mechanismsarray数组Optional.可选。Specify the specific SCRAM mechanism or mechanisms for creating SCRAM user credentials.指定创建SCRAM用户凭据的特定SCRAM机制。If如果指定了authenticationMechanismsis specified, you can only specify a subset of theauthenticationMechanisms.authenticationMechanisms,则只能指定authenticationMechanisms的一个子集。Valid values are:有效值为:"SCRAM-SHA-1"Uses the使用SHA-1hashing function.SHA-1哈希函数。
"SCRAM-SHA-256"Uses the使用SHA-256hashing function.SHA-256哈希函数。Requires passwordDigestor to be需要server.passwordDigestor作为server。
The default is both默认值为SCRAM-SHA-1andSCRAM-SHA-256.SCRAM-SHA-1和SCRAM-SHA-256。passwordDigestorstring字符串Optional.可选。Indicates whether the server or the client digests the password.指示是服务器还是客户端对密码进行摘要。Available values are:可用值包括:"server"(Default)(默认)The server receives undigested password from the client and digests the password.服务器从客户端接收未消化的密码,并对密码进行摘要。
"client"(Not compatible with(与SCRAM-SHA-256)SCRAM-SHA-256不兼容)The client digests the password and passes the digested password to the server.客户端对密码进行摘要,并将摘要后的密码传递给服务器。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
Important
This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Roles角色
In the 在roles field, you can specify both built-in roles and user-defined roles.roles字段中,您可以指定内置角色和用户定义的角色。
To specify a role that exists in the same database where 要指定存在于运行db.createUser() runs, you can either specify the role with the name of the role:db.createUser()的同一数据库中的角色,您可以使用角色的名称指定角色:
"readWrite"
Or you can specify the role with a document, as in:或者,您可以在文档中指定角色,如:
{ role: "<role>", db: "<database>" }
To specify a role that exists in a different database, specify the role with a document.若要指定存在于其他数据库中的角色,请使用文档指定该角色。
Authentication Restrictions身份验证限制
The authenticationRestrictions document can contain only the following fields. The server throws an error if the authenticationRestrictions document contains an unrecognized field:authenticationRestrictions文档只能包含以下字段。如果authenticationRestrictions文档包含无法识别的字段,服务器将抛出错误:
clientSource | ||
serverAddress |
Important
If a user inherits multiple roles with incompatible authentication restrictions, that user becomes unusable.如果用户继承了多个身份验证限制不兼容的角色,则该用户将无法使用。
For example, if a user inherits one role in which the 例如,如果用户继承了一个角色,其中clientSource field is ["198.51.100.0"] and another role in which the clientSource field is ["203.0.113.0"] the server is unable to authenticate the user.clientSource字段为["198.51.100.0"],另一个角色的clientSource字段是["203.0.113.0"],则服务器无法对用户进行身份验证。
For more information on authentication in MongoDB, see Authentication on Self-Managed Deployments.有关MongoDB中身份验证的更多信息,请参阅自我管理部署的身份验证。
The db.createUser() method wraps the createUser command.db.createUser()方法封装createUser命令。
Behavior行为
User ID用户ID
MongoDB automatically assigns a unique MongoDB在创建时会自动为用户分配一个唯一的userId to the user upon creation.userId。
Replica set副本集
If run on a replica set, 如果在副本集上运行db.createUser() is executed using "majority" write concern by default.db.createUser(),默认情况下使用"majority"写关注执行。
Encryption加密
Warning
By default, 默认情况下,db.createUser() sends all specified data to the MongoDB instance in cleartext, even if using passwordPrompt(). db.createUser()以明文形式将所有指定的数据发送到MongoDB实例,即使使用passwordPrompt()。Use TLS transport encryption to protect communications between clients and the server, including the password sent by 使用TLS传输加密来保护客户端和服务器之间的通信,包括db.createUser(). db.createUser()发送的密码。For instructions on enabling TLS transport encryption, see Configure 有关启用TLS传输加密的说明,请参阅在自我管理部署上为TLS/SSL配置mongod and mongos for TLS/SSL on Self-Managed Deployments.mongod和mongos。
MongoDB does not store the password in cleartext. The password is only vulnerable in transit between the client and the server, and only if TLS transport encryption is not enabled.MongoDB不以明文形式存储密码。密码仅在客户端和服务器之间传输时易受攻击,并且只有在未启用TLS传输加密的情况下才易受攻击。
External Credentials外部凭证
Users created on the 在$external database should have credentials stored externally to MongoDB, as, for example, with MongoDB Enterprise installations that use Kerberos.$external数据库上创建的用户应该将凭据存储在MongoDB的外部,例如使用Kerberos的MongoDB Enterprise安装。
To use Client Sessions and Causal Consistency Guarantees with 要对$external authentication users (Kerberos, LDAP, or X.509 users), usernames cannot be greater than 10k bytes.$external身份验证用户(Kerberos、LDAP或X.509用户)使用客户端会话和因果一致性保证,用户名不能大于10k字节。
local Database数据库
You cannot create users on the local database.您无法在本地数据库上创建用户。
Required Access所需访问权限
To create a new user in a database, you must have the要在数据库中创建新用户,您必须对该数据库资源执行createUseraction on that database resource.createUser操作。To grant roles to a user, you must have the若要向用户授予角色,您必须对角色的数据库执行grantRoleaction on the role's database.grantRole操作。
The userAdmin and userAdminAnyDatabase built-in roles provide createUser and grantRole actions on their respective resources.userAdmin和userAdminAnyDatabase内置角色在各自的资源上提供createUser和grantRole操作。
Examples示例
The following 以下db.createUser() operation creates the accountAdmin01 user on the products database.db.createUser()操作在products数据库上创建accountAdmin01用户。
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.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:accountAdmin01以下角色:
the管理员数据库上的clusterAdminandreadAnyDatabaseroles on theadmindatabaseclusterAdmin和readAnyDatabase角色thereadWriterole on theproductsdatabaseproducts数据库上的readWrite角色
Create User with Roles创建具有角色的用户
The following operation creates 以下操作在accountUser in the products database and gives the user the readWrite and dbAdmin roles.products数据库中创建accountUser,并为用户提供readWrite和dbAdmin角色。
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。但是,您仍然可以像使用早期版本的mongo shell一样直接指定密码。
use products
db.createUser(
{
user: "accountUser",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ "readWrite", "dbAdmin" ]
}
)Create User without Roles创建没有角色的用户
The following operation creates a user named 以下操作在reportsUser in the admin database but does not yet assign roles:admin数据库中创建了一个名为reportsUser的用户,但尚未分配角色:
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。但是,您仍然可以像使用早期版本的mongo shell一样直接指定密码。
use admin
db.createUser(
{
user: "reportsUser",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ ]
}
)Create Administrative User with 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.admin数据库中创建了一个名为appAdmin的用户,并授予用户对config数据库的readWrite访问权限,这允许用户更改分片集群的某些设置,例如平衡器设置。
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。但是,您仍然可以像使用早期版本的mongo shell一样直接指定密码。
use admin
db.createUser(
{
user: "appAdmin",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles:
[
{ role: "readWrite", db: "config" },
"clusterAdmin"
]
}
)Create User with Authentication Restrictions创建具有身份验证限制的用户
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.admin数据库中创建了一个名为restricted的用户。此用户只能在从IP地址192.0.2.0连接到IP地址198.51.100.0时进行身份验证。
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.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"]
} ]
}
)Create User with SCRAM-SHA-256 Credentials Only仅使用SCRAM-SHA-256凭据创建用户
SCRAM-SHA-256 Credentials OnlyNote
To use SCRAM-SHA-256, the 要使用SCRAM-SHA-256,功能兼容性版本必须设置为4.0。有关featureCompatibilityVersion must be set to 4.0. For more information on featureCompatibilityVersion, see Get FeatureCompatibilityVersion and setFeatureCompatibilityVersion.featureCompatibilityVersion的更多信息,请参阅获取获取FeatureCompatibilityVersion和setFeatureCompatibilityVersion。
The following operation creates a user with only 以下操作将创建仅具有SCRAM-SHA-256 credentials.SCRAM-SHA-256凭据的用户。
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.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.authenticationMechanisms参数,则mechanisms字段只能包含authenticationMechanisms参数中指定的值。