On this page本页内容
myTester
myTester
进行身份验证myTester
myTester
的身份插入文档With access control enabled, users are required to identify themselves. 启用访问控制后,用户需要识别自己的身份。You have to grant a user one or more roles. 您必须向用户授予一个或多个角色。A role grants a user privileges to perform certain actions on MongoDB resources.角色授予用户在MongoDB资源上执行某些操作的权限。
Each application and user of a MongoDB system should map to a distinct user. MongoDB系统的每个应用程序和用户都应该映射到不同的用户。This principle of access isolation facilitates access revocation and ongoing user maintenance. 这种访问隔离原则有助于访问撤销和持续的用户维护。To ensure a system of least privilege, only grant the minimal set of privileges required to a user.为了确保系统具有最低权限,只授予用户所需的最低权限集。
To be able to create users, you need to:要创建用户,您需要:
For routine user creation, you must possess the following permissions:对于常规用户创建,您必须拥有以下权限:
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 procedure uses SCRAM authentication. 以下程序使用SCRAM认证。For additional information on other authentication mechanisms, see Additional Examples.有关其他身份验证机制的更多信息,请参阅其他示例。
Using 使用mongosh
, connect to your primary mongod
or, in a sharded cluster, connect to your mongos
and authenticate as a user administrator or a user with the required privileges:mongosh
,连接到您的主mongod
,或者在分片集群中,连接到mongos
,并作为用户管理员或具有所需权限的用户进行身份验证:
Start 使用mongosh
with the -u <username>
, -p
, and the --authenticationDatabase <database>
command line options:-u <username>
、-p
和--authenticationDatabase <database>
命令行选项启动mongosh
:
mongosh --port 27017 --authenticationDatabase \ "admin" -u "myUserAdmin" -p
Enter your password when prompted.提示时输入密码。
Using 使用mongosh
, connect to your database deployment:mongosh
连接到您的数据库部署:
mongosh --port 27017
In 在mongosh
, switch to the authentication database (in this case, admin
), and use the db.auth(<username>, <pwd>)
method to authenticate:mongosh
中,切换到身份验证数据库(在本例中为admin
),并使用db.auth(<username>, <pwd>)
方法进行身份验证:
use admin db.auth("myUserAdmin", passwordPrompt()) // or cleartext password
The passwordPrompt()
method prompts you to enter the password. passwordPrompt()
方法会提示您输入密码。You can also specify your password directly as a string. We recommend to use the 您也可以直接将密码指定为字符串。我们建议使用passwordPrompt()
method to avoid the password being visible on your screen and potentially leaking the password to your shell history.passwordPrompt()
方法,以避免密码在屏幕上可见,并可能将密码泄露到shell历史记录中。
Enter the password when prompted.提示时输入密码。
The following step uses SCRAM authentication. 以下步骤使用SCRAM身份验证。For additional information on other authentication mechanisms, see Additional Examples.有关其他身份验证机制的更多信息,请参阅其他示例。
After authenticating as the user administrator, use the 作为用户管理员进行身份验证后,使用db.createUser()
method to create additional users. db.createUser()
方法创建其他用户。You can assign any built-in roles or user-defined roles to the users.您可以将任何内置角色或用户定义的角色分配给用户。
The following operation adds a user 以下操作将用户myTester
to the test
database who has the readWrite
role in the test
database as well as the read
role in the reporting
database.myTester
添加到test
数据库,该用户在test
数据库中具有readWrite
角色,在报告数据库中具有读取角色。
use test db.createUser( { user: "myTester", pwd: passwordPrompt(), // or cleartext password roles: [ { role: "readWrite", db: "test" }, { role: "read", db: "reporting" } ] } )
The passwordPrompt()
method prompts you to enter the password. passwordPrompt()
方法会提示您输入密码。You can also specify your password directly as a string. We recommend to use the 您也可以直接将密码指定为字符串。我们建议使用passwordPrompt()
method to avoid the password being visible on your screen and potentially leaking the password to your shell history.passwordPrompt()
方法,以避免密码在屏幕上可见,并可能将密码泄露到shell历史记录中。
The database where you create the user (in this example, 创建用户的数据库(在本例中为test
) is that user's authentication database. test
)是该用户的身份验证数据库。Although the user authenticates to this database, the user can have roles in other databases. 尽管用户对此数据库进行身份验证,但用户可以在其他数据库中具有角色。The user's authentication database does not limit the user's privileges.用户的身份验证数据库不会限制用户的权限。
After creating the additional users, exit 创建完附加用户后,退出mongosh
.mongosh
。
myTester
myTester
进行身份验证It is not possible to switch between users in the same 不可能在同一个mongosh
session. mongosh
会话中的用户之间切换。Authenticating as a different user means the session has the privileges of both authenticated users. 以不同用户身份进行身份验证意味着会话具有两个已验证用户的权限。To switch between users exit and relaunch 在用户退出和重新启动mongosh
.mongosh
之间切换。
After exiting 以mongosh
as myUserAdmin
, reconnect as myTester
:myUserAdmin
身份退出mongosh
后,以myTester
身份重新连接:
Start 使用mongosh
with the -u <username>
, -p
, and the --authenticationDatabase <database>
command line options:-u <username>
、-p
和--authenticationDatabase <database>
命令行选项启动mongosh
:
mongosh --port 27017 -u "myTester" \ --authenticationDatabase "test" -p
Enter the password for the user when prompted.出现提示时,输入用户的密码。
Using 使用mongosh
, connect to your database deployment:mongosh
连接到您的数据库部署:
mongosh --port 27017
In 在mongosh
, switch to the authentication database (in this case, admin
), and use the db.auth(<username>, <pwd>)
method to authenticate:mongosh
中,切换到身份验证数据库(在本例中为admin
),并使用db.auth(<username>, <pwd>)
方法进行身份验证:
use test db.auth("myTester", passwordPrompt()) // or cleartext password
The passwordPrompt()
method prompts you to enter the password. passwordPrompt()
方法会提示您输入密码。You can also specify your password directly as a string. 您也可以直接将密码指定为字符串。We recommend to use the 我们建议使用passwordPrompt()
method to avoid the password being visible on your screen and potentially leaking the password to your shell history.passwordPrompt()
方法,以避免密码在屏幕上可见,并可能将密码泄露到shell历史记录中。
Enter the password for the user when prompted.出现提示时,输入用户的密码。
myTester
myTester
的身份插入文档As the user 作为用户myTester
, you have privileges to perform read and write operations in the test
database (as well as perform read operations in the reporting
database). myTester
,您有权在test
数据库中执行读和写操作(以及在reporting
数据库中执行读取操作)。Once authenticated as 通过myTester
, insert a document into a collection in the test
database. myTester
身份验证后,将文档插入test
数据库中的集合中。For example, you can perform the following insert operation in the 例如,您可以在test
database:test
数据库中执行以下插入操作:
db.foo.insertOne( { x: 1, y: 1 } )
The following operation creates a user in the 以下操作在reporting
database with the specified name, password, and roles.reporting
数据库中创建一个具有指定名称、密码和角色的用户。
The passwordPrompt()
method prompts you to enter the password. passwordPrompt()
方法会提示您输入密码。You can also specify your password directly as a string. We recommend to use the 您也可以直接将密码指定为字符串。我们建议使用passwordPrompt()
method to avoid the password being visible on your screen and potentially leaking the password to your shell history.passwordPrompt()
方法,以避免密码在屏幕上可见,并可能将密码泄露到shell历史记录中。
use reporting db.createUser( { user: "reportsUser", pwd: passwordPrompt(), // or cleartext password roles: [ { role: "read", db: "reporting" }, { role: "read", db: "products" }, { role: "read", db: "sales" }, { role: "readWrite", db: "accounts" } ] } )
Users that authenticate to MongoDB using an external authentication mechanism, such as Kerberos, must be created in the 必须在$external
database, which allows mongos
or mongod
to consult an external source for authentication.$external
数据库中创建使用外部身份验证机制(如Kerberos)对MongoDB进行身份验证的用户,该数据库允许mongos
或mongod
咨询外部源进行身份验证。
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字节。
For Kerberos authentication, you must add the Kerberos principal as the username. 对于Kerberos身份验证,必须添加Kerberos主体作为用户名。You do not need to specify a password.您不需要指定密码。
The following operation adds the Kerberos principal 下面的操作添加Kerberos主体reportingapp@EXAMPLE.NET
with read-only access to the records
database:reportingapp@EXAMPLE.NET
以只读方式访问records
数据库:
use $external db.createUser( { user: "reportingapp@EXAMPLE.NET", roles: [ { role: "read", db: "records" } ] } )
For more information about setting up Kerberos authentication for your MongoDB deployment, see the following tutorials:有关为MongoDB部署设置Kerberos身份验证的更多信息,请参阅以下教程:
Users that authenticate to MongoDB using an external authentication mechanism, such as LDAP, must be created in the 必须在$external
database, which allows mongos
or mongod
to consult an external source for authentication.$external
数据库中创建使用外部身份验证机制(如LDAP)对MongoDB进行身份验证的用户,该数据库允许mongos
或mongod
咨询外部源进行身份验证。
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字节。
For LDAP authentication, you must specify a username. 对于LDAP身份验证,必须指定用户名。You do not need to specify the password, as that is handled by the LDAP service.您不需要指定密码,因为这是由LDAP服务处理的。
The following operation adds the 以下操作添加了对reporting
user with read-only access to the records
database:records
数据库具有只读访问权限的reporting
用户:
use $external db.createUser( { user: "reporting", roles: [ { role: "read", db: "records" } ] } )
For more information about setting up LDAP authentication for your MongoDB deployment, see the following tutorials:有关为MongoDB部署设置LDAP身份验证的更多信息,请参阅以下教程:
Users that authenticate to MongoDB using an external authentication mechanism, such as x.509 Client Certificate Authentication, must be created in the 使用外部身份验证机制(如x.509客户端证书身份验证)对MongoDB进行身份验证的用户必须在$external
database, which allows mongos
or mongod
to consult an external source for authentication.$external
数据库中创建,这允许mongos
或mongod
咨询外部来源进行身份验证。
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字节。
For x.509 Client Certificate authentication, you must add the value of the 对于x.509客户端证书身份验证,您必须以MongoDB用户的身份从客户端证书中添加subject
from the client certificate as a MongoDB user. subject
的值。Each unique x.509 client certificate corresponds to a single MongoDB user. 每个唯一的x.509客户端证书对应于一个MongoDB用户。You do not need to specify a password.您不需要指定密码。
The following operation adds the client certificate subject 以下操作添加了客户端证书使用者CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry
user with read-only access to the records
database.CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry
用户,这些用户对records
数据库具有只读访问权限。
use $external db.createUser( { user: "CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry", roles: [ { role: "read", db: "records" } ] } )
For more information about setting up x.509 Client Certificate authentication for your MongoDB deployment, see the following tutorials:有关为MongoDB部署设置x.509客户端证书身份验证的更多信息,请参阅以下教程:
To manage users, assign roles, and create custom roles, see Manage Users and Roles.要管理用户、分配角色和创建自定义角色,请参阅管理用户和角色。