On this page本页内容
This tutorial converts a single three-member replica set to a sharded cluster with two shards. 本教程将单个三成员副本集转换为具有两个分片的分片集群。Each shard is an independent three-member replica set. 每个分片都是一个独立的三成员副本集。This tutorial is specific to MongoDB 5.0. For other versions of MongoDB, refer to the corresponding version of the MongoDB Manual.本教程特定于MongoDB 5.0。有关MongoDB的其他版本,请参阅相应版本的MongoDB手册。
The procedure is as follows:程序如下:
mongos
. mongos
。mongos
.mongos
。Individual steps in these procedures note when downtime will occur.这些程序中的各个步骤会记录何时会发生停机。
These procedures cause some downtime for your deployment.这些过程会导致部署停机。
This tutorial uses a total of ten servers: one server for the 本教程总共使用十台服务器:一台服务器用于mongos
and three servers each for the first replica set, the second replica set, and the config server replica set.mongos
,三台服务器分别用于第一个副本集、第二个副本集和配置服务器副本集。
Each server must have a resolvable domain, hostname, or IP address within your system.每个服务器必须在系统中具有可解析的域、主机名或IP地址。
The tutorial uses the default data directories (e.g. 本教程使用默认的数据目录(例如/data/db
and /data/configdb
). /data/db
和/data/configdb
)。Create the appropriate directories with appropriate permissions. 创建具有适当权限的适当目录。To use different paths, see Configuration File Options .要使用不同的路径,请参阅配置文件选项。
This procedure creates the initial three-member replica set 此过程创建初始的三成员副本集rs0
. rs0
。The replica set members are on the following hosts: 副本集成员位于以下主机上:mongodb0.example.net
, mongodb1.example.net
, and mongodb2.example.net
.mongodb0.example.net
、 mongodb1.example.net
和mongodb2.example.net
。
For each member, start a 对于每个成员,使用以下设置启动mongod
instance with the following settings:mongod
实例:
replication.replSetName
option to the replica set name. replication.replSetName
选项设置为副本集名称。net.bindIp
option to the hostname/ip or a comma-delimited list of hostnames/ips.net.bindIp
选项设置为主机名/ip或以逗号分隔的主机名/ips列表。In this tutorial, the three 在本教程中,三个mongod
instances are associated with the following hosts:mongod
实例与以下主机关联:
Member 0 | mongodb0.example.net |
Member 1 | mongodb1.example.net |
Member 2 | mongodb2.example.net |
The following example specifies the replica set name and the ip binding through the 以下示例通过--replSet
and --bind_ip
command-line options:--replSet
和--bind_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.至少,考虑启用身份验证和加强网络基础设施。
mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>
For 对于<hostname(s)|ip address(es)>
, specify the hostname(s) and/or ip address(es) for your mongod
instance that remote clients (including the other members of the replica set) can use to connect to the instance.<hostname(s)|ip address(es)>
,指定远程客户端(包括副本集的其他成员)可以用来连接到该实例的mongod
实例的主机名和/或ip地址。
Alternatively, you can also specify the 或者,您也可以在配置文件中指定replica set name
and the ip addresses
in a configuration file:replica set name
和ip addresses
:
replication: replSetName: "rs0" net: bindIp: localhost,<hostname(s)|ip address(es)>
To start 要使用配置文件启动mongod
with a configuration file, specify the configuration file's path with the --config
option:mongod
,请使用--config
选项指定配置文件的路径:
mongod --config <path-to-config>
In production deployments, you can configure a init script to manage this process. 在生产部署中,可以配置init脚本来管理此过程。Init scripts are beyond the scope of this document.初始化脚本超出了本文档的范围。
mongosh
to one of the mongod
instances.mongosh
连接到其中一个mongod
实例。From the same machine where one of the 在运行mongod
is running (in this tutorial, mongodb0.example.net
), start mongosh
. mongod
之一的同一台机器上(在本教程中为mongodb0.example.net
),启动mongosh
。To connect to the 要连接到默认端口mongod
listening to localhost on the default port of 27017
, simply issue:27017
上监听localhost的mongod
,只需发出:
mongo
Depending on your path, you may need to specify the path to the 根据您的路径,您可能需要指定mongosh
binary.mongosh
二进制文件的路径。
From 从mongosh
, run rs.initiate()
on replica set member 0.mongosh
,对副本集成员0运行rs.initiate()
。
Run 仅在副本集的一个且仅一个rs.initiate()
on just one and only onemongod
instance for the replica set.mongod
实例上运行rs.initiate()
。
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地址的节点将无法通过启动验证,无法启动。
rs.initiate( { _id : "rs0", members: [ { _id: 0, host: "mongodb0.example.net:27017" }, { _id: 1, host: "mongodb1.example.net:27017" }, { _id: 2, host: "mongodb2.example.net:27017" } ] })
MongoDB initiates a replica set, using the default replica set configuration.MongoDB使用默认副本集配置启动副本集。
The following step adds one million documents to the collection 以下步骤将向集合test_collection
and can take several minutes depending on your system.test_collection
添加100万个文档,可能需要几分钟的时间,具体取决于您的系统。
To determine the primary, use 要确定主节点,请使用rs.status()
.rs.status()
。
Issue the following operations on the primary of the replica set:在副本集的主副本上执行以下操作:
use test var bulk = db.test_collection.initializeUnorderedBulkOp(); people = ["Marc", "Bill", "George", "Eliot", "Matt", "Trey", "Tracy", "Greg", "Steve", "Kristina", "Katie", "Jeff"]; for(var i=0; i<1000000; i++){ user_id = i; name = people[Math.floor(Math.random()*people.length)]; number = Math.floor(Math.random()*10001); bulk.insert( { "user_id":user_id, "name":name, "number":number }); } bulk.execute();
For more information on deploying a replica set, see Deploy a Replica Set.有关部署副本集的更多信息,请参阅部署副本集。
mongos
mongos
This procedure deploys the three-member replica set for the config servers and the 此过程为mongos
.config
服务器和mongos
部署三成员副本集。
mongodb7.example.net
, mongodb8.example.net
, and mongodb9.example.net
.mongodb7.example.net
、mongodb8.example.net
和mongodb9.example.net
。mongos
uses mongodb6.example.net
.mongos
使用mongodb6.example.net
。Start a config server on 在mongodb7.example.net
, mongodb8.example.net
, and mongodb9.example.net
. mongodb7.example.net
、mongodb8.example.net
和mongodb9.example.net
上启动配置服务器。Specify the same replica set name. 指定相同的副本集名称。The config servers use the default data directory 配置服务器使用默认数据目录/data/configdb
and the default port 27019
./data/configdb
和默认端口27019
。
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 configReplSet --bind_ip localhost,<hostname(s)|ip address(es)>
To modify the default settings or to include additional options specific to your deployment, see 要修改默认设置或包含特定于展开的其他选项,请参阅mongod
or Configuration File Options.mongod
或配置文件选项。
Connect 将mongosh
to one of the config servers and run rs.initiate()
to initiate the replica set.mongosh
连接到一个配置服务器,然后运行rs.initiate()
以启动副本集。
Run 仅在副本集的一个且仅一个rs.initiate()
on just one and only onemongod
instance for the replica set.mongod
实例上运行rs.initiate()
。
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地址的节点将无法通过启动验证,无法启动。
rs.initiate( { _id: "configReplSet", configsvr: true, members: [ { _id: 0, host: "mongodb07.example.net:27019" }, { _id: 1, host: "mongodb08.example.net:27019" }, { _id: 2, host: "mongodb09.example.net:27019" } ] } )
mongos
instance.mongos
实例。On 在mongodb6.example.net
, start the mongos
specifying the config server replica set name followed by a slash /
and at least one of the config server hostnames and ports.mongodb6.example.net
上,启动mongos
,指定配置服务器副本集名称,后跟斜杠/
和至少一个配置服务器主机名和端口。
mongos --configdb configReplSet/mongodb07.example.net:27019,mongodb08.example.net:27019,mongodb09.example.net:27019 --bind_ip localhost,<hostname(s)|ip address(es)>
For sharded clusters, 对于分片集群,分片的mongod
instances for the shards must explicitly specify its role as a shardsvr
, either via the configuration file setting sharding.clusterRole
or via the command line option --shardsvr
.mongod
实例必须通过配置文件设置sharding.clusterRole
或通过命令行选项--shardsvr
显式指定其角色作为shardsvr
。
Connect 将mongosh
to one of the members and run rs.status()
to determine the primary and secondary members.mongosh
连接到其中一个成员,然后运行rs.status()
来确定主要成员和次要成员。
--shardsvr
option.--shardsvr
选项重新启动次要成员。One secondary at a time, shut down and restart each secondary with the 一次一个辅助服务器,使用--shardsvr
option.--shardsvr
选项关闭并重新启动每个辅助服务器。
This step requires some downtime for applications connected to secondary members of the replica set. 此步骤要求连接到副本集辅助成员的应用程序停机。Applications connected to a secondary may error with 连接到辅助服务器的应用程序在重新启动辅助服务器后可能会出现CannotVerifyAndSignLogicalTime
after restarting the secondary until you perform the steps in Add Initial Replica Set as a Shard. CannotVerifyAndSignLogicalTime
错误,直到您执行将初始副本集添加为分片中的步骤为止。Restarting your application will also stop it from receiving 重新启动应用程序也会阻止它接收CannotVerifyAndSignLogicalTime
errors.CannotVerifyAndSignLogicalTime
错误。
To continue to use the same port, include the 要继续使用同一端口,请包括--port
option. --port
选项。Include additional options, such as 根据您的部署,包括其他选项,例如--bind_ip
, as appropriate for your deployment.--bind_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.至少,考虑启用身份验证和加强网络基础设施。
mongod --replSet "rs0" --shardsvr --port 27017 --bind_ip localhost,<hostname(s)|ip address(es)>
Include any other options as appropriate for your deployment. 包括适合您的部署的任何其他选项。Repeat this step for the other secondary.对其他辅助设备重复此步骤。
Connect 将mongosh
to the primary and stepdown the primary.mongosh
连接到主服务器,然后逐步降低主服务器。
This step requires some downtime. 此步骤需要一些停机时间。Applications may error with 在执行“将初始副本集添加为分片”中的步骤之前,应用程序在逐步停止主副本后可能会出现CannotVerifyAndSignLogicalTime
after stepping down the primary until you perform the steps in Add Initial Replica Set as a Shard. CannotVerifyAndSignLogicalTime
错误。Restarting your application will also stop it from receiving 重新启动应用程序也会阻止它接收CannotVerifyAndSignLogicalTime
errors.CannotVerifyAndSignLogicalTime
错误。
rs.stepDown()
--shardsvr
option.--shardsvr
选项重新启动主服务器。Shut down the primary and restart with the 关闭主服务器,然后使用--shardsvr
option.--shardsvr
选项重新启动。
To continue to use the same port, include the 要继续使用同一端口,请包括--port
option.--port
选项。
mongod --replSet "rs0" --shardsvr --port 27017 --bind_ip localhost,<hostname(s)|ip address(es)>
Include any other options as appropriate for your deployment.包括适合您的部署的任何其他选项。
The following procedure adds the initial replica set 以下过程将初始副本集rs0
as a shard.rs0
添加为分片。
Add a shard to the cluster with the 使用sh.addShard()
method:sh.addShard()
方法将分片添加到群集中:
sh.addShard( "rs0/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017" )
The following procedure deploys a new replica set 以下过程为第二个分片部署一个新的副本集rs1
for the second shard and adds it to the cluster. rs1
,并将其添加到集群中。The replica set members are on the following hosts: 副本集成员位于以下主机上:mongodb3.example.net
, mongodb4.example.net
, and mongodb5.example.net
.mongodb3.example.net
、mongodb4.example.net
和mongodb5.example.net
。
For sharded clusters, 对于分片集群,分片的mongod
instances for the shards must explicitly specify its role as a shardsvr
, either via the configuration file setting sharding.clusterRole
or via the command line option --shardsvr
.mongod
实例必须通过配置文件设置sharding.clusterRole
或通过命令行选项--shardsvr
显式指定其角色作为shardsvr
。
For each member, start a 对于每个成员,启动一个mongod
, specifying the replica set name through the --replSet
option and its role as a shard with the --shardsvr
option. mongod
,通过--replSet
选项指定副本集名称,并使用--shardsvr
选项指定其作为分片的角色。Specify additional options, such as 根据需要指定其他选项,例如--bind_ip
, as appropriate.--bind_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.至少,考虑启用身份验证和加强网络基础设施。
For replication-specific parameters, see Replication Options.有关复制特定的参数,请参阅复制选项。
mongod --replSet "rs1" --shardsvr --port 27017 --bind_ip localhost,<hostname(s)|ip address(es)>
Repeat this step for the other two members of the 对rs1
replica set.rs1
副本集的其他两个成员重复此步骤。
From 在mongosh
, run rs.initiate()
to initiate a replica set that consists of the current member.mongosh
中,运行rs.initiate()
以启动由当前成员组成的副本集。
Run 仅在副本集的一个且仅一个rs.initiate()
on just one and only onemongod
instance for the replica set.mongod
实例上运行rs.initiate()
。
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地址的节点将无法通过启动验证,无法启动。
rs.initiate( { _id : "rs1", members: [ { _id: 0, host: "mongodb3.example.net:27017" }, { _id: 1, host: "mongodb4.example.net:27017" }, { _id: 2, host: "mongodb5.example.net:27017" } ] })
In 在mongosh
, when connected to the mongos
, add the shard to the cluster with the sh.addShard()
method:mongosh
中,当连接到mongos
时,使用sh.addShard()
方法将分片添加到集群中:
sh.addShard( "rs1/mongodb3.example.net:27017,mongodb4.example.net:27017,mongodb5.example.net:27017" )
Before you can shard a collection, you must first enable sharding for the collection's database. 在对集合进行分片之前,必须首先对集合的数据库启用分片。Enabling sharding for a database does not redistribute data but makes it possible to shard the collections in that database.为数据库启用分片不会重新分发数据,但可以对该数据库中的集合进行分片。
The following operation enables sharding on the 以下操作在test
database:test
数据库上启用分片:
sh.enableSharding( "test" )
mongos
uses "majority"
for the enableSharding
command and its helper sh.enableSharding()
.mongos
对enableSharding
命令及其助手shenableSharding()
使用"majority"
。
The operation returns the status of the operation:操作返回操作的状态:
{ "ok" : 1 }
For the collection to shard, determine the shard key. 对于要分片的集合,确定分片键。The shard key determines how MongoDB distributes the documents between shards. 分片键决定MongoDB如何在shard之间分发文档。Good shard keys:好的分片键:
For more information, see Choose a Shard Key.有关详细信息,请参阅选择分片键。
This procedure will use the 此过程将使用number
field as the shard key for test_collection
.number
字段作为test_collection
的分片键。
Before sharding a non-empty collection, create an index on the shard key.在分片非空集合之前,请在分片键上创建索引。
use test db.test_collection.createIndex( { number : 1 } )
In the 在test
database, shard the test_collection
, specifying number
as the shard key.test
数据库中,分片test_collection
,指定数字作为分片键。
use test sh.shardCollection( "test.test_collection", { "number" : 1 } )
mongos
uses "majority"
for the write concern of the shardCollection
command and its helper sh.shardCollection()
.mongos
对shardCollection
命令及其助手sh.shardCollection()
的写入关注使用"majority"
。
The method returns the status of the operation:该方法返回操作的状态:
{ "collectionsharded" : "test.test_collection", "ok" : 1 }
The balancer redistributes chunks of documents when it next runs. 平衡器在下次运行时重新分配大块文档。As clients insert additional documents into this collection, the 当客户端向这个集合中插入额外的文档时,mongos
routes the documents to the appropriate shard.mongos
会将文档路由到适当的分片。
To confirm balancing activity, run 要确认平衡活动,请在db.stats()
or db.printShardingStatus()
in the test
database.test
数据库中运行db.stats()
或db.printShardingStatus()
。
use test db.stats() db.printShardingStatus()
Example output of the db.stats()
:db.stats()
的输出示例:
{ "raw" : { "rs0/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017" : { "db" : "test", "collections" : 1, "views" : 0, "objects" : 640545, "avgObjSize" : 70.83200339949052, "dataSize" : 45370913, "storageSize" : 50438144, "numExtents" : 0, "indexes" : 2, "indexSize" : 24502272, "ok" : 1, "$gleStats" : { "lastOpTime" : Timestamp(0, 0), "electionId" : ObjectId("7fffffff0000000000000003") } }, "rs1/mongodb3.example.net:27017,mongodb4.example.net:27017,mongodb5.example.net:27017" : { "db" : "test", "collections" : 1, "views" : 0, "objects" : 359455, "avgObjSize" : 70.83259935179647, "dataSize" : 25461132, "storageSize" : 8630272, "numExtents" : 0, "indexes" : 2, "indexSize" : 8151040, "ok" : 1, "$gleStats" : { "lastOpTime" : Timestamp(0, 0), "electionId" : ObjectId("7fffffff0000000000000001") } } }, "objects" : 1000000, "avgObjSize" : 70, "dataSize" : 70832045, "storageSize" : 59068416, "numExtents" : 0, "indexes" : 4, "indexSize" : 32653312, "fileSize" : 0, "extentFreeList" : { "num" : 0, "totalSize" : 0 }, "ok" : 1 }
Example output of the db.printShardingStatus()
:db.printShardingStatus()
的输出示例:
--- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5be0a488039b1964a7208c60") } shards: { "_id" : "rs0", "host" : "rs0/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017", "state" : 1 } { "_id" : "rs1", "host" : "rs1/mongodb3.example.net:27017,mongodb4.example.net:27017,mongodb5.example.net:27017", "state" : 1 } active mongoses: "3.6.8" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: yes Collections with active migrations: test.test_collection started at Mon Nov 05 2018 15:16:45 GMT-0500 Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 1 : Success databases: { "_id" : "test", "primary" : "rs0", "partitioned" : true } test.test_collection shard key: { "number" : 1 } unique: false balancing: true chunks: rs0 5 rs1 1 { "number" : { "$minKey" : 1 } } -->> { "number" : 1195 } on : rs1 Timestamp(2, 0) { "number" : 1195 } -->> { "number" : 2394 } on : rs0 Timestamp(2, 1) { "number" : 2394 } -->> { "number" : 3596 } on : rs0 Timestamp(1, 5) { "number" : 3596 } -->> { "number" : 4797 } on : rs0 Timestamp(1, 6) { "number" : 4797 } -->> { "number" : 9588 } on : rs0 Timestamp(1, 1) { "number" : 9588 } -->> { "number" : { "$maxKey" : 1 } } on : rs0 Timestamp(1, 2)
Run these commands for a second time to demonstrate that chunks are migrating from 再次运行这些命令,以演示块正在从rs0
to rs1
.rs0
迁移到rs1
。