- 分片 >
- Sharded Cluster Administration >
- Convert a Replica Set to a Sharded Cluster
Convert a Replica Set to a Sharded Cluster¶
概述¶
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 3.4. For other versions of MongoDB, refer to the corresponding version of the MongoDB Manual.
The procedure is as follows:
创建初始的三成员复制集并且向集合中插入数据。查阅 搭建初始的复制集。
- Start the config servers and a mongos. See Deploy Config Server Replica Set and mongos.
增加初始的复制集作为一个分片。查阅 Add Initial Replica Set as a Shard 。
- Create a second shard and add to the cluster. See 该步骤部署了 config servers 的三成员复制集和 mongos 。.
对目标集合进行分片。查阅 Shard a Collection 。
准备工作¶
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.
Each server must have a resolvable domain, hostname, or IP address within your system.
本教程使用默认的数据目录(例如 /data/db 或者 /data/configdb )。使用合适的权限创建合适的目录。如果要使用不同路径,请查阅 配置文件选项 。
过程¶
搭建初始的复制集¶
该步骤创建初始的三成员复制集 rs0 。复制集成员在以下主机上: mongodb0.example.net , mongodb1.example.net 和 mongodb2.example.net 。
Start each member of the replica set with the appropriate options.¶
For each member, start a mongod, specifying the replica set name through the replSet option. Include any other parameters specific to your deployment. For replication-specific parameters, see Replication Options.
mongod --replSet "rs0"
Repeat this step for the other two members of the rs0 replica set.
Initiate the replica set.¶
From the mongo shell, run rs.initiate() to initiate a replica set that consists of the current member.
rs.initiate()
Add the remaining members to the replica set.¶
rs.add("mongodb1.example.net")
rs.add("mongodb2.example.net")
Create and populate a new collection.¶
The following step adds one million documents to the collection test_collection and can take several minutes depending on your system.
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();
请查阅 部署复制集 了解更多关于部署复制集的信息。
Restart the Replica Set as a Shard¶
在 3.4 版更改: For MongoDB 3.4 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.
Determine the primary and secondary members.¶
Connect a mongo shell to one of the members and run rs.status() to determine the primary and secondary members.
Restart secondary members with the --shardsvr option.¶
One secondary at a time, restart each secondary with the --shardsvr option. To continue to use the same port, include the --port option.
mongod --replSet "rs0" --shardsvr --port 27017
Repeat this step for the other secondary.
Restart the primary with the --shardsvr option.¶
Restart the primary with the --shardsvr option. To continue to use the same port, include the --port option.
mongod --replSet "rs0" --shardsvr --port 27017
Deploy Config Server Replica Set and mongos¶
This procedure deploys the three-member replica set for the config servers and the mongos.
启动配置服务器和 mongos 。 查阅 Deploy Config Server Replica Set and mongos 。
本教程一共使用了10台服务器:一台服务器上运行 mongos ,每三台服务器分别运行 第一个 replica set ,第二个复制集以及 config server replica set 。
Deploy the config servers as a three-member replica set.¶
Start a config server on mongodb7.example.net, mongodb8.example.net, and mongodb9.example.net. Specify the same replica set name. The config servers use the default data directory /data/configdb and the default port 27019.
mongod --configsvr --replSet configReplSet
To modify the default settings or to include additional options specific to your deployment, see mongod or 配置文件选项.
Connect a mongo shell to one of the config servers and run rs.initiate() to initiate the replica set.
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" }
]
} )
Start a mongos instance.¶
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.
mongos --configdb configReplSet/mongodb07.example.net:27019,mongodb08.example.net:27019,mongodb09.example.net:27019
Add Initial Replica Set as a Shard¶
从MongoDB3.2开始,分片集群的配置服务器可以被部署为一个 replica set 。复制集配置服务器必须运行 WiredTiger storage engine 。 MongoDB 3.2 已经不再使用 配置服务器中三个镜像 mongod 实例 。
Connect a mongo shell to the mongos.¶
mongo mongodb6.example.net:27017/admin
Add the shard.¶
Add a shard to the cluster with the sh.addShard method:
sh.addShard( "rs0/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017" )
该步骤部署了 config servers 的三成员复制集和 mongos 。¶
配置服务器使用以下主机: mongodb7.example.net, mongodb8.example.net, 和 mongodb9.example.net 。
在 3.4 版更改: For MongoDB 3.4 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.
Start each member of the replica set with the appropriate options.¶
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. Include any other parameters specific to your deployment. For replication-specific parameters, see Replication Options.
mongod --replSet "rs1" --shardsvr --port 27017
Repeat this step for the other two members of the rs1 replica set.
Initiate the replica set.¶
From the mongo shell, run rs.initiate() to initiate a replica set that consists of the current member.
rs.initiate()
Connect a mongo shell to the mongos.¶
mongo mongodb6.example.net:27017/admin
Add the shard.¶
In a mongo shell connected to the mongos, add the shard to the cluster with the sh.addShard() method:
sh.addShard( "rs1/mongodb3.example.net:27017,mongodb4.example.net:27017,mongodb5.example.net:27017" )
Shard a Collection¶
Connect a mongo shell to the mongos.¶
mongo mongodb6.example.net:27017/admin
Enable sharding for a database.¶
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:
sh.enableSharding( "test" )
The operation returns the status of the operation:
{ "ok" : 1 }
Determine the shard key.¶
For the collection to shard, determine the shard key. The shard key determines how MongoDB distributes the documents between shards. Good shard keys:
- have values that are evenly distributed among all documents,
- group documents that are often accessed at the same time into contiguous chunks, and
- allow for effective distribution of activity among shards.
Once you shard a collection with the specified shard key, you cannot change the shard key. For more information on shard keys, see 片键.
This procedure will use the number field as the shard key for test_collection.
Create an index on the shard key.¶
Before sharding a non-empty collection, create an index on the shard key.
use test
db.test_collection.createIndex( { number : 1 } )
Shard the collection.¶
In the test database, shard the test_collection, specifying number as the shard key.
use test
sh.shardCollection( "test.test_collection", { "number" : 1 } )
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.
Confirm the shard is balancing.¶
To confirm balancing activity, run db.stats() or db.printShardingStatus() in the test database.
use test
db.stats()
db.printShardingStatus()
Example output of the 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():
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("58039f730a4826076824c25f")
}
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.4.0" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: yes
Balancer lock taken at Sun Oct 16 2016 11:59:51 GMT-0400 (EDT) by ConfigServer:Balancer
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
3 : Success
1 : Failed with error 'aborted', from rs0 to rs1
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.