用户
On this page
To authenticate a client in MongoDB, you must add a corresponding user to MongoDB.
在此页
要在MongoDB中认证客户端,必须向MongoDB数据库中添加相应的用户。
User Management Interface
To add a user, MongoDB provides the db.createUser()
method. When adding a user, you can assign roles to the user in order to grant privileges.
NOTE
The first user created in the database should be a user administrator who has the privileges to manage other users. See Enable Access Control.
You can also update existing users, such as to change password and grant or revoke roles. For a full list of user management methods, see User Management.
A user is uniquely identified by the user’s name and associated authentication database. Starting in MongoDB 4.0.9, a users managed by MongoDB are assigned a unique userId
. [1]
SEE ALSO
用户管理接口
添加一个用户,MongoDB提供 db.createUser()
方法,当添加一个用户,同时需要分配角色 roles 来向用户授予特权。
注意 创建第一个用户,需要具有管理其他用户特权的管理员用户。请参见 启用访问控制 。 你还可以更新现有用户,例如更改密码和授予或撤销角色。有关用户管理方法的完整列表,请参见 用户管理 。 用户是由用户名和关联的身份验证数据库唯一标识的。从MongoDB 4.0.9开始,MongoDB管理的用户被分配一个唯一的
userId
。详情请参考
Authentication Database
When adding a user, you create the user in a specific database. This database is the authentication database for the user.
A user can have privileges across different databases; that is, a user’s privileges are not limited to their authentication database. By assigning to the user roles in other databases, a user created in one database can have permissions to act on other databases. For more information on roles, see Role-Based Access Control.
The user’s name and authentication database serve as a unique identifier for that user. [1] That is, if two users have the same name but are created in different databases, they are two separate users. If you intend to have a single user with permissions on multiple databases, create a single user with roles in the applicable databases instead of creating the user multiple times in different databases.
[1] | (1 , 2 _)_Starting in version 4.0.9, MongoDB associates a user with a unique |
数据库认证
添加用户时,在特定数据库中创建用户。这个数据库是该用户的身份验证数据库。 一个用户可以拥有跨不同数据库进行授权;也就是说,用户的授权并不局限于他们的身份验证数据库。 通过将用户角色分配给其他数据库中的用户,在一个数据库中创建的用户可以拥有对其他数据库进行操作的权限。有关角色的更多信息,请参见 基于角色的访问控制 。 用户名和身份验证数据库作为该用户的唯一标识符。 唯一标识UserID详解 ,也就是说,如果两个用户具有相同的名称,但在不同的数据库中创建,那么他们是两个独立的用户。如果您打算拥有一个对多个数据库具有权限的用户,请创建一个具有适用数据库中的角色的用户,而不是在不同的数据库中多次创建该用户。
Authenticate a User
To authenticate as a user, you must provide a username, password, and the authentication database associated with that user.
To authenticate using the mongo
shell, either:
Use the
mongo
command-line authentication options (--username
,--password
, and--authenticationDatabase
) when connecting to themongod
ormongos
instance, orConnect first to the
mongod
ormongos
instance, and then run theauthenticate
command or thedb.auth()
method against the authentication database.IMPORTANT
Authenticating multiple times as different users does not drop the credentials of previously-authenticated users. This may lead to a connection having more permissions than intended by the user, and causes operations within a logical session to raise an error.
For examples of authenticating using a MongoDB driver, see the driver documentation.
用户授权
作为用户进行身份验证,您必须提供与该用户相关联的用户名、密码。详情请看 身份验证数据库 。 使用 'mongo' Shell命令进行认证:
使用 'mongo' 命令进行身份验证,选项为( '--usernmae',
--password
, 和--authenticationDatabase
),之后连接到mongod
或mongos
实例,或有关使用MongoDB驱动程序进行身份验证的示例,请参阅 驱动程序文档 。
Centralized User Data
For users created in MongoDB, MongoDB stores all user information, including name
, password
, and the user's authentication database
, in the system.users collection in the admin
database.
Do not access this collection directly but instead use the user management commands.
用户数据集中心
在MongoDB创建的用户, MongoDB存储所有用户信息,包括 name
, password
,和 用户的权限认证数据库
, 在 system.users 集合的admin
数据库中。 不要直接访问集合,而是使用 用户管理命令 。
Sharded Cluster Users
To create users for a sharded cluster, connect to the mongos
instance and add the users. Clients then authenticate these users through the mongos
instances. In sharded clusters, MongoDB stores user configuration data in the admin
database of the config servers.
分片集群用户
为分片集群创建用户,先连接到 mongos
实例并添加用户。然后客户端通过 mongos
实例对这些用户进行身份验证。在分片集群中,MongoDB将用户配置数据存储在 配置服务器 的admin
数据库中。
Shard Local Users
However, some maintenance operations, such as cleanupOrphaned
, compact
, rs.reconfig()
, require direct connections to specific shards in a sharded cluster. To perform these operations, you must connect directly to the shard and authenticate as a shard local administrative user.
To create a shard local administrative user, connect directly to the shard and create the user. MongoDB stores shard local users in the admin
database of the shard itself.
These shard local users are completely independent from the users added to the sharded cluster via mongos
. Shard local users are local to the shard and are inaccessible by mongos
.
Direct connections to a shard should only be for shard-specific maintenance and configuration. In general, clients should connect to the sharded cluster through the mongos
.
共享本地用户
一些维护操作,如 cleanupOrphaned
, compact
, rs.reconfig()
,在分片集群中,需要直接连接到特定的分片。要执行这些操作,您必须直接连接到特定分片,并作为_本地分片_管理员用户进行身份验证。 创建一个_本地分片_管理员用户,请直接连接到该分片并创建该用户。MongoDB将_本地分片_用户存储在分片本身的 admin
数据库中。 这些_本地分片_用户完全独立于通过 mongos
添加到分片集群的用户。_本地分片_用户是分片的本地用户,并通过命令 mongos
无法访问。 与分片直接连接应该仅用于分片特定的维护和配置。通常,客户端应该通过 mongos
连接到分片集群。
Localhost Exception
The localhost exception allows you to enable access control and then create the first user in the system. With the localhost exception, after you enable access control, connect to the localhost interface and create the first user in the admin
database. The first user must have privileges to create other users, such as a user with the userAdmin
or userAdminAnyDatabase
role. Connections using the localhost exception only have access to create the first user on the admin
database.
Changed in version 3.4: MongoDB 3.4 extended the localhost exception to permit execution of the db.createRole()
method. This method allows users authorizing via LDAP to create a role inside of MongoDB that maps to a role defined in LDAP. See LDAP Authorization for more information.
The localhost exception applies only when there are no users created in the MongoDB instance.
In the case of a sharded cluster, the localhost exception applies to each shard individually as well as to the cluster as a whole. Once you create a sharded cluster and add a user administrator through the mongos
instance, you must still prevent unauthorized access to the individual shards. Follow one of the following steps for each shard in your cluster:
Create an administrative user, or
Disable the localhost exception at startup. To disable the localhost exception, set the
enableLocalhostAuthBypass
parameter to0
.
本地主机异常
本地主机异常允许您启用访问控制,然后在系统中创建第一个用户。对于本地主机异常,在启用访问控制后,连接到本地主机接口并在 admin
数据库中创建第一个用户。第一个用户必须拥有创建其他用户的特权,例如具有 userAdmin
或 userAdminAnyDatabase
角色的用户。使用本地异常连接可以在 admin
数据库上创建第一个用户。 在3.4版本中: MongoDB 3.4扩展了本地主机异常,允许执行 db.createRole()
方法。该方法允许通过LDAP授权的用户在MongoDB中创建一个映射到LDAP中自定义的角色的角色。有关更多信息,请参见 LDAP授权 。 本地主机异常仅在MongoDB实例中没有创建用户时的场景。 在分片集群的情况下,本地主机异常分别应用于每个分片,也应用于整个集群。一旦您创建了一个分片集群并通过 mongos
实例添加了一个管理员用户,您仍然不能访问单个分片。要访问集群中单个分片,对集群中的每个分片执行操作步骤如下:
创建一个管理用户,或
在启动时禁用localhost异常。要禁用本地异常,请将
enableLocalhostAuthBypass
参数设置为0
。
英文原文地址:https://docs.mongodb.com/v4.2/core/security-users/
译者:管祥青
最后更新于