On this page本页内容
This tutorial involves creating a new sharded cluster that consists of a 本教程涉及创建一个新的分片集群,该集群由一个mongos
, the config server replica set, and two shard replica sets.mongos
、配置服务器副本集和两个分片副本集组成。
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.确保网络和安全系统(包括所有接口和防火墙)允许这些连接。
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 MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅配置IP地址的节点将无法启动验证,并且不会启动。
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
作为集群中所有其他分片的主机。
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安全性进行内部身份验证和客户端访问。
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 |
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.选项设置为主机名/ip地址或以逗号分隔的主机名或ip地址列表,远程客户端(包括配置服务器副本集的其他成员以及分片集群的其他成员)可以使用该列表连接到实例。
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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.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 binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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 将mongosh
to one of the config server members.mongosh
连接到配置服务器成员之一。
mongosh --host <hostname> --port <port>
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:在副本集配置文档中,包括:
_id
set to the replica set name specified in either the replication.replSetName
or the --replSet
option._id
设置为在replication.replSetName
或--replSet
选项中指定的副本集名称。configsvr
field set to true
for the config server replica set.configsvr
字段设置为true
。members
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)启动并启动,继续创建分片副本集。
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:对于每个分片,使用以下步骤创建分片副本集:
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列表。
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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.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 将mongosh
to one of the replica set members.mongosh
连接到其中一个复制集成员。
mongosh --host <hostname> --port <port>
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:在副本集配置文档中,包括:
_id
field set to the replica set name specified in either the replication.replSetName
or the --replSet
option._id
字段设置为在replication.replSetName
或--replSet
选项中指定的副本集名称。members
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" } ] } )
mongos
for the Sharded Clustermongos
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
设置为配置服务器副本集名称,并以<replSetName>/<host:port>
格式至少设置一个副本集成员。
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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 binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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
和配置服务器组成。You can now connect to the sharded cluster using 现在可以使用mongosh
.mongosh
连接到分片集群。
Connect 将mongosh
to the mongos
. mongosh
连接到mongos
。Specify the 指定运行host
and port
on which the mongos
is running: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
后,继续下一个过程,将分片添加到集群。
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.重复这些步骤,直到集群包含所有所需的分片。
Before you can shard a collection, you must enable sharding for the collection's database. 在对集合进行分片之前,必须为集合的数据库启用分片。Enabling sharding for a database does not redistribute data but make it possible to shard the collections in that database.为数据库启用分片不会重新分发数据,但可以对该数据库中的集合进行分片。
From a 在连接到mongosh
session that is connected to the mongos
, use the sh.enableSharding()
method to enable sharding on the target database. mongos
的mongosh
会话中,使用sh.enableSharding()
方法在目标数据库上启用分片。Enabling sharding on a database makes it possible to shard collections within a database.在数据库上启用分片可以在数据库中分片集合。
sh.enableSharding("<database>")
Once you enable sharding for a database, MongoDB assigns a primary shard for that database where MongoDB stores all data in that database.一旦为数据库启用分片,MongoDB将为该数据库分配一个主分片,其中MongoDB存储该数据库中的所有数据。
Before you can shard a collection you must first enable sharding for the database where the collection resides.在对集合进行分片之前,必须首先为集合所在的数据库启用分片。
To shard a collection, connect 要共享集合,请将mongosh
to the mongos
and use the sh.shardCollection()
method.mongosh
连接到mongos
,并使用sh.shardCollection()
方法。
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, ... } )
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.该方法使用与散列索引相同的散列函数,并可用于查看键的散列值。