MongoDB-CN-Manual
  • MongoDB中文手册|官方文档中文版
  • MongoDB用户手册说明
  • MongoDB简介
    • 入门
    • 数据库和集合
      • 视图
      • 按需物化视图
      • 封顶集合
      • 时间序列集合
    • 文档
    • BSON类型
      • Comparison and Sort Order
      • MongoDB Extended JSON (v2)
      • MongoDB Extended JSON (v1)
  • 安装 MongoDB
    • 安装MongoDB社区版
      • 在Linux上安装MongoDB社区版
      • 在macOS上安装MongoDB社区版
      • 在Windows上安装MongoDB社区版
    • 安装MongoDB企业版
      • 在Linux上安装MongoDB企业版
      • 在Mac OS安装MongoDB企业版
      • 在Windows安装MongoDB企业版
      • 使用Docker安装MongoDB企业版
    • 将社区版MongoDB升级到企业版MongoDB
    • 验证MongoDB软件包的完整性
  • The mongo Shell
    • 配置mongo Shell
    • 使用 mongo Shell帮助
    • 为mongo Shell编写脚本
    • mongo Shell中的数据类型
    • mongo Shell 快速参考
  • MongoDB CRUD操作
    • 插入文档
      • 插入方法
    • 查询文档
      • 在mongo Shell中迭代游标
      • 从查询返回的项目字段
      • 查询嵌入式文档数组
      • 查询数组
      • 查询空字段或缺少字段
      • 查询嵌入/嵌套文档
    • 更新文档
      • 更新方法
      • 聚合管道更新
    • 删除文档
      • 删除方法
    • 地理空间查询
      • 用地理空间查询查找餐馆
      • GeoJSON对象
    • 批量写入操作
    • 可重试写入
    • 可重试读取
    • SQL到MongoDB的映射图表
    • 文本搜索
      • 文本索引
      • 文本索引操作
      • 集合管道中的文本索引
      • 文本索引语言
    • Read Concern读关注
      • 读关注 "local"
      • 读关注 "available"
      • 读关注 "majority"
      • 读关注 "linearizable"
      • 读关注 "snapshot"
    • Write Concern写关注
    • MongoDB CRUD概念
      • 原子性和事务
      • 读隔离性,一致性和近因性
        • 因果一致性和读写关注
      • 分布式查询
      • 通过findAndModify进行线性化读取
      • 查询计划
      • 查询优化
        • 评估当前操作性能
        • 优化查询性能
        • 写操作性能
        • 说明结果
      • 分析查询表现
      • Tailable 游标
  • MongoDB聚合
    • 聚合管道
      • 聚合管道优化
      • 聚合管道限制
      • 聚合管道和分片集合
      • 使用 Zip Code 数据集进行聚合
      • 使用用户首选项数据进行聚合
    • Map-Reduce
      • Map-Reduce 和分片集合
      • Map-Reduce 并发
      • Map-Reduce 示例
      • 执行增量 Map-Reduce
      • 对 Map Function 进行故障排除
      • 排除 Reduce Function 问题
      • Map-Reduce转换到聚合管道
    • 聚合参考
      • 聚合管道快速参考
      • 聚合命令
      • 聚合命令对比
      • 聚合表达式中的变量
      • SQL 到聚合映射图表
  • MongoDB数据模型
    • 数据建模介绍
    • 模式验证
    • 数据模型设计
      • 一对一嵌套关系模型
  • MongoDB事务
  • MongoDB事务
    • 驱动程序 API
    • 生产注意事项
    • 生产注意事项 (分片集群)
    • 事务操作
  • MongoDB索引
    • 单字段索引
    • 复合索引
    • 多键索引
      • 多键索引范围
    • 文本索引
      • 为文本索引指定语言
      • 指定文本索引的名称
      • 用权重控制搜索结果
      • 限制扫描条目的数量
    • 通配符索引
      • 通配符索引限制
    • 2dsphere 索引
      • 查询一个2dsphere索引
    • 2d 索引
      • 创建一个2d索引
      • 查询一个2d索引
      • 2d索引内部
      • 使用球面几何计算距离
    • geoHaystack 索引
      • 创建Haystack索引
      • 查询Haystack索引
    • 哈希索引
    • 索引特性
      • TTL 索引
        • 通过设置TTL使集合中的数据过期
      • 唯一索引
      • 部分索引
      • 不分大小写索引
      • Sparse 索引
    • 在填充的集合上建立索引
      • 在副本集上建立滚动索引
      • 在分片群集上建立滚动索引
    • 索引交集
    • 管理索引
    • 衡量索引使用
    • 索引策略
      • 创建索引来支持查询
      • 使用索引对查询结果进行排序
      • 确保索引适合RAM
      • 创建以确保选择性的查询
    • 索引参考
  • MongoDB安全
    • 安全检查列表
    • 启用访问控制
    • 身份验证
      • 用户
        • 添加用户
        • 权限认证机制
          • SCRAM
            • 用x.509证书来认证客户端
    • 审计
      • 配置审计过滤器
      • 配置审计
      • 系统事件审计消息
    • 网络和配置强化
    • 安全参考
      • system.roles集合
      • system.users集合
      • 资源文档
      • 权限操作
    • 附录
      • 附录-A-用于测试的 OpenSSl CA 证书
      • 附录-B-用于测试的OpenSSL服务器证书
      • 附录-C-用于测试的OpenSSL客户端证书
  • Change Streams变更流
    • 变更流生产建议
    • 变更事件
  • MongoDB复制
    • 副本集成员
    • 副本集日志
    • 副本集数据同步
    • 副本集部署架构
    • 副本集成员配置教程
    • 副本集维护教程
    • MongoDB复制参考
  • MongoDB分片
    • 分片集群组件
    • 分片键
    • 哈希分片
    • 范围分片
    • 区
      • 管理分片区
      • 按位置细分数据
      • 用于更改SLA或SLO的分层硬件
      • 按应用或客户细分数据
      • 仅插入工作负载的分布式本地写入
      • 管理分片区
    • 使用块进行数据分区
      • 在分片集群中拆分数据块
    • 分片管理
      • 查看集群设置
    • 重启一个分片集群
    • [把一个分片集群迁移到不同的硬件](fen-pian/migrate-a -sharded-cluster-to-different-hardware.md)
    • 分片参考
  • MongoDB管理
    • 产品说明
    • 操作检查列表
    • 开发检查列表
    • 配置和维护
    • 性能
    • 数据中心意识
      • MongoDB部署中的工作负载隔离
      • 区
        • 管理分片区
        • 按位置细分数据
        • 用于更改SLA或SLO的分层硬件
        • 按应用或客户细分数据
        • 仅插入工作负载的分布式本地写入
        • 管理分片区
    • MongoDB备份方法
    • MongoDB监控
  • MongoDB存储
    • 存储引擎
      • WiredTiger 存储引擎
      • 内存存储引擎
    • 日志记录
      • 管理日志记录
        • GridFS
        • FAQ:MongoDB 存储
  • MongoDB参考
    • 运算符
      • 查询与映射运算符
        • 比较查询运算符
          • $eq
          • $gt
          • $gte
          • $in
          • $lt
          • $lte
          • $ne
          • $nin
        • 逻辑查询运算符
          • $and
          • $not
          • $nor
          • $or
        • 元素查询运算符
        • 评估查询运算符
        • 地理空间查询运算符
        • 数组查询运算符
        • 按位查询运算符
        • $comment
        • 映射运算符
      • 更新运算符
        • 字段更新运算符
        • 数组更新运算符
        • 按位更新运算符
      • 聚合管道阶段
      • 聚合管道操作符
        • $abs (aggregation)
        • $acos (aggregation)
        • $acosh (aggregation)
        • $add (aggregation)
        • $addToSet (aggregation)
        • $allElementsTrue (aggregation)
        • $and (aggregation)
        • $anyElementTrue (aggregation)
        • $arrayElemAt (aggregation)
        • $arrayToObject (aggregation)
        • $asin (aggregation)
        • $asinh (aggregation)
        • $atan (aggregation)
        • $atan2 (aggregation)
        • $atanh (aggregation)
        • $avg (aggregation)
        • $ceil (aggregation)
        • $cmp (aggregation)
        • $concat (aggregation)
        • $concatArrays (aggregation)
        • $cond (aggregation)
        • $convert (aggregation)
        • $cos (aggregation)
        • $dateFromParts (aggregation)
        • $dateToParts (aggregation)
        • $dateFromString (aggregation)
        • $literal (aggregation)
      • 查询修饰符
    • 数据库命令
      • 聚合命令
      • 地理空间命令
      • 查询和写操作命令
      • 查询计划缓存命令
      • 认证命令
      • 用户管理命令
      • 角色管理命令
      • 复制命令
      • 分片命令
      • 会话命令
      • 管理命令
      • 诊断命令
      • 免费监控命令
      • 系统事件审计命令
    • mongo Shell 方法
      • 集合方法
        • db.collection.aggregate()
        • db.collection.bulkWrite()
        • db.collection.copyTo()
        • db.collection.count()
        • db.collection.countDocuments()
        • db.collection.estimatedDocumentCount()
        • db.collection.createIndex()
        • db.collection.createIndexes()
        • db.collection.dataSize()
        • db.collection.deleteOne()
        • db.collection.deleteMany()
        • db.collection.distinct()
        • db.collection.drop()
        • db.collection.dropIndex()
        • db.collection.dropIndexes()
        • db.collection.ensureIndex()
        • db.collection.explain()
        • db.collection.find()
        • db.collection.findAndModify()
        • db.collection.findOne()
        • db.collection.findOneAndDelete()
        • db.collection.findOneAndReplace()
        • db.collection.findOneAndUpdate()
        • db.collection.getIndexes()
        • db.collection.getShardDistribution()
        • db.collection.getShardVersion()
        • db.collection.insert()
        • db.collection.insertOne()
        • db.collection.insertMany()
        • db.collection.isCapped()
        • db.collection.latencyStats()
        • db.collection.mapReduce()
        • db.collection.reIndex()
        • db.collection.remove()
        • db.collection.renameCollection()
        • db.collection.replaceOne()
        • db.collection.save()
        • db.collection.stats()
        • db.collection.storageSize()
        • db.collection.totalIndexSize()
        • db.collection.totalSize()
        • db.collection.update()
        • db.collection.updateOne()
        • db.collection.updateMany()
        • db.collection.watch()
        • db.collection.validate()
    • MongoDB中的限制与阈值
    • MongoDB系统集合
    • 词汇表
    • 默认的MongoDB端口
    • 默认的MongoDB读/写关注
    • 服务器会话
  • MongoDB FAQ
    • FAQ: MongoDB基础知识
    • FAQ: MongoDB索引
    • FAQ: MongoDB并发
    • FAQ: MongoDB分片
    • FAQ: MongoDB复制和副本集
    • FAQ: MongoDB存储
    • FAQ: MongoDB诊断
  • MongoDB 版本管理
  • 联系我们
    • Tapdata Cloud
    • MongoDB中文社区
    • 社区合作伙伴—锦木信息
由 GitBook 提供支持
在本页
  • User Management Interface
  • 用户管理接口
  • Authentication Database
  • 数据库认证
  • Authenticate a User
  • 用户授权
  • Centralized User Data
  • 用户数据集中心
  • Sharded Cluster Users
  • 分片集群用户
  • Shard Local Users
  • 共享本地用户
  • Localhost Exception
  • 本地主机异常
  1. MongoDB安全
  2. 身份验证

用户

上一页身份验证下一页添加用户

最后更新于3年前

On this page

To authenticate a client in MongoDB, you must add a corresponding user to MongoDB.

在此页

要在MongoDB中认证客户端,必须向MongoDB数据库中添加相应的用户。

User Management Interface

NOTE

SEE ALSO

用户管理接口

Authentication Database

When adding a user, you create the user in a specific database. This database is the authentication database for the user.

[1]

数据库认证

Authenticate a User

  • IMPORTANT

用户授权

Centralized User Data

用户数据集中心

Sharded Cluster Users

分片集群用户

Shard Local Users

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.

共享本地用户

Localhost Exception

The localhost exception applies only when there are no users created in the MongoDB instance.

  • Create an administrative user, or

本地主机异常

  • 创建一个管理用户,或

译者:管祥青

To add a user, MongoDB provides the method. When adding a user, you can assign to the user in order to grant privileges.

The first user created in the database should be a user administrator who has the privileges to manage other users. See .

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 .

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.

添加一个用户,MongoDB提供 方法,当添加一个用户,同时需要分配角色 来向用户授予特权。

注意 创建第一个用户,需要具有管理其他用户特权的管理员用户。请参见 。 你还可以更新现有用户,例如更改密码和授予或撤销角色。有关用户管理方法的完整列表,请参见 。 用户是由用户名和关联的身份验证数据库唯一标识的。从MongoDB 4.0.9开始,MongoDB管理的用户被分配一个唯一的 userId。

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 .

The user’s name and authentication database serve as a unique identifier for that user. 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.

( , _)_Starting in version 4.0.9, MongoDB associates a user with a unique userId upon creation in MongoDB. created on the LDAP server do not have an associated document in the collection, and hence, do not have a field associated with them.

添加用户时,在特定数据库中创建用户。这个数据库是该用户的身份验证数据库。 一个用户可以拥有跨不同数据库进行授权;也就是说,用户的授权并不局限于他们的身份验证数据库。 通过将用户角色分配给其他数据库中的用户,在一个数据库中创建的用户可以拥有对其他数据库进行操作的权限。有关角色的更多信息,请参见 。 用户名和身份验证数据库作为该用户的唯一标识符。 ,也就是说,如果两个用户具有相同的名称,但在不同的数据库中创建,那么他们是两个独立的用户。如果您打算拥有一个对多个数据库具有权限的用户,请创建一个具有适用数据库中的角色的用户,而不是在不同的数据库中多次创建该用户。

To authenticate as a user, you must provide a username, password, and the associated with that user.

To authenticate using the shell, either:

Use the command-line authentication options (, , and ) when connecting to the or instance, or

Connect first to the or instance, and then run the command or the method against the .

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 to raise an error.

For examples of authenticating using a MongoDB driver, see the .

作为用户进行身份验证,您必须提供与该用户相关联的用户名、密码。详情请看 。 使用 Shell命令进行认证:

使用 命令进行身份验证,选项为( , , 和 ),之后连接到 或 实例,或

首先连接到 或 实例,然后运行 命令或 方法,详情请参考 。

重要的 不同用户多次身份验证,因为后面的用户不会删除之前经过身份验证的用户的凭据。这可能导致连接时,拥有比用户预期更多的权限,并导致 中的操作引发错误。

有关使用MongoDB驱动程序进行身份验证的示例,请参阅 。

For users created in MongoDB, MongoDB stores all user information, including , , and the , in the collection in the admin database.

Do not access this collection directly but instead use the .

在MongoDB创建的用户, MongoDB存储所有用户信息,包括 , ,和 , 在 集合的admin数据库中。 不要直接访问集合,而是使用 。

To create users for a sharded cluster, connect to the instance and add the users. Clients then authenticate these users through the instances. In sharded clusters, MongoDB stores user configuration data in the admin database of the .

为分片集群创建用户,先连接到 实例并添加用户。然后客户端通过 实例对这些用户进行身份验证。在分片集群中,MongoDB将用户配置数据存储在 的admin 数据库中。

However, some maintenance operations, such as , , , 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.

These shard local users are completely independent from the users added to the sharded cluster via . Shard local users are local to the shard and are inaccessible by .

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 .

一些维护操作,如 , , ,在分片集群中,需要直接连接到特定的分片。要执行这些操作,您必须直接连接到特定分片,并作为_本地分片_管理员用户进行身份验证。 创建一个_本地分片_管理员用户,请直接连接到该分片并创建该用户。MongoDB将_本地分片_用户存储在分片本身的 admin 数据库中。 这些_本地分片_用户完全独立于通过 添加到分片集群的用户。_本地分片_用户是分片的本地用户,并通过命令 无法访问。 与分片直接连接应该仅用于分片特定的维护和配置。通常,客户端应该通过 连接到分片集群。

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 or 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 method. This method allows users authorizing via LDAP to create a role inside of MongoDB that maps to a role defined in LDAP. See for more information.

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 instance, you must still prevent unauthorized access to the individual shards. Follow one of the following steps for each shard in your cluster:

Disable the localhost exception at startup. To disable the localhost exception, set the parameter to 0.

本地主机异常允许您启用访问控制,然后在系统中创建第一个用户。对于本地主机异常,在启用访问控制后,连接到本地主机接口并在 admin 数据库中创建第一个用户。第一个用户必须拥有创建其他用户的特权,例如具有 或 角色的用户。使用本地异常连接可以在 admin 数据库上创建第一个用户。 在3.4版本中: MongoDB 3.4扩展了本地主机异常,允许执行 方法。该方法允许通过LDAP授权的用户在MongoDB中创建一个映射到LDAP中自定义的角色的角色。有关更多信息,请参见 。 本地主机异常仅在MongoDB实例中没有创建用户时的场景。 在分片集群的情况下,本地主机异常分别应用于每个分片,也应用于整个集群。一旦您创建了一个分片集群并通过 实例添加了一个管理员用户,您仍然不能访问单个分片。要访问集群中单个分片,对集群中的每个分片执行操作步骤如下:

在启动时禁用localhost异常。要禁用本地异常,请将 参数设置为0。

英文原文地址:

User Management Interface
Authentication Database
Authenticate a User
Centralized User Data
Sharded Cluster Users
Localhost Exception
用户管理接口
数据库认证
用户认证
用户数据中心
分片集群用户
本地主机异常
db.createUser()
roles
Enable Access Control
User Management
[1]
Add Users
db.createUser()
roles
启用访问控制
用户管理
详情请参考
Role-Based Access Control
[1]
基于角色的访问控制
唯一标识UserID详解
authentication database
mongo
mongo
--username
--password
--authenticationDatabase
mongod
mongos
mongod
mongos
authenticate
db.auth()
authentication database
logical session
driver documentation
身份验证数据库
'mongo'
'mongo'
'--usernmae'
--password
--authenticationDatabase
mongod
mongos
mongod
mongos
authenticate
db.auth()
数据库认证授权
逻辑会话
驱动程序文档
name
password
user's authentication database
system.users
user management commands
name
password
用户的权限认证数据库
system.users
用户管理命令
mongos
mongos
config servers
mongos
mongos
配置服务器
cleanupOrphaned
compact
rs.reconfig()
mongos
mongos
mongos
cleanupOrphaned
compact
rs.reconfig()
mongos
mongos
mongos
userAdmin
userAdminAnyDatabase
db.createRole()
LDAP Authorization
mongos
enableLocalhostAuthBypass
userAdmin
userAdminAnyDatabase
db.createRole()
LDAP授权
mongos
enableLocalhostAuthBypass
https://docs.mongodb.com/v4.2/core/security-users/
1
2
LDAP managed users
system.users
userId