发布于 2015-09-14 15:00:18 | 127 次阅读 | 评论: 0 | 来源: 网络整理
参数: |
|
---|
Use this method to add a database instance or replica set to a sharded cluster. This method must be run on a mongos instance. The host parameter can be in any of the following forms:
[hostname]
[hostname]:[port]
[set]/[hostname]
[set]/[hostname],[hostname]:port
You can specify shards using the hostname, or a hostname and port combination if the shard is running on a non-standard port.
警告
Do not use localhost for the hostname unless your configuration server is also running on localhost.
The optimal configuration is to deploy shards across replica sets. To add a shard on a replica set you must specify the name of the replica set and the hostname of at least one member of the replica set. You must specify at least one member of the set, but can specify all members in the set or another subset if desired. sh.addShard() takes the following form:
If you specify additional hostnames, all must be members of the same replica set.
sh.addShard("set-name/seed-hostname")
Example
sh.addShard("repl0/mongodb3.example.net:27327")
The sh.addShard() method is a helper for the addShard command. The addShard command has additional options which are not available with this helper.
也可以参考
参数: |
|
---|
Enables sharding on the specified database. This does not automatically shard any collections, but makes it possible to begin sharding collections using sh.shardCollection().
也可以参考
参数: |
|
---|
Shards the named collection, according to the specified shard key. Specify shard keys in the form of a document. Shard keys may refer to a single document field, or more typically several document fields to form a “compound shard key.”
See
Size of Sharded Collection
参数: |
|
---|
Splits the chunk containing the document specified by the query at its median point, creating two roughly equal chunks. Use sh.splitAt() to split a collection in a specific point.
In most circumstances, you should leave chunk splitting to the automated processes. However, when initially deploying a sharded cluster it is necessary to perform some measure of pre-splitting using manual methods including sh.splitFind().
参数: |
|
---|
Splits the chunk containing the document specified by the query as if that document were at the “middle” of the collection, even if the specified document is not the actual median of the collection. Use this command to manually split chunks unevenly. Use the “sh.splitFind()” function to split a chunk at the actual median.
In most circumstances, you should leave chunk splitting to the automated processes within MongoDB. However, when initially deploying a sharded cluster it is necessary to perform some measure of pre-splitting using manual methods including sh.splitAt().
参数: |
|
---|
Moves the chunk containing the documents specified by the query to the shard described by destination.
This function provides a wrapper around the moveChunk. In most circumstances, allow the balancer to automatically migrate chunks, and avoid calling sh.moveChunk() directly.
参数: |
|
---|
Enables or disables the balancer. Use sh.getBalancerState() to determine if the balancer is currently enabled or disabled and sh.isBalancerRunning() to check its current state.
也可以参考
返回: | boolean |
---|
Returns true if the balancer process is currently running and migrating chunks and false if the balancer process is not running. Use sh.getBalancerState() to determine if the balancer is enabled or disabled.
也可以参考
返回: | a formatted report of the status of the sharded cluster, including data regarding the distribution of chunks. |
---|
2.2 新版功能.
参数: |
|
---|
sh.addShardTag() associates a shard with a tag or identifier. MongoDB uses these identifiers to direct chunks that fall within a tagged range to specific shards.
sh.addTagRange() associates chunk ranges with tag ranges.
Always issue sh.addShardTag() when connected to a mongos instance.
Example
The following example adds three tags, NYC, LAX, and NRT, to three shards:
sh.addShardTag("shard0000", "NYC")
sh.addShardTag("shard0001", "LAX")
sh.addShardTag("shard0002", "NRT")
也可以参考
2.2 新版功能.
参数: |
|
---|
sh.addTagRange() attaches a range of values of the shard key to a shard tag created using the sh.addShardTag() method. Use this operation to ensure that the documents that exist within the specified range exist on shards that have a matching tag.
Always issue sh.addTagRange() when connected to a mongos instance.
Example
Given a shard key of {STATE:1,ZIP:1}, create a tag range covering ZIP codes in New York State:
sh.addTagRange( "exampledb.collection",
{STATE: "NY", ZIP: {minKey:1}},
{STATE:"NY", ZIP: {maxKey:1}},
"NY"
)
2.2 新版功能.
参数: |
|
---|
Removes the association between a tag and a shard.
Always issue sh.removeShardTag() when connected to a mongos instance.
也可以参考
返回: | a basic help text for all sharding related shell functions. |
---|
The following database commands support sharded clusters.
参数: |
|
---|
Use the addShard command to add a database instance or replica set to a sharded cluster. You must run this command when connected a mongos instance.
The command takes the following form:
{ addShard: "<hostname>:<port>" }
Example
db.runCommand({addShard: "mongodb0.example.net:27027"})
Replace <hostname>:<port> with the hostname and port of the database instance you want to add as a shard.
警告
Do not use localhost for the hostname unless your configuration server is also running on localhost.
The optimal configuration is to deploy shards across replica sets. To add a shard on a replica set you must specify the name of the replica set and the hostname of at least one member of the replica set. You must specify at least one member of the set, but can specify all members in the set or another subset if desired. addShard takes the following form:
{ addShard: "replica-set/hostname:port" }
Example
db.runCommand( { addShard: "repl0/mongodb3.example.net:27327"} )
If you specify additional hostnames, all must be members of the same replica set.
Send this command to only one mongos instance, it will store shard configuration information in the config database.
注解
Specify a maxSize when you have machines with different disk capacities, or if you want to limit the amount of data on some shards.
The maxSize constraint prevents the balancer from migrating chunks to the shard when the value of mem.mapped exceeds the value of maxSize.
Use the listShards command to return a list of configured shards. The command takes the following form:
{ listShards: 1 }
The enableSharding command enables sharding on a per-database level. Use the following command form:
{ enableSharding: "<database name>" }
Once you’ve enabled sharding in a database, you can use the shardCollection command to begin the process of distributing data among the shards.
The shardCollection command marks a collection for sharding and will allow data to begin distributing among shards. You must run enableSharding on a database before running the shardCollection command.
{ shardCollection: "<db>.<collection>", key: <shardkey> }
This enables sharding for the collection specified by <collection> in the database named <db>, using the key <shardkey> to distribute documents among the shard. <shardkey> is a document, and takes the same form as an index specification document.
Choosing the right shard key to effectively distribute load among your shards requires some planning.
也可以参考
分片 for more information related to sharding. Also consider the section on Shard Key Selection for documentation regarding shard keys.
警告
There’s no easy way to disable sharding after running shardCollection. In addition, you cannot change shard keys once set. If you must convert a sharded cluster to a standalone node or replica set, you must make a single backup of the entire cluster and then restore the backup to the standalone mongod or the replica set..
The shardingState command returns true if the mongod instance is a member of a sharded cluster. Run the command using the following syntax:
{ shardingState: 1 }
警告
This command obtains a write lock on the affected database and will block other operations until it has completed; however, the operation is typically short lived.
Starts the process of removing a shard from a cluster. This is a multi-stage process. Begin by issuing the following command:
{ removeShard : "[shardName]" }
The balancer will then migrate chunks from the shard specified by [shardName]. This process happens slowly to avoid placing undue load on the overall cluster.
The command returns immediately, with the following message:
{ msg : "draining started successfully" , state: "started" , shard: "shardName" , ok : 1 }
If you run the command again, you’ll see the following progress output:
{ msg: "draining ongoing" , state: "ongoing" , remaining: { chunks: 23 , dbs: 1 }, ok: 1 }
The remaining document specifies how many chunks and databases remain on the shard. Use db.printShardingStatus() to list the databases that you must move from the shard.
Each database in a sharded cluster has a primary shard. If the shard you want to remove is also the primary of one of the cluster’s databases, then you must manually move the database to a new shard. This can be only after the shard is empty. See the movePrimary command for details.
After removing all chunks and databases from the shard, you may issue the command again, to return:
{ msg: "remove shard completed successfully , stage: "completed", host: "shardName", ok : 1 }