Deploy a Sharded Cluster部署分片群集
On this page本页内容
Overview概述
This tutorial involves creating a new sharded cluster that consists of a 本教程涉及创建一个新的分片集群,该集群由一个mongos
, the config server replica set, and two shard replica sets.mongos
、配置服务器副本集和两个分片副本集组成。
Considerations注意事项
Connectivity连通性
Each member of a sharded cluster must be able to connect to all other members in the cluster. 分片集群的每个成员都必须能够连接到集群中的所有其他成员。This includes all shards and config servers. Ensure that network and security systems, including all interface and firewalls, allow these connections.这包括所有的分片和配置服务器。确保网络和安全系统,包括所有接口和防火墙,允许这些连接。
Hostnames and Configuration主机名和配置
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. 使用主机名而不是IP地址来跨拆分的网络范围配置群集。Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongoDB 5.0开始,只配置了IP地址的节点将无法通过启动验证,也不会启动。
Localhost Deployments本地主机部署
If you use either 如果使用localhost
or its IP address as the hostname portion of any host identifier, you must use that identifier as the host setting for any other MongoDB component in the cluster.localhost
或其IP地址作为任何主机标识符的主机名部分,则必须使用该标识符作为集群中任何其他MongoDB组件的主机设置。
For example, the 例如,sh.addShard()
method takes a host
parameter for the hostname of the target shard. sh.addShard()
方法为目标分片的主机名取一个host
参数。If you set 如果将host
to localhost
, you must then use localhost
as the host for all other shards in the cluster.host
设置为localhost
,则必须使用localhost
作为集群中所有其他分片的主机。
Security安全
This tutorial does not include the required steps for configuring Internal/Membership Authentication or Role-Based Access Control.本教程不包括配置内部/成员身份验证或基于角色的访问控制所需的步骤。
In production environments, sharded clusters should employ at minimum x.509 security for internal authentication and client access.在生产环境中,分片集群应至少使用x.509安全性进行内部身份验证和客户端访问。
Procedure过程
Create the Config Server Replica Set创建配置服务器副本集
The following steps deploys a config server replica set.以下步骤部署配置服务器副本集。
For a production deployment, deploy a config server replica set with at least three members. 对于生产部署,请部署至少有三个成员的配置服务器副本集。For testing purposes, you can create a single-member replica set.出于测试目的,您可以创建单个成员复制副本集。
The config server replica set must not use the same name as any of the shard replica sets.配置服务器副本集不能使用与任何分片副本集相同的名称。
For this tutorial, the config server replica set members are associated with the following hosts:在本教程中,配置服务器副本集成员与以下主机关联:
Member 0 | cfg1.example.net |
Member 1 | cfg2.example.net |
Member 2 | cfg3.example.net |
Start each member of the config server replica set.启动配置服务器副本集的每个成员。
When starting each 启动每个mongod
, specify the mongod
settings either via a configuration file or the command line.mongod
时,通过配置文件或命令行指定mongod
设置。
If using a configuration file, set:如果使用配置文件,请设置:
sharding:
clusterRole: configsvr
replication:
replSetName: <replica set name>
net:
bindIp: localhost,<hostname(s)|ip address(es)>
-
sharding.clusterRole
to为configsvr
, -
replication.replSetName
to the desired name of the config server replica set,为配置服务器副本集的期望名称, -
net.bindIp
option to the hostname/ip address or comma-delimited list of hostnames or ip addresses that remote clients (including the other members of the config server replica set as well as other members of the sharded cluster) can use to connect to the instance.net.bindIp
选项指定远程客户端(包括配置服务器副本集的其他成员以及分片集群的其他成员)可以用来连接到实例的主机名/ip地址或主机名或ip地址的逗号分隔列表。WarningBefore you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist.在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。 -
Additional settings as appropriate to your deployment, such as适用于部署的其他设置,如storage.dbPath
andnet.port
.storage.dbPath
和net.port
。For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
Start the 启动mongod
with the --config
option set to the configuration file path.mongod
时,将--config
选项设置为配置文件路径。
mongod --config <path-to-config-file>
If using the command line options, start the 如果使用命令行选项,请使用mongod
with the --configsvr
, --replSet
, --bind_ip
, and other options as appropriate to your deployment. --configsvr
、--replSet
、--bind_ip
和其他适合您的部署的选项启动mongod
。For example:例如:
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
mongod --configsvr --replSet <replica set name> --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)>
For more information on startup parameters, see the 有关启动参数的更多信息,请参阅mongod
reference page.mongod
参考页面。
Connect to one of the config servers.连接到其中一个配置服务器。
Connect 将mongosh
to one of the config server members.mongosh
连接到其中一个配置服务器成员。
mongosh --host <hostname> --port <port>
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_id
set to the replica set name specified in either thereplication.replSetName
or the--replSet
option._id
设置为replication.replSetName
或--replSet
选项中指定的副本集名称。The配置服务器副本集的configsvr
field set totrue
for the config server replica set.configsvr
字段设置为true
。Themembers
array with a document per each member of the replica set.members
数组,副本集中的每个成员都有一个文档。
Run 仅对副本集的一个且仅对一个rs.initiate()
on just one and only one mongod
instance for the replica set.mongod
实例运行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" }
]
}
)
See Replica Set Configuration for more information on replica set configuration documents.有关副本集配置文档的详细信息,请参阅副本集配置。
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.出于测试目的,您可以创建单个成员复制副本集。
Shard replica sets must not use the same name as the config server replica set.分片副本集不能使用与配置服务器副本集相同的名称。
For each shard, use the following steps to create the shard replica set:对于每个分片,请使用以下步骤创建分片副本集:
Start each member of the shard replica set.启动分片复制集的每个成员。
When starting each 启动每个mongod
, specify the mongod
settings either via a configuration file or the command line.mongod
时,通过配置文件或命令行指定mongod
设置。
If using a configuration file, set:如果使用配置文件,请设置:
sharding:
clusterRole: shardsvr
replication:
replSetName: <replSetName>
net:
bindIp: localhost,<ip address>
-
replication.replSetName
to the desired name of the replica set,为复制集的期望名称, -
sharding.clusterRole
option to选项为shardsvr
,shardsvr
, -
net.bindIp
option to the ip or a comma-delimited list of ips that remote clients (including the other members of the config server replica set as well as other members of the sharded cluster) can use to connect to the instance.选项为远程客户端(包括配置服务器副本集的其他成员以及分片集群的其他成员)的IP或逗号分隔的IP列表,可以用来连接到实例。WarningBefore you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access.在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.有关安全建议的完整列表,请参阅安全检查表。至少,考虑启用身份验证和强化网络基础设施。 -
Additional settings as appropriate to your deployment, such as适用于部署的其他设置,如storage.dbPath
andnet.port
.storage.dbPath
和net.port
。For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
Start the 启动mongod
with the --config
option set to the configuration file path.mongod
时,将--config
选项设置为配置文件路径。
mongod --config <path-to-config-file>
If using the command line option, start the 如果使用命令行选项,请使用mongod
with the --replSet
, and --shardsvr
, --bind_ip
options, and other options as appropriate to your deployment. --replSet
、--shardsvr
、--bind_ip
选项以及其他适合您的部署的选项启动mongod
。For example:例如:
mongod --shardsvr --replSet <replSetname> --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)>
For more information on startup parameters, see the 有关启动参数的更多信息,请参阅mongod
reference page.mongod
参考页面。
Connect to one member of the shard replica set.连接到分片复制集的一个成员。
Connect 将mongosh
to one of the replica set members.mongosh
连接到其中一个复制集成员。
mongosh --host <hostname> --port <port>
Initiate the replica set.启动复制副本集。
From 在mongosh
, run the rs.initiate()
method.mongosh
中,运行rs.initiate()
方法。
rs.initiate()
can take an optional replica set configuration document. rs.initiate()
可以采用可选的副本集配置文档。In the replica set configuration document, include:在复制副本集配置文档中,包括:
The_id
field set to the replica set name specified in either thereplication.replSetName
or the--replSet
option._id
字段设置为replication.replSetName
或--replSet
选项中指定的副本集名称。Themembers
array with a document per each member of the replica set.members
数组,副本集中的每个成员都有一个文档。
The following example initiates a three member replica set.以下示例启动一个由三个成员组成的复制副本集。
Run 仅对副本集的一个且仅对一个rs.initiate()
on just one and only one mongod
instance for the replica set.mongod
实例运行rs.initiate()
。
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" }
]
}
)
Start a mongos
for the Sharded Cluster为分片集群启动mongos
mongos
for the Sharded ClusterStart a 使用配置文件或命令行参数启动mongos
using either a configuration file or a command line parameter to specify the config servers.mongos
以指定配置服务器。
If using a configuration file, set the 如果使用配置文件,请以sharding.configDB
to the config server replica set name and at least one member of the replica set in <replSetName>/<host:port>
format.sharding.configDB
格式将sharding.configDB
设置为配置服务器副本集名称和副本集的至少一个成员。
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
sharding:
configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019
net:
bindIp: localhost,<hostname(s)|ip address(es)>
Start the 启动mongos
specifying the --config
option and the path to the configuration file.mongos
,指定--config
选项和配置文件的路径。
mongos --config <path-to-config>
For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
If using command line parameters start the 如果使用命令行参数启动mongos
and specify the --configdb
, --bind_ip
, and other options as appropriate to your deployment. mongos
,并根据部署情况指定--configdb
、--bind_ip
和其他选项。For example:例如:
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
mongos --configdb <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,cfg3.example.net:27019 --bind_ip localhost,<hostname(s)|ip address(es)>
Include any other options as appropriate for your deployment.包括适用于您的部署的任何其他选项。
At this point, your sharded cluster consists of the 此时,您的分片集群由mongos
and the config servers. mongos
和config
服务器组成。You can now connect to the sharded cluster using 现在,您可以使用mongosh
.mongosh
连接到分片集群。
Connect to the Sharded Cluster连接到分片群集
Connect 将mongosh
to the mongos
. Specify the host
and port
on which the mongos
is running:mongosh
连接到mongos
。指定运行mongos
的host
和port
:
mongosh --host <hostname> --port <port>
Once you have connected 将mongosh
to the mongos
, continue to the next procedure to add shards to the cluster.mongosh
连接到mongos
后,继续执行下一个过程,将分片添加到集群中。
Add Shards to the Cluster将分片添加到群集
In a 在连接到mongosh
session that is connected to the mongos
, use the sh.addShard()
method to add each shard to the cluster.mongos
的mongosh
会话中,使用sh.addShard()
方法将每个分片添加到集群中。
The following operation adds a single shard replica set to the cluster:以下操作将单个分片副本集添加到集群中:
sh.addShard( "<replSetName>/s1-mongo1.example.net:27018,s1-mongo2.example.net:27018,s1-mongo3.example.net:27018")
Repeat these steps until the cluster includes all desired shards.重复这些步骤,直到群集包含所有所需的分片。
Shard a Collection对集合进行分片
To shard a collection, connect 要分割集合,请将mongosh
to the mongos
and use the sh.shardCollection()
method.mongosh
连接到mongos
并使用sh.shardCollection()
方法。
Sharding and Indexes分片和索引
If the collection already contains data, you must create an index that supports the shard key before sharding the collection. 如果集合已经包含数据,则在对集合进行分片之前,必须创建一个支持分片键的索引。If the collection is empty, MongoDB creates the index as part of 如果集合为空,MongoDB将创建索引作为sh.shardCollection()
.sh.shardCollection()
的一部分。
MongoDB provides two strategies to shard collections:MongoDB为分片集合提供了两种策略:
Hashed sharding uses a hashed index of a single field as the shard key to partition data across your sharded cluster.哈希分片使用单个字段的哈希索引作为分片键,在分片集群中对数据进行分区。sh.shardCollection("<database>.<collection>", { <shard key field> : "hashed" } )
Range-based sharding can use multiple fields as the shard key and divides data into contiguous ranges determined by the shard key values.基于范围的分片可以使用多个字段作为分片键,并将数据划分为由分片键值确定的连续范围。sh.shardCollection("<database>.<collection>", { <shard key field> : 1, ... } )
Shard Key Considerations分片关键注意事项
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. 您对分片键的选择会影响分片的效率,以及您利用某些分片功能(如区域)的能力。To learn how to choose an effective shard key, see Choose a Shard Key.要了解如何选择有效的分片键,请参阅选择分片键。
Starting in version 4.0, 从4.0版本开始,mongosh
provides the method convertShardKeyToHashed()
. mongosh
提供了convertShardKeyToHashed()
方法。This method uses the same hashing function as the hashed index and can be used to see what the hashed value would be for a key.此方法使用与哈希索引相同的哈希函数,可以用来查看键的哈希值。
See also: 另请参阅:
For hashed sharding shard keys, see Hashed Sharding Shard Key有关哈希分片分片键,请参阅哈希分片分片分片键For ranged sharding shard keys, see Shard Key Selection有关范围内的分片分片关键帧,请参阅分片关键帧选择