On this page本页内容
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.如果数据库中已存在用户,则返回重复的用户错误。
The db.createUser() method has the following syntax:db.createUser()方法语法如下所示:
user |
document | |
writeConcern |
document | writeConcern document takes the same fields as the getLastError command.writeConcern文档采用与getLastError命令相同的字段。 |
The user document defines the user and has the following form:user文档定义了用户,并具有以下形式:
Tip
Starting in version 4.2 of the 从mongo shell的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.mongo shell一样直接指定密码。
The user document has the following fields:user文档包含以下字段:
user |
string | |
pwd |
string |
Tip
|
customData |
document | |
roles |
array | [] to create users without roles.[]来创建没有角色的用户。 |
authenticationRestrictions |
array |
|
mechanisms |
array |
|
passwordDigestor |
string |
|
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()的同一数据库中存在的角色,可以使用角色名称指定该角色:
Or you can specify the role with a document, as in:也可以通过文档指定角色,如中所示:
To specify a role that exists in a different database, specify the role with a document.要指定存在于其他数据库中的角色,请使用文档指定该角色。
New in version 3.6.版本3.6中的新功能。
The authenticationRestrictions document can contain only the following fields. The server throws an error if the authenticationRestrictions document contains an unrecognized field:authenticationRestrictions文档只能包含以下字段。如果authenticationRestrictions文档包含无法识别的字段,服务器将抛出错误:
| Value | ||
|---|---|---|
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.有关MongoDB中身份验证的更多信息,请参阅身份验证。
The db.createUser() method wraps the createUser command.db.createUser()方法包装createUser命令。
Starting in version 4.0.9, MongoDB automatically assigns a unique 从4.0.9版开始,MongoDB会在创建时自动为用户分配一个唯一的userId to the user upon creation.userId。
If run on a replica set, 如果在副本集上运行,db.createUser() is executed using majority write concern by default.db.createUser()将默认使用majority写入关注点执行。
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 mongod and mongos for TLS/SSL.有关启用TLS传输加密的说明,请参阅为TLS/SSL配置mongod和mongos。
MongoDB does not store the password in cleartext. MongoDB不以明文形式存储密码。The password is only vulnerable in transit between the client and the server, and only if TLS transport encryption is not enabled.只有在客户端和服务器之间传输时,并且只有在未启用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数据库上创建的用户应该具有存储在MongoDB外部的凭据,例如,使用Kerberos的MongoDB企业安装。
Changed in version 3.6.3:在版本3.6.3中更改:To use sessions with 要与$external authentication users (i.e. Kerberos, LDAP, x.509 users), the usernames cannot be greater than 10k bytes.$external authentication用户(即Kerberos、LDAP、x.509用户)使用会话,用户名不能超过10k字节。
localYou cannot create users on the local database.无法在local数据库上创建用户。
createUser action on that database resource.createUser操作。grantRole action 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操作。
The following 以下db.createUser() operation creates the accountAdmin01 user on the products database.db.createUser()操作在products数据库上创建accountAdmin01用户。
Tip
Starting in version 4.2 of the 从mongo shell的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.mongo shell一样直接指定密码。
The operation gives 该操作赋予accountAdmin01 the following roles:accountAdmin01以下角色:
clusterAdmin and readAnyDatabase roles on the admin databaseclusterAdmin和readAnyDatabase角色readWrite role on the products databaseproducts数据库上的readWrite角色The following operation creates 以下操作在产品数据库中创建accountUser in the products database and gives the user the readWrite and dbAdmin roles.accountUser,并为用户提供readWrite和dbAdmin角色。
Tip
Starting in version 4.2 of the 从mongo shell的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.mongo shell一样直接指定密码。
The following operation creates a user named 以下操作在reportsUser in the admin database but does not yet assign roles:admin数据库中创建名为reportsUser的用户,但尚未分配角色:
Tip
Starting in version 4.2 of the 从mongo shell的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.mongo shell一样直接指定密码。
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的用户,并为用户提供对配置数据库的读写访问权限,这允许用户更改分片集群的某些设置,例如平衡器设置。
Tip
Starting in version 4.2 of the 从mongo shell的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.mongo shell一样直接指定密码。
The following operation creates a user named 以下操作在restricted in the admin database. admin数据库中创建名为restricted的用户。This user may only authenticate if connecting from IP address 该用户只能在从IP地址192.0.2.0 to IP address 198.51.100.0.192.0.2.0连接到IP地址198.51.100.0时进行身份验证。
Tip
Starting in version 4.2 of the 从mongo shell的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.mongo shell一样直接指定密码。
SCRAM-SHA-256 Credentials OnlySCRAM-SHA-256凭据创建用户¶Note
To use SCRAM-SHA-256, the 要使用SCRAM-SHA-256,featureCompatibilityVersion must be set to 4.0. featureCompatibilityVersion必须设置为4.0。For more information on featureCompatibilityVersion, see View FeatureCompatibilityVersion and 有关setFeatureCompatibilityVersion.featureCompatibilityVersion的更多信息,请参阅查看FeatureCompatibilityVersion和setFeatureCompatibilityVersion。
The following operation creates a user with only 以下操作将创建一个仅具有SCRAM-SHA-256 credentials.SCRAM-SHA-256凭据的用户。
Tip
Starting in version 4.2 of the 从mongo shell的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.mongo shell一样直接指定密码。
If the 如果设置了authenticationMechanisms parameter is set, the mechanisms field can only include values specified in the authenticationMechanisms parameter.authenticationMechanisms参数,则mechanisms字段只能包括authenticationMechanisms参数中指定的值。