Docs HomeMongoDB Manual

Deploy a Sharded Cluster部署分片群集

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主机名和配置

Important

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.出于测试目的,您可以创建单个成员复制副本集。

Note

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:在本教程中,配置服务器副本集成员与以下主机关联:

Config Server Replica Set Member配置服务器副本集成员Hostname主机名
Member 0cfg1.example.net
Member 1cfg2.example.net
Member 2cfg3.example.net
1

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地址的逗号分隔列表。

    Warning

    Before 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 and net.port. 适用于部署的其他设置,如storage.dbPathnet.portFor 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和其他适合您的部署的选项启动mongodFor example:例如:

Warning

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参考页面。

2

Connect to one of the config servers.连接到其中一个配置服务器。

Connect mongosh to one of the config server members.mongosh连接到其中一个配置服务器成员。

mongosh --host <hostname> --port <port>
3

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 the replication.replSetName or the --replSet option._id设置为replication.replSetName--replSet选项中指定的副本集名称。
  • The configsvr field set to true for the config server replica set.配置服务器副本集的configsvr字段设置为true
  • The members array with a document per each member of the replica set.members数组,副本集中的每个成员都有一个文档。
Important

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.出于测试目的,您可以创建单个成员复制副本集。

Note

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:对于每个分片,请使用以下步骤创建分片副本集:

1

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列表,可以用来连接到实例。

    Warning

    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.有关安全建议的完整列表,请参阅安全检查表。至少,考虑启用身份验证强化网络基础设施

  • Additional settings as appropriate to your deployment, such as storage.dbPath and net.port. 适用于部署的其他设置,如storage.dbPathnet.portFor 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选项以及其他适合您的部署的选项启动mongodFor 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参考页面。

2

Connect to one member of the shard replica set.连接到分片复制集的一个成员。

Connect mongosh to one of the replica set members.mongosh连接到其中一个复制集成员。

mongosh --host <hostname> --port <port>
3

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 the replication.replSetName or the --replSet option._id字段设置为replication.replSetName--replSet选项中指定的副本集名称。
  • The members array with a document per each member of the replica set.members数组,副本集中的每个成员都有一个文档。

The following example initiates a three member replica set.以下示例启动一个由三个成员组成的复制副本集。

Important

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

Start 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设置为配置服务器副本集名称和副本集的至少一个成员。

Warning

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:例如:

Warning

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. 此时,您的分片集群由mongosconfig服务器组成。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。指定运行mongoshostport

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.在连接到mongosmongosh会话中,使用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()方法。

Note

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 sh.shardCollection().如果集合为空,MongoDB将创建索引作为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, mongosh provides the method convertShardKeyToHashed(). 从4.0版本开始,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.此方法使用与哈希索引相同的哈希函数,可以用来查看键的哈希值。

Tip

See also: 另请参阅: