createUser
On this page本页内容
Definition定义
createUser-
Creates a new user on the database where you run the command.在运行命令的数据库上创建新用户。The如果用户存在,createUsercommand returns a duplicate user error if the user exists.createUser命令将返回一个重复的用户错误。TipIn在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.mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要方便,或者需要额外的返回字段,请使用数据库命令。
Syntax语法
The command has the following syntax:该命令具有以下语法:
Starting in version 4.2 of the 从mongoshell的4.2版本开始,您可以将mongo shell, you can use the passwordPrompt() method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call. passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongoshell一样直接指定密码。
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>", ... ], //Available starting in MongoDB 4.0
digestPassword: <boolean>,
comment: <any>
}
)
Command Fields命令字段
createUser has the following fields:具有以下字段:
createUser | string | |
pwd | string | pwd field is not required if you run createUser on the $external database to create users who have credentials stored externally to MongoDB.$external数据库上运行createUser来创建具有存储在MongoDB外部的凭据的用户,则不需要pwd字段。
Tip
|
customData | document | |
roles | array | [] to create users without roles.[]来创建没有角色的用户。 |
digestPassword | boolean | true,则服务器从客户端接收未消化的密码,并对密码进行消化。false,客户端将对密码进行摘要处理,并将摘要处理后的密码传递给服务器。SCRAM-SHA-256 SCRAM-SHA-256不兼容true. true。false. false。 |
writeConcern | document | |
authenticationRestrictions | array | |
mechanisms | array | authenticationMechanisms is specified, you can only specify a subset of the authenticationMechanisms.authenticationMechanisms,则只能指定authenticationMechanisms的子集。Valid values are:
4.0 is both SCRAM-SHA-1 and SCRAM-SHA-256.The default for featureCompatibilityVersion is 3.6 is SCRAM-SHA-1. |
digestPassword | boolean | true,则服务器从客户端接收未消化的密码,并对密码进行消化。SCRAM-SHA-256 false,客户端将对密码进行摘要处理,并将摘要处理后的密码传递给服务器。与SCRAM-SHA-256不兼容true. true。false. false。 |
comment | any |
|
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 要指定在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. authenticationRestrictions文档只能包含以下字段。The server throws an error if the 如果authenticationRestrictions document contains an unrecognized field:authenticationRestrictions文档包含无法识别的字段,则服务器将引发错误:
clientSource | Array of IP addresses and/or CIDR ranges | |
serverAddress | Array of IP addresses and/or CIDR ranges |
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.有关MongoDB中身份验证的更多信息,请参阅身份验证。
Behavior行为
User ID用户ID
Starting in version 4.0.9, MongoDB automatically assigns a unique 从4.0.9版本开始,MongoDB会在创建时自动为用户分配一个唯一的userId to the user upon creation.userId。
Encryption加密
By default, 默认情况下,createUser sends all specified data to the MongoDB instance in cleartext, even if using passwordPrompt(). createUser以明文形式将所有指定的数据发送到MongoDB实例,即使使用passwordPrompt()也是如此。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.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. The command gives accountAdmin01 the clusterAdmin and readAnyDatabase roles on the admin database and the readWrite role on the products database:createUser命令在products数据库上创建一个用户accountAdmin01。该命令赋予accountAdmin01在admin数据库上的clusterAdmin和readAnyDatabase角色,以及products数据库上的readWrite角色:
Starting in version 4.2 of the 从mongoshell的4.2版本开始,您可以将mongo shell, you can use the passwordPrompt() method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。但是,您仍然可以像使用早期版本的mongoshell一样直接指定密码。
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 }
} )