Overview概述
Enforcing access control on a sharded cluster requires configuring:在分片集群上实施访问控制需要配置:
Security between components of the cluster using Internal Authentication.使用内部身份验证的群集组件之间的安全性。Security between connecting clients and the cluster using User Access Controls.使用用户访问控制连接客户端和群集之间的安全性。
For this tutorial, each member of the sharded cluster must use the same internal authentication mechanism and settings. 对于本教程,分片集群的每个成员都必须使用相同的内部身份验证机制和设置。This means enforcing internal authentication on each 这意味着对集群中的每个mongos and mongod in the cluster.mongos和mongod强制执行内部身份验证。
The following tutorial uses a keyfile to enable internal authentication.以下教程使用键文件启用内部身份验证。
Enforcing internal authentication also enforces user access control. 强制内部身份验证也会强制用户访问控制。To connect to the replica set, clients like 要连接到副本集,像mongosh need to use a user account. See Access Control.mongosh这样的客户端需要使用用户帐户。请参阅访问控制。
CloudManager and OpsManagerCloudManager和OpsManager
If you are using Cloud Manager or Ops Manager to manage your deployment, see the respective Cloud Manager manual or the Ops Manager manual to enforce authentication.如果您使用Cloud Manager或Ops Manager来管理部署,请参阅相应的Cloud Manager手册或Ops Manager手册以强制执行身份验证。
Considerations注意事项
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.为避免因IP地址更改而进行配置更新,请使用DNS主机名而不是IP地址。在配置副本集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address fail startup validation and do not start.使用主机名而不是IP地址来配置跨拆分网络范围的集群。从MongoDB 5.0开始,仅配置了IP地址的节点无法启动验证,也无法启动。
IP BindingIP绑定
MongoDB binaries, MongoDB二进制文件mongod and mongos, bind to localhost by default.mongod和mongos默认绑定到localhost。
Keyfile Security键文件安全
Keyfiles are bare-minimum forms of security and are best suited for testing or development environments. For production environments we recommend using X.509 certificates.键文件是最低限度的安全形式,最适合测试或开发环境。对于生产环境,我们建议使用X.509证书。
Access Control访问控制
This tutorial covers creating the minimum number of administrative users on the 本教程仅介绍在admin database only. For the user authentication, the tutorial uses the default SCRAM authentication mechanism. admin数据库上创建最小数量的管理用户。对于用户身份验证,本教程使用默认的SCRAM身份验证机制。Challenge-response security mechanisms are best suited for testing or development environments. 挑战响应安全机制最适合测试或开发环境。For production environments, we recommend using X.509 certificates or Self-Managed LDAP Proxy Authentication (available for MongoDB Enterprise only) or Kerberos Authentication on Self-Managed Deployments (available for MongoDB Enterprise only).对于生产环境,我们建议在自我管理部署上使用X.509证书或自我管理LDAP代理身份验证(仅适用于MongoDB Enterprise)或Kerberos身份验证(仅供MongoDB Enterprise)。
For details on creating users for specific authentication mechanism, refer to the specific authentication mechanism pages.有关为特定身份验证机制创建用户的详细信息,请参阅特定的身份验证机制页面。
See ➤ Configure Role-Based Access Control for best practices for user creation and management.有关用户创建和管理的最佳实践,请参阅➤ 配置基于角色的访问控制。
Users用户
In general, to create users for a sharded clusters, connect to the 一般来说,要为分片集群创建用户,请连接到mongos and add the sharded cluster users.mongos并添加分片集群用户。
However, some maintenance operations 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.然而,一些维护操作需要直接连接到分片集群中的特定分片。要执行这些操作,您必须直接连接到分片,并作为分片本地管理用户进行身份验证。
Shard-local users exist only in the specific shard and should only be used for shard-specific maintenance and configuration. You cannot connect to the 分片本地用户只存在于特定的分片中,只应用于分片特定的维护和配置。您无法使用分片本地用户连接到mongos with shard-local users.mongos。
This tutorial requires creating sharded cluster users, but includes optional steps for adding shard-local users.本教程要求创建分片集群用户,但包括添加分片本地用户的可选步骤。
See the Users in Self-Managed Deployments security documentation for more information.有关更多信息,请参阅自我管理部署中的用户安全文档。
Operating System操作系统
This tutorial uses the 本教程使用mongod and mongos programs. mongod和mongos程序。Windows users should use the Windows用户应该使用mongod.exe and mongos.exe programs instead.mongod.exe和mongos.exe程序。
Before You Begin开始之前
Starting in MongoDB 8.0, you can use the 从MongoDB 8.0开始,您可以使用directShardOperations role to perform maintenance operations that require you to execute commands directly against a shard.directShardOperations角色执行维护操作,这些操作要求您直接对分片执行命令。
Warning
Running commands using the 使用directShardOperations role can cause your cluster to stop working correctly and may cause data corruption. directShardOperations角色运行命令可能会导致集群停止正常工作,并可能导致数据损坏。Only use the 仅将directShardOperations role for maintenance purposes or under the guidance of MongoDB support. directShardOperations角色用于维护目的或在MongoDB支持的指导下使用。Once you are done performing maintenance operations, stop using the 完成维护操作后,停止使用directShardOperations role.directShardOperations角色。
Deploy Sharded Cluster with Keyfile Access Control部署具有键文件访问控制的分片集群
The following procedures involve creating a new sharded cluster that consists of a 以下过程涉及创建一个新的分片集群,该集群由一个mongos, the config servers, and two shards.mongos、配置服务器和两个分片组成。
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.为避免因IP地址更改而进行配置更新,请使用DNS主机名而不是IP地址。在配置副本集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address fail startup validation and do not start.使用主机名而不是IP地址来配置跨拆分网络范围的集群。从MongoDB 5.0开始,仅配置了IP地址的节点无法启动验证,也无法启动。
Create the Keyfile创建键文件
With keyfile authentication, each 通过键文件身份验证,分片集群中的每个mongod or mongos instances in the sharded cluster uses the contents of the keyfile as the shared password for authenticating other members in the deployment. Only mongod or mongos instances with the correct keyfile can join the sharded cluster.mongod或mongos实例都使用键文件的内容作为共享密码,用于对部署中的其他成员进行身份验证。只有具有正确键文件的mongod或mongos实例才能加入分片集群。
Note
Keyfiles for internal membership authentication用于内部成员身份验证的键文件 use YAML format to allow for multiple keys in a keyfile. The YAML format accepts either:使用YAML格式允许键文件中有多个键。YAML格式接受以下任一格式:
A single key string (same as in earlier versions)单个键字符串(与早期版本相同)A sequence of key strings一串键串
The YAML format is compatible with the existing single-key keyfiles that use the text file format.YAML格式与使用文本文件格式的现有单键键文件兼容。
A key's length must be between 6 and 1024 characters and may only contain characters in the base64 set. All members of the sharded cluster must share at least one common key.键的长度必须在6到1024个字符之间,并且只能包含base64集合中的字符。分片集群的所有成员必须至少共享一个公共键。
Note
On UNIX systems, the keyfile must not have group or world permissions. On Windows systems, keyfile permissions are not checked.在UNIX系统上,键文件不得具有组或世界权限。在Windows系统上,不检查键文件权限。
You can generate a keyfile using any method you choose. For example, the following operation uses 您可以使用您选择的任何方法生成键文件。例如,以下操作使用openssl生成一个复杂的伪随机1024字符串,用作共享密码。然后,它使用chmod更改文件权限,仅为文件所有者提供读取权限:openssl to generate a complex pseudo-random 1024 character string to use as a shared password. It then uses chmod to change file permissions to provide read permissions for the file owner only:
openssl rand -base64 756 > <path-to-keyfile>
chmod 400 <path-to-keyfile>
See Keyfiles for additional details and requirements for using keyfiles.有关使用键文件的其他详细信息和要求,请参阅键文件。
Distribute the Keyfile分发键文件
Copy the keyfile to each server hosting the sharded cluster members. 将键文件复制到托管分片集群成员的每个服务器。Ensure that the user running the 确保运行mongod or mongos instances is the owner of the file and can access the keyfile.mongod或mongos实例的用户是文件的所有者,并且可以访问键文件。
Avoid storing the keyfile on storage mediums that can be easily disconnected from the hardware hosting the 避免将键文件存储在容易与托管mongod or mongos instances, such as a USB drive or a network attached storage device.mongod或mongos实例的硬件断开连接的存储介质上,例如USB驱动器或网络连接的存储设备。
Create the Config Server Replica Set创建配置服务器副本集
The following steps deploys a config server replica set.以下步骤部署配置服务器副本集。
For a production deployment, deploys a config server replica set with at least three members. For testing purposes, you can create a single-member replica set.对于生产部署,部署一个至少有三个成员的配置服务器副本集。出于测试目的,您可以创建单个成员副本集。
Start each 启动配置服务器副本集中的每个mongod in the config server replica set. Include the keyFile setting. mongod。包括keyFile设置。The keyFile setting enforces both Self-Managed Internal/Membership Authentication and Role-Based Access Control in Self-Managed Deployments.keyFile设置在自我管理部署中强制执行自我管理内部/成员身份验证和基于角色的访问控制。
You can specify the 您可以通过配置文件或命令行指定mongod settings either via a configuration file or the command line.mongod设置。
Configuration File配置文件
If using a configuration file, set 如果使用配置文件,请将security.keyFile to the keyfile's path, sharding.clusterRole to configsvr, and replication.replSetName to the desired name of the config server replica set.security.keyFile设置为键文件的路径,将sharding.clusterRole设置为configsvr,并将replication.replSetName设置为配置服务器副本集的所需名称。
security:
keyFile: <path-to-keyfile>
sharding:
clusterRole: configsvr
replication:
replSetName: <setname>
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定net.bindIp setting.net.bindIp设置。
Start the 启动mongod specifying the --config option and the path to the configuration file.mongod,指定--config选项和配置文件的路径。
mongod --config <path-to-config-file>
Command Line
If using the command line parameters, start the 如果使用命令行参数,请使用mongod with the --keyFile, --configsvr, and --replSet parameters.--keyFile、--configsvr和--replSet参数启动mongod。
mongod --keyFile <path-to-keyfile> --configsvr --replSet <setname> --dbpath <path>
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ip.--bind_ip。
Connect to a member of the replica set over the localhost interface.通过localhost接口连接到副本集的成员。
Connect 通过localhost接口将mongosh to one of the mongod instances over the localhost interface. mongosh连接到mongod实例之一。You must run 您必须在与mongosh on the same physical machine as the mongod instance.mongod实例相同的物理机器上运行mongosh。
The localhost interface is only available since no users have been created for the deployment. The localhost interface closes after the creation of the first user.由于没有为部署创建用户,因此只能使用localhost接口。创建第一个用户后,localhost接口关闭。
The rs.initiate() method initiates the replica set and can take an optional replica set configuration document. rs.initiate()方法启动副本集,并可以获取可选的副本集配置文档。In the replica set configuration document, include:在副本集配置文档中,包括:
The_id._id。The_idmust match the必须与传递给--replSetparameter passed to themongod.mongod的--replSet参数匹配。Themembersfield. Themembersfield is an array and requires a document per each member of the replica set.members字段。members字段是一个数组,每个副本集的成员都需要一个文档。Theconfigsvrfield. Theconfigsvrfield must be set totruefor the config server replica set.configsvr字段。对于配置服务器副本集,configsvr字段必须设置为true。
See Self-Managed Replica Set Configuration for more information on replica set configuration documents.有关副本集配置文档的更多信息,请参阅自我管理副本集配置。
Initiate the replica set using the 使用rs.initiate() method and a configuration document:rs.initiate()方法和配置文档启动副本集:
rs.initiate(
{
_id: "myReplSet",
configsvr: true,
members: [
{ _id : 0, host : "cfg1.example.net:27019" },
{ _id : 1, host : "cfg2.example.net:27019" },
{ _id : 2, host : "cfg3.example.net:27019" }
]
}
)Once the config server replica set (CSRS) is initiated and up, proceed to creating the shard replica sets.启动并启动配置服务器副本集(CSRS)后,继续创建分片副本集。
Create the Shard Replica Sets创建分片副本集
For a production deployment, use a replica set with at least three members. For testing purposes, you can create a single-member replica set.对于生产部署,使用至少有三个成员的副本集。出于测试目的,您可以创建单个成员副本集。
These steps include optional procedures for adding shard-local users. Executing them now ensures that there are users available for each shard to perform shard-level maintenance.这些步骤包括添加分片本地用户的可选过程。现在执行它们可以确保每个分片都有用户可以执行分片级维护。
Start each member of the replica set with access control enabled.在启用访问控制的情况下启动副本集的每个成员。
Running a 使用mongod with the keyFile parameter enforces both Self-Managed Internal/Membership Authentication and Role-Based Access Control in Self-Managed Deployments.keyFile参数运行mongod会在自我管理部署中强制执行自我管理内部/成员身份验证和基于角色的访问控制。
Start each 使用配置文件或命令行启动副本集中的每个mongod in the replica set using either a configuration file or the command line.mongod。
Configuration File配置文件
If using a configuration file, set the 如果使用配置文件,请将security.keyFile option to the keyfile's path, the replication.replSetName to the desired name of the replica set, and the sharding.clusterRole option to shardsvr.security.keyFile选项设置为键文件的路径,将replication.replSetName设置为副本集的所需名称,将sharding.clusterRole选项设置为shardsvr。
security:
keyFile: <path-to-keyfile>
sharding:
clusterRole: shardsvr
replication:
replSetName: <replSetName>
storage:
dbPath: <path>
Include additional options as required for your configuration. 根据配置要求,包括其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定net.bindIp setting.net.bindIp设置。
Start the 启动mongod specifying the --config option and the path to the configuration file.mongod,指定--config选项和配置文件的路径。
mongod --config <path-to-config-file>
Command Line
If using the command line option, when starting the component, specify the 如果使用命令行选项,在启动组件时,请指定--keyFile, replSet, and --shardsvr parameters, as in the following example:--keyFile、replSet和--shardsvr参数,如下例所示:
mongod --keyFile <path-to-keyfile> --shardsvr --replSet <replSetName> --dbpath <path>
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ip.--bind_ip。
Connect to a member of the replica set over the localhost interface.通过localhost接口连接到副本集的成员。
Connect 通过localhost接口将mongosh to one of the mongod instances over the localhost interface. mongosh连接到mongod实例之一。You must run 您必须在与mongosh on the same physical machine as the mongod instance.mongod实例相同的物理机器上运行mongosh。
The localhost interface is only available since no users have been created for the deployment. 由于没有为部署创建用户,因此只能使用localhost接口。The localhost interface closes after the creation of the first user.创建第一个用户后,localhost接口关闭。
Initiate the replica set.启动副本集。
From 从mongosh, run the rs.initiate() method.mongosh运行rs.initiate()方法。
rs.initiate() can take an optional replica set configuration document. 可以采用可选的副本集配置文档。In the replica set configuration document, include:在副本集配置文档中,包括:
The_idfield set to the replica set name specified in either thereplication.replSetNameor the--replSetoption._id字段设置为replication.replSetName或--replSet选项中指定的副本集名称。Themembersarray with a document per each member of the replica set.members数组,每个副本集的成员都有一个文档。
The following example initates a three member replica set.以下示例初始化一个由三个成员组成的副本集。
rs.initiate(
{
_id : "myReplSet",
members: [
{ _id : 0, host : "s1-mongo1.example.net:27018" },
{ _id : 1, host : "s1-mongo2.example.net:27018" },
{ _id : 2, host : "s1-mongo3.example.net:27018" }
]
}
)
rs.initiate() triggers an election and elects one of the members to be the primary.触发选举,并选举其中一名成员为primary。
Connect to the primary before continuing. Use 请先连接到主服务器,然后再继续。使用rs.status() to locate the primary member.rs.status()查找主要成员。
Create the shard-local user administrator (optional).创建分片本地用户管理员(可选)。
Important
After you create the first user, the localhost exception is no longer available.创建第一个用户后,localhost异常不再可用。
The first user must have privileges to create other users, such as a user with the 第一个用户必须具有创建其他用户的权限,例如具有userAdminAnyDatabase. userAdminAnyDatabase的用户。This ensures that you can create additional users after the Localhost Exception in Self-Managed Deployments closes.这可确保您可以在自我管理部署中的Localhost异常关闭后创建其他用户。
If at least one user does not have privileges to create users, once the localhost exception closes you may be unable to create or modify users with new privileges, and therefore unable to access necessary operations.
Add a user using the 使用db.createUser() method. db.createUser()方法添加用户。The user should have at minimum the 用户在userAdminAnyDatabase role on the admin database.admin数据库上至少应具有userAdminAnyDatabase角色。
You must be connected to the primary to create users.您必须连接到primary才能创建用户。
The following example creates the user 以下示例在fred with the userAdminAnyDatabase role on the admin database.admin数据库上创建了具有userAdminAnyDatabase角色的用户fred。
Important
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应该是随机的、长的和复杂的,以确保系统安全,防止或延迟恶意访问。
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "fred",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Enter the password when prompted. 出现提示时输入密码。See Database User Roles for a full list of built-in roles related to database administration operations.有关与数据库管理操作相关的内置角色的完整列表,请参阅数据库用户角色。
Authenticate as the shard-local user administrator (optional).以分片本地用户管理员身份进行身份验证(可选)。
Authenticate to the 对admin database.admin数据库进行身份验证。
In 在mongosh, use db.auth() to authenticate. mongosh中,使用db.auth()进行身份验证。For example, the following authenticate as the user administrator 例如,以下人员以用户管理员fred:fred身份进行身份验证:
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
db.getSiblingDB("admin").auth("fred", passwordPrompt()) // or cleartext password
Alternatively, connect a new 或者,使用mongosh instance to the primary replica set member using the -u <username>, -p <password>, and the --authenticationDatabase parameters.-u <username>、-p <password>和--authenticationDatabase参数将新的mongosh实例连接到主副本集成员。
mongosh -u "fred" -p --authenticationDatabase "admin"
If you do not specify the password to the 如果不在-p command-line option, mongosh prompts for the password.-p命令行选项中指定密码,mongosh会提示输入密码。
Create the shard-local cluster administrator (optional).创建分片本地集群管理员(可选)。
The shard-local cluster administrator user has the 分片本地集群管理员用户具有clusterAdmin role, which provides privileges that allow access to replication operations.clusterAdmin角色,该角色提供允许访问复制操作的权限。
For a full list of roles related to replica set operations see Cluster Administration Roles.有关与副本集操作相关的角色的完整列表,请参阅群集管理角色。
Create a cluster administrator user and assign the 创建一个集群管理员用户,并在clusterAdmin role in the admin database:admin数据库中分配clusterAdmin角色:
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
db.getSiblingDB("admin").createUser(
{
"user" : "ravi",
"pwd" : passwordPrompt(), // or cleartext password
roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
}
)
Enter the password when prompted.出现提示时输入密码。
See Cluster Administration Roles for a full list of built-in roles related to replica set and sharded cluster operations.有关与副本集和分片群集操作相关的内置角色的完整列表,请参阅群集管理角色。
Connect a mongos to the Sharded Cluster将mongos连接到分片集群
mongos to the Sharded ClusterConnect a mongos to the cluster将mongos连接到集群
mongos to the clusterStart a 启动mongos specifying the keyfile using either a configuration file or a command line parameter.mongos,使用配置文件或命令行参数指定键文件。
Configuration File配置文件
If using a configuration file, set the 如果使用配置文件,请将security.keyFile to the keyfile's path and the sharding.configDB to the replica set name and at least one member of the replica set in <replSetName>/<host:port> format.security.keyFile设置为键文件的路径,将sharding.configDB设置为副本集名称,并以<replSetName>/<host:port>格式设置副本集的至少一个成员。
security:
keyFile: <path-to-keyfile>
sharding:
configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,...
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定net.bindIp setting.net.bindIp设置。
Start the 启动mongos specifying the --config option and the path to the configuration file.mongos,指定--config选项和配置文件的路径。
mongos --config <path-to-config>
Command Line命令行
If using command line parameters start the 如果使用命令行参数,请启动mongos and specify the --keyFile and --configdb parameters.mongos并指定--keyFile和--configdb参数。
mongos --keyFile <path-to-keyfile> --configdb <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,...
Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the 根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ip.--bind_ip。
Connect to a mongos over the localhost interface.通过localhost接口连接到mongos。
mongos over the localhost interface.Connect 通过localhost接口将mongosh to one of the mongos instances over the localhost interface. mongosh连接到mongos实例之一。You must run 您必须在与mongosh on the same physical machine as the mongos instance.mongos实例相同的物理机器上运行mongosh。
The localhost interface is only available since no users have been created for the deployment. The localhost interface closes after the creation of the first user.由于没有为部署创建用户,因此只能使用localhost接口。创建第一个用户后,localhost接口关闭。
Create the user administrator.创建用户管理员。
Important
After you create the first user, the localhost exception is no longer available.创建第一个用户后,localhost异常不再可用。
The first user must have privileges to create other users, such as a user with the 第一个用户必须具有创建其他用户的权限,例如具有userAdminAnyDatabase. userAdminAnyDatabase的用户。This ensures that you can create additional users after the Localhost Exception in Self-Managed Deployments closes.这可确保您可以在自我管理部署中的Localhost异常关闭后创建其他用户。
If at least one user does not have privileges to create users, once the localhost exception closes you cannot create or modify users, and therefore may be unable to perform necessary operations.如果至少有一个用户没有创建用户的权限,则一旦localhost异常关闭,您就无法创建或修改用户,因此可能无法执行必要的操作。
Add a user using the 使用db.createUser() method. db.createUser()方法添加用户。The user should have at minimum the 用户在userAdminAnyDatabase role on the admin database.admin数据库上至少应具有userAdminAnyDatabase角色。
Important
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应该是随机的、长的和复杂的,以确保系统安全,防止或延迟恶意访问。
The following example creates the user 以下示例在fred on the admin database:admin数据库上创建用户fred:
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "fred",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
See Database User Roles for a full list of built-in roles and related to database administration operations.有关内置角色和与数据库管理操作相关的完整列表,请参阅数据库用户角色。
Authenticate as the user administrator.以用户管理员身份进行身份验证。
Use 使用db.auth() to authenticate as the user administrator to create additional users:db.auth()以用户管理员身份进行身份验证,以创建其他用户:
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
db.getSiblingDB("admin").auth("fred", passwordPrompt()) // or cleartext password
Enter the password when prompted.出现提示时输入密码。
Alternatively, connect a new 或者,使用mongosh session to the target replica set member using the -u <username>, -p <password>, and the --authenticationDatabase "admin" parameters. -u <username>、-p <password>和--authenticationDatabase "admin"参数将新的mongosh会话连接到目标副本集成员。You must use the Localhost Exception in Self-Managed Deployments to connect to the 您必须在自我管理部署中使用Localhost异常来连接到mongos.mongos。
mongosh -u "fred" -p --authenticationDatabase "admin"
If you do not specify the password to the 如果不在-p command-line option, mongosh prompts for the password.-p命令行选项中指定密码,mongosh会提示输入密码。
Create Administrative User for Cluster Management创建群集管理的管理用户
The cluster administrator user has the 集群管理员用户具有clusterAdmin role, which grants access to replication and sharding operations.clusterAdmin角色,该角色授予对复制和分片操作的访问权限。
Create a 在clusterAdmin user in the admin database.admin数据库中创建clusterAdmin用户。
The following example creates the user 以下示例在ravi on the admin database.admin数据库上创建用户ravi。
Important
Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应该是随机的、长的和复杂的,以确保系统安全,防止或延迟恶意访问。
Tip
You can use the 您可以将passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo shell.mongo shell一样直接指定密码。
db.getSiblingDB("admin").createUser(
{
"user" : "ravi",
"pwd" : passwordPrompt(), // or cleartext password
roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
}
)
See Cluster Administration Roles for a full list of built-in roles related to replica set and sharded cluster operations.有关与副本集和分片群集操作相关的内置角色的完整列表,请参阅群集管理角色。
Create additional users (Optional).创建其他用户(可选)。
Create users to allow clients to connect and access the sharded cluster. 创建用户以允许客户端连接和访问分片集群。See Database User Roles for available built-in roles, such as 有关可用的内置角色(如read and readWrite. read和readWrite),请参阅数据库用户角色。You may also want additional administrative users. For more information on users, see Users in Self-Managed Deployments.您可能还需要其他管理用户。有关用户的更多信息,请参阅自我管理部署中的用户。
To create additional users, you must authenticate as a user with 要创建其他用户,您必须以userAdminAnyDatabase or userAdmin roles.userAdminAnyDatabase或userAdmin角色的用户身份进行身份验证。
Add Shards to the Cluster将分片添加到集群
To proceed, you must be connected to the 要继续,您必须连接到mongos and authenticated as the cluster administrator user for the sharded cluster.mongos,并作为分片集群的集群管理员用户进行身份验证。
Note
This is the cluster administrator for the sharded cluster and not the shard-local cluster administrator.这是分片集群的集群管理员,而不是分片本地集群管理员。
To add each shard to the cluster, use the 要将每个分片添加到集群中,请使用sh.addShard() method. sh.addShard()方法。If the shard is a replica set, specify the name of the replica set and specify a member of the set. In production deployments, all shards should be replica sets.如果分片是副本集,请指定副本集的名称并指定该集的一个成员。在生产部署中,所有分片都应该是副本集。
The following operation adds a single shard replica set to the cluster:以下操作将单个分片副本集添加到集群中:
sh.addShard( "<replSetName>/s1-mongo1.example.net:27017")
The following operation is an example of adding a standalone 以下操作是向集群添加独立mongod shard to the cluster:mongod分片的示例:
sh.addShard( "s1-mongo1.example.net:27017")
Repeat these steps until the cluster includes all shards. At this point, the sharded cluster enforces access control for the cluster as well as for internal communications between each sharded cluster component.重复这些步骤,直到集群包含所有分片。此时,分片集群对集群以及每个分片集群组件之间的内部通信实施访问控制。
Shard a Collection分片集合
To proceed, you must be connected to the 要继续,您必须连接到mongos and authenticated as the cluster administrator user for the sharded cluster.mongos,并作为分片集群的集群管理员用户进行身份验证。
Note
This is the cluster administrator for the sharded cluster and not the shard-local cluster administrator.这是分片集群的集群管理员,而不是分片本地集群管理员。
To shard a collection, use the 要对集合进行分片,请使用sh.shardCollection() method. You must specify the full namespace of the collection and a document containing the shard key.sh.shardCollection()方法。您必须指定集合的完整命名空间和包含分片键的文档。
Your selection of shard key affects the efficiency of sharding, as well as your ability to take advantage of certain sharding features such as zones. 您对分片键的选择会影响分片的效率,以及您利用某些分片功能(如区域)的能力。See the selection considerations listed in the Choose a Shard Key.请参阅选择分片键中列出的选择注意事项。
If the collection already contains data, you must create an index on the shard key using the 如果集合已经包含数据,则必须在使用db.collection.createIndex() method before using shardCollection().shardCollection()之前使用db.collection.createIndex()方法在分片键上创建索引。
If the collection is empty, MongoDB creates the index as part of 如果集合为空,MongoDB将创建索引作为sh.shardCollection().sh.shardCollection()的一部分。
The following is an example of the 以下是sh.shardCollection() method:sh.shardCollection()方法的示例:
sh.shardCollection("<database>.<collection>", { <key> : <direction> } )Next Steps后续步骤
Create users to allow clients to connect to and interact with the sharded cluster.创建用户以允许客户端连接到分片集群并与之交互。
See Database User Roles for basic built-in roles to use in creating read-only and read-write users.有关创建只读和读写用户时使用的基本内置角色,请参阅数据库用户角色。
X.509 Internal AuthenticationX.509内部身份验证
For details on using X.509 for internal authentication, see Use X.509 Certificates for Membership Authentication with Self-Managed MongoDB.有关使用X.509进行内部身份验证的详细信息,请参阅使用X.509证书进行自我管理MongoDB的成员身份验证。
To upgrade from keyfile internal authentication to X.509 internal authentication, see Upgrade Self-Managed MongoDB from Keyfile Authentication to X.509 Authentication.要从键文件内部身份验证升级到X.509内部身份验证,请参阅将自管理MongoDB从键文件身份验证升级为X.509身份验证。