Configuring Sharding
http://www.mongodb.org/display/DOCS/A+Sample+Configuration+Session
理论:http://www.mongodb.org/display/DOCS/Configuring+Sharding
A Sample Configuration Session
The following example uses two shards (each with a single mongod process), one config db, and onemongosprocess, all running on a single test server. In addition to the script below, apython script for starting and configuring shard components on a single machineis available. Creating the ShardsFirst, start up a couplemongods to be your shards. $ mkdir /data/db/a /data/db/b $ ./mongod --shardsvr --dbpath /data/db/a --port 10000 > /tmp/sharda.log & $ cat /tmp/sharda.log $ ./mongod --shardsvr --dbpath /data/db/b --port 10001 > /tmp/shardb.log & $ cat /tmp/shardb.log Now you need a configuration server andmongos: $ mkdir /data/db/config $ ./mongod --configsvr --dbpath /data/db/config --port 20000 > /tmp/configdb.log & $ cat /tmp/configdb.log $ ./mongos --configdb localhost:20000 > /tmp/mongos.log & $ cat /tmp/mongos.log mongosdoes not require a data directory, it gets its information from the config server. You can toy with sharding by using a small--chunkSize, e.g. 1MB. This is more satisfying when you're playing around, as you won't have to insert 64MB of documents before you start seeing them moving around. It should not be used in production. $ ./mongos --configdb localhost:20000 --chunkSize 1 > /tmp/mongos.log & Setting up the ClusterWe need to run a few commands on the shell to hook everything up. Start the shell, connecting to themongosprocess (at localhost:27017 if you followed the steps above). To set up our cluster, we'll add the two shards (aandb). $ ./mongo MongoDB shell version: 1.6.0 connecting to: test > use admin switched to db admin > db.runCommand( { addshard : "localhost:10000" } ) { "shardadded" : "shard0000", "ok" : 1 } > db.runCommand( { addshard : "localhost:10001" } ) { "shardadded" : "shard0001", "ok" : 1 } Now you need to tell the database that you want to spread out your data at a database and collection level. You have to give the collection a key (or keys) to partition by. > db.runCommand( { enablesharding : "test" } ) {"ok" : 1} > db.runCommand( { shardcollection : "test.people", key : {name : 1} } ) {"ok" : 1} AdministrationTo see what's going on in the cluster, use theconfigdatabase. > use config switched to db config > show collections chunks databases lockpings locks mongos settings shards system.indexes version These collections contain all of the sharding configuration information. |
理论:http://www.mongodb.org/display/DOCS/Configuring+Sharding
Configuring Sharding
1. One to 1000 shards. Shards are partitions of data. Each shard consists of one or moremongodprocesses which store the data for that shard. When multiplemongod's are in a single shard, they are each storing the same data – that is, they are replicating to each other. For testing purposes, it's possible to start all the required processes on a single server, whereas in a production situation, a number ofserver configurationsare possible. Once the shards (mongod's), config servers, andmongosprocesses are running, configuration is simply a matter of issuing a series of commands to establish the various shards as being part of the cluster. Once the cluster has been established, you can begin sharding individual collections. This document is fairly detailed; for a terse, code-only explanation, see thesample shard configuration. If you'd like a quick script to set up a test cluster on a single machine, we have apython sharding scriptthat can do the trick. Sharding ComponentsFirst, start the individual shards (mongod's), config servers, andmongosprocesses. Shard ServersA shard server consists of amongodprocess or a replica set of mongod processes. For production, use areplica setfor each shard for data safety and automatic failover. To get started with a simple test, we can run a singlemongodprocess per shard, as a test configuration doesn't demand automated failover. Config ServersRun amongod --configsvrprocess for each config server. If you're only testing, you can use only one config server. For production, use three. Note: Replicating data to each config server is managed by the router (mongos); they have a synchronous replication protocol optimized for three machines, if you were wondering why that number. Do not run any of the config servers with --replSet; replication between them is automatic. Note: As the metadata of a MongoDB cluster is fairly small, it is possible to run the config server processes on boxes also used for other purposes. mongosRouterRunmongoson the servers of your choice. Specify the --configdb parameter to indicate location of the config database(s). Note: use dns names,not ip addresses, for the --configdb parameter's value. Otherwise moving config servers later isdifficult. Note that eachmongoswill read from the first config server in the list provided. If you're running config servers across more than one data center, you should put the closest config servers early in the list. Configuring the Shard ClusterOnce the shard components are running, issue the sharding commands. You may want to automate or record your steps below in a .js file for replay in the shell when needed. Start by connecting to one of themongosprocesses, and then switch to theadmindatabase before issuing any commands. The mongos will route commands to the right machine(s) in the cluster and, if commands change metadata, the mongos will update that on the config servers. So, regardless of the number ofmongosprocesses you've launched, you'll only need run these commands on one of those processes. You can connect to the admin database viamongoslike so: ./mongo <mongos-hostname>:<mongos-port>/admin > db admin Adding shardsEach shard can consist of more than one server (seereplica sets); however, for testing, only a single server with onemongodinstance need be used. You must explicitly add each shard to the cluster's configuration using theaddshardcommand: > db.runCommand( { addshard : "<serverhostname>[:<port>]" } ); {"ok" : 1 , "added" : ...} Run this command once for each shard in the cluster. If the individual shards consist of replica sets, they can be added by specifyingreplicaSetName/<serverhostname>[:port][,serverhostname2[:port],...], where at least one server in the replica set is given. > db.runCommand( { addshard : "foo/<serverhostname>[:<port>]" } ); {"ok" : 1 , "added" : "foo"} Any databases and collections that existed already in the mongod/replica set will be incorporated to the cluster. The databases will have as the "primary" host that mongod/replica set and the collections will not be sharded (but you can do so later by issuing ashardCollectioncommand). Optional Parametersname maxSize As an example: > db.runCommand( { addshard : "sf103", maxSize:100000/*MB*/ } );
Listing shardsTo see current set of configured shards, run thelistshardscommand: > db.runCommand( { listshards : 1 } ); This way, you can verify that all the shard have been committed to the system. Removing a shardSee theremoveshard command. Enabling Sharding on a DatabaseOnce you've added one or more shards, you can enable sharding on a database. Unless enabled, all data in the database will be stored on the same shard. After enabling you then need to run shardCollection on the relevant collections (i.e., the big ones). > db.runCommand( { enablesharding : "<dbname>" } );
Once enabled,mongoswill place new collections on the primary shard for that database. Existing collections within the database will stay on the original shard. To enable partitioning of data, we have to shard an individual collection. Sharding a CollectionUse theshardcollectioncommand to shard a collection. When you shard a collection, you must specify the shard key. If there is data in the collection, mongo will require an index to be created upfront (it speeds up the chunking process); otherwise, an index will be automatically created for you.
> db.runCommand( { shardcollection : "<namespace>",
key : <shardkeypatternobject> });
For example, let's assume we want to shard aGridFSchunkscollection stored in thetestdatabase. We'd want to shard on thefiles_idkey, so we'd invoke theshardcollectioncommand like so: > db.runCommand( { shardcollection : "test.fs.chunks", key : { files_id : 1 } } ) { "collectionsharded" : "mydb.fs.chunks", "ok" : 1 } You can use the {unique: true} option to ensure that the underlying index enforces uniqueness so long as the unique index is a prefix of the shard key. (note: prior to version 2.0 this worked only if the collection is empty). db.runCommand( { shardcollection : "test.users" , key : { email : 1 } , unique : true } ); If the "unique: true" option isnotused, the shard key does not have to be unique. db.runCommand( { shardcollection : "test.products" , key : { category : 1, _id : 1 } } );
You can shard on multiple fields if you are using a compound index. In the end, picking the right shard key for your needs is extremely important for successful sharding.Choosing a Shard Key. Examples
See Also |
Procedure
Complete this procedure by connecting to anymongosin the cluster using themongoshell.
You can only remove a shard by its shard name. To discover or confirm the name of a shard using thelistshardsorprintShardingStatuscommands or thesh.status()shell helper.
The following example will remove shard namedmongodb0.
Note
To successfully migrate data from a shard, thebalancerprocessmustbe active. Check the balancer state using thesh.getBalancerState()helper in themongoshell. See this section onbalancer operationsfor more information.
Remove Chunks from the Shard
Start by running theremoveShardcommand. This begins “draining” chunks from the shard you are removing.
db.runCommand( { removeshard: "mongodb0" } )
This operation will return a response immediately. For example:
{ msg : "draining started successfully" , state: "started" , shard :"mongodb0" , ok : 1 }
Depending on your network capacity and the amount of data in your cluster, this operation can take anywhere from a few minutes to several days to complete.
Check the Status of the Migration
You can run theremoveShardagain at any stage of the process to check the progress of the migration, as follows:
db.runCommand( { removeshard: "mongodb0" } )
The output will resemble the following document:
{ msg: "draining ongoing" , state: "ongoing" , remaining: { chunks: 42, dbs : 1 }, ok: 1 }
In theremainingsub document, a counter displays the remaining number of chunks that MongoDB must migrate to other shards, and the number of MongoDB databases that have “primary” status on this shard.
Continue checking the status of theremoveshardcommand until the number of chunks remaining is 0, then you can proceed to the next step.
Move Unsharded Databases
Databases with non-sharded collections store these collections on a single shard, known as the “primary” shard for that database. The following step is necessary only when the shard you want to remove is also the “primary” shard for one or more databases.
Issue the following command at themongoshell:
db.runCommand( { movePrimary: "myapp", to: "mongodb1" })
This command will migrate all remaining non-sharded data in the database namedmyappto the shard namedmongodb1.
Warning
Do not run themovePrimaryuntil you havefinisheddraining the shard.
This command will not return until MongoDB completes moving all data, which may take a long time. The response from this command will resemble the following:
{ "primary" : "mongodb1", "ok" : 1 }
Finalize the Migration
RunremoveShardagain to clean up all metadata information and finalize the removal, as follows:
db.runCommand( { removeshard: "mongodb0" } )
When successful, the response will be the following:
{ msg: "remove shard completed succesfully" , stage: "completed", host: "mongodb0", ok : 1 }
When the value of “state” is “completed”, you may safely stop themongodb0shard.
相关推荐
4.1.1.11 Packet Tracer - Configuring Extended ACLs Scenario 2.pka
本文将基于《Configuring Siebel Business Applications》(版本8.1)的内容,详细介绍如何配置Siebel业务应用程序。 #### 二、软件许可与版权信息 根据文档开头部分提供的信息,该文档为Siebel业务应用程序配置...
Configuring OSPF Authentication OSPF实验配置指南,认证配置方法,拓扑图,实验过程,实验现象。
Prepare for Microsoft Exam 70-410 – and help demonstrate your real-world mastery of implementing and configuring core services in Windows Server 2012 R2. Designed for experienced IT professionals ...
在本次实验“Configuring OSPF External Summary”中,我们旨在深入理解并实践OSPF(Open Shortest Path First)的外部路由汇总配置。该实验不仅涵盖了如何配置外部路由汇总,还强调了不同类型的外部汇总路由及其...
Cisco Packet Tracer 思科...0.2.1.8 Packet Tracer - Configuring RIPv2思科作业 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights Reserved
英文书籍,排版及页面清晰度不错,对SAP SD的相关知识还是比较全面的,个人建议有一定英语基础的人并且具备SAP基础知识的人去学习。
NAV350 报文解析 Telegram_listing_Telegrams_for_Configuring_and_Operating_the_NAV350_
根据提供的文件信息,本书《Installing and Configuring Windows Server 2012 Exam Ref 70-410》是一本针对Microsoft认证考试70-410的专业辅导教材。本书由Craig Zacker编写,并由Microsoft Press出版。以下是本书中...
4.1.3.5 Packet Tracer - Configuring IPv4 and IPv6 Interfaces.pka
Configuring SAP R3 FICO The Essential Resource for Configuring the Financial and Controlling Modules
Prepare for Microsoft Exam 70-687 – and help demonstrate your real-world mastery of configuring Windows 8.1 in the enterprise. Designed for experienced IT professionals ready to advance their status...
MCTS Self-paced Training Kit (Exam 70-640):Configuring Windows Server 2008 Active Directory (2nd Edition)
很详细的ERP Sales and Distribution,收入确认等
Configuring SAP R3 FICO The Essential Resource for Configuring the Financial and Controlling Modules.part3
西门子 plc Hans Berger Automating with 系列丛书 Automating with SIMATICS 7-1500 Configuring, Programming and Testingwith STEP 7 Professional 2017.pdf