Definition定义
createUserCreates a new user on the database where you run the command. The在运行命令的数据库上创建新用户。如果用户存在,createUsercommand returns a duplicate user error if the user exists.createUser命令将返回重复用户错误。Tip
In在mongosh, this command can also be run through thedb.createUser()helper method.mongosh中,此命令也可以通过db.createUser()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, 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.mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用database命令。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- 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的源代码可用、免费使用和自我管理版本
Important
This command is not supported in MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.MongoDB Atlas集群不支持此命令。有关Atlas支持所有命令的信息,请参阅不支持的命令。
Syntax语法
The command has the following syntax:该命令具有以下语法:
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一样直接指定密码。
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>
}
)
Command Fields命令字段
createUser has the following fields:具有以下字段:
createUser | ||
pwd |
| |
customData | ||
roles | [] to create users without roles.[]来创建没有角色的用户。 | |
digestPassword |
| |
writeConcern | ||
authenticationRestrictions | ||
mechanisms |
| |
digestPassword |
| |
comment |
|
Roles角色
In the 在角色字段中,您可以指定内置角色和用户定义的角色。roles field, you can specify both built-in roles and user-defined 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: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中身份验证的更多信息,请参阅自我管理部署的身份验证。
Behavior行为
User ID用户ID
MongoDB automatically assigns a unique MongoDB在创建时会自动为用户分配一个唯一的用户ID。userId to the user upon creation.
Encryption加密
Warning
By default, 默认情况下,即使使用createUser sends all specified data to the MongoDB instance in cleartext, even if using passwordPrompt(). passwordPrompt(),createUser也会以明文形式将所有指定的数据发送到MongoDB实例。Use TLS transport encryption to protect communications between clients and the server, including the password sent by 使用TLS传输加密来保护客户端和服务器之间的通信,包括createUser. 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.您无法在本地数据库上创建用户。
Username Limits用户名限制
Usernames must consist of at least one character and cannot be larger than 7MB.用户名必须至少包含一个字符,并且不能大于7MB。
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操作。
Example示例
The following 以下createUser command creates a user accountAdmin01 on the products database. createUser命令在products数据库上创建用户accountAdmin01。The command gives 该命令为accountAdmin01 the clusterAdmin and readAnyDatabase roles on the admin database and the readWrite role on the products database:accountAdmin01赋予了admin数据库上的clusterAdmin和readAnyDatabase角色,以及products数据库上的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. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.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 }
} )