On this page本页内容
Enabling access control on a MongoDB deployment enforces authentication, requiring users to identify themselves.对MongoDB部署启用访问控制将强制进行身份验证,要求用户标识自己。When accessing a MongoDB deployment that has access control enabled, users can only perform actions as determined by their roles.访问启用了访问控制的MongoDB部署时,用户只能执行由其角色确定的操作。
The following tutorial enables access control on a standalone 下面的教程在独立的mongod
instance and uses the default authentication mechanism.mongod
实例上启用访问控制,并使用默认的身份验证机制。For all supported authentication mechanisms, see Authentication Mechanisms.有关所有支持的身份验证机制,请参阅身份验证机制。
With access control enabled, ensure you have a user with 启用访问控制后,请确保管理数据库中有一个具有userAdmin
or userAdminAnyDatabase
role in the admin
database.userAdmin
或userAdminAnyDatabase
角色的用户。This user can administrate user and roles such as: create users, grant or revoke roles from users, and create or modify customs roles.此用户可以管理用户和角色,例如:创建用户、从用户授予或撤消角色,以及创建或修改自定义角色。
The following procedure first adds a user administrator to a MongoDB instance running without access control and then enables access control.下面的过程首先向运行时没有访问控制的MongoDB实例添加用户管理员,然后启用访问控制。
Note
The example MongoDB instance uses 示例MongoDB实例使用端口port 27017
and the data directory /var/lib/mongodb
directory .27017
和数据目录/var/lib/mongodb
。The example assumes the existence of the data directory 该示例假设存在数据目录/var/lib/mongodb
./var/lib/mongodb
。Specify a different data directory as appropriate.根据需要指定其他数据目录。
From the 在mongo
shell, add a user with the userAdminAnyDatabase
role in the admin
database.mongo
shell中,在admin
数据库中添加一个具有userAdminAnyDatabase
角色的用户。Include additional roles as needed for this user.包括此用户所需的其他角色。For example, the following creates the user 例如,下面在myUserAdmin
in the admin
database with the userAdminAnyDatabase
role and the readWriteAnyDatabase
role.admin
数据库中创建用户myUserAdmin
,角色为userAdminAnyDatabase
和readWriteAnyDatabase
。
Tip
Starting in version 4.2 of the 从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.mongo
shell的4.2版开始,您可以将passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo
shell.mongo
shell一样直接指定密码。
Note
The database where you create the user (in this example, 创建用户的数据库(在本例中为admin
) is the user’s authentication database.admin
)是用户的身份验证数据库。Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.尽管用户将对此数据库进行身份验证,但用户可以在其他数据库中拥有角色;即,用户的身份验证数据库不限制用户的权限。
mongod
instance.mongod
实例。mongo
shell, issue the following command:mongo
shell发出以下命令:
mongo
shell.mongo
shell。mongod
with access control enabled.mongod
。
mongod
from the command line, add the --auth
command line option:mongod
,请添加--auth
命令行选项:
mongod
using a configuration file, add the security.authorization
configuration file setting:mongod
,请添加security.authorization
配置文件设置:
Clients that connect to this instance must now authenticate themselves as a MongoDB user.连接到此实例的客户端现在必须将自己验证为MongoDB用户。Clients can only perform actions as determined by their assigned roles.客户端只能执行由其分配的角色确定的操作。
Using the 使用mongo
shell, you can:mongo
shell,您可以:
db.auth()
method to authenticate.db.auth()
验证方法。Start a 使用mongo
shell with the -u <username>
, -p
, and the --authenticationDatabase <database>
command line options:-u<username>
、-p
和--authenticationDatabase<database>
命令行选项启动mongo
shell:
Enter your password when prompted.出现提示时输入密码。
Connect the 将mongo
shell to the mongod
:mongo
shell连接到mongod:
In the 在mongo
shell, switch to the authentication database (in this case, admin
), and use db.auth(<username>, <pwd>)
method to authenticate:mongo
shell中,切换到身份验证数据库(在本例中为admin
),并使用db.auth(<username>, <pwd>)
方法验证:
Tip
Starting in version 4.2 of the 从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.mongo
shell的4.2版开始,您可以将passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo
shell.mongo
shell一样直接指定密码。
Enter the password when prompted.出现提示时输入密码。
Once authenticated as the user administrator, use 身份验证为用户管理员后,使用db.createUser()
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 readWrite
role in the test
database as well as read
role in the reporting
database.myTester
添加到test
数据库中,该用户在测试数据库中具有readWrite
角色,在reporting
数据库中具有read
角色。
Tip
Starting in version 4.2 of the 从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.mongo
shell的4.2版开始,您可以将passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo
shell.mongo
shell一样直接指定密码。
Note
The database where you create the user (in this example, 创建用户(在本例中为test
) is that user’s authentication database.test
)的数据库是该用户的身份验证数据库。Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.尽管用户将对此数据库进行身份验证,但用户可以在其他数据库中拥有角色;即,用户的身份验证数据库不限制用户的权限。
After creating the additional users, disconnect the 创建其他用户后,断开mongo
shell.mongo
shell的连接。
myTester
.myTester
进行身份验证。¶After disconnecting the 以mongo
shell as myUserAdmin
, reconnect as myTester
.myUserAdmin
身份断开mongo
shell后,以myTester
身份重新连接。You can:你可以:
db.auth()
method to authenticate.db.auth()
方法验证。Start a 使用mongo
shell with the -u <username>
, -p
, and the --authenticationDatabase <database>
command line options:-u<username>
、-p
和--authenticationDatabase<database>
命令行选项启动mongo
shell:
Enter the password for the user when prompted.出现提示时输入用户的密码。
Connect the 将mongo
shell to the mongod
:mongo
shell连接到mongod
:
In the 在mongo
shell, switch to the authentication database (in this case, test
), and use db.auth(<username>, <pwd>)
method to authenticate:mongo
shell中,切换到身份验证数据库(在本例中为test
),并使用db.auth(<username>, <pwd>)
方法验证:
Tip
Starting in version 4.2 of the 从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.mongo
shell的4.2版开始,您可以将passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo
shell.mongo
shell一样直接指定密码。
Enter the password for the user when prompted.出现提示时输入用户的密码。
myTester
.myTester
。¶As 作为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 test
database.myTester
进行身份验证后,将文档插入到测试数据库中的集合中。For example, you can perform the following insert operation in the 例如,可以在test
database:test
数据库中执行以下插入操作:
See also另请参见
Replica sets and sharded clusters require internal authentication between members when access control is enabled.启用访问控制时,副本集和分片群集需要成员之间的内部身份验证。For more details, please see Internal/Membership Authentication.有关更多详细信息,请参阅内部/成员身份验证。
You can create users either before or after enabling access control.您可以在启用访问控制之前或之后创建用户。If you enable access control before creating any user, MongoDB provides a localhost exception which allows you to create a user administrator in the 如果在创建任何用户之前启用访问控制,MongoDB将提供本地主机异常,允许您在admin
database.admin
数据库中创建用户管理员。Once created, you must authenticate as the user administrator to create additional users as needed.创建后,必须以用户管理员身份进行身份验证,才能根据需要创建其他用户。