Building a MongoDB cluster (replica set + Shard)

Source: Internet
Author: User
Tags scp command

Complete example of building a MongoDB cluster (replica set + Shard) ...

Prepare four machines, namely BLUEJOE1,BLUEJOE2,BLUEJOE3, and BLUEJOE0

The replica set and Shard policy are determined as follows:

    • 3 replica sets will be created, named Shard1,shard2,shard3;
    • The above 3 replica sets as 3 shards;
    • Each replica set consists of 2 replicas (primary, secondary);
    • Copies are stored separately, that is, Shard1 exists bluejoe1 and bluejoe2 on each copy ... And so on
    • 3 Configuration library Instances will be created, one for each machine
    • Configure a MONGOs on BLUEJOE0 (MONGOs can be configured on the application side)
Draw a diagram:


The following are the steps:
  1. Download MONGDB installation package on BLUEJOE1;
  2. Unzip to/usr/local/mongdb (pay attention to renaming);
  3. Create the Data/node1 and Data/node2 directories, launch 2 Mongod instances, and note that they are shard1-1 and shard3-2, respectively:
    /usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/node1--port 10001--replSet shard1/bluejoe2:10002-- Logpath=/usr/local/mongodb/log/node1.log/usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/node2-- Port 10002--replset shard3/bluejoe3:10001--logpath=/usr/local/mongodb/log/node2.log
  4. Initializing replicas
    Db.runcommand ({"replsetinitiate": {"_id": "Shard1", "members": [{"_id": 1, "host": "bluejoe1:10001"},{"_id": 2, "host": "Bluejoe2:10002"}]})

  5. To create a data/configdb, launch the configuration library instance:
    /usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/configdb--port 20000--logpath=/usr/local/mongodb/ Log/configdb.log
  6. Using the SCP command, copy the MongoDB directory to the Bluejoe2 and Bluejoe3 machines and start the 3 instances on each machine in steps 3rd, 4, and note that the replicas are initialized;
  7. So far there should be 9 Mongd instances, 3 of which are configuration library instances, and the remaining 3 replica sets;
  8. Next, manage the shards and start the mongs on BLUEJOE0:
    /usr/local/mongodb/bin/mongos--port 30000--configdb bluejoe1:20000,bluejoe2:20000,bluejoe3:20000
  9. To connect the MONGOs, configure the Shard information:
    Mongos> use adminswitched to db Admindb.runcommand ({"Addshard": "shard1/bluejoe1:10001"}) Db.runcommand ({"AddShard ":" shard2/bluejoe2:10001 "}) Db.runcommand ({" Addshard ":" shard3/bluejoe3:10001 "})
  10. To turn on the Shard feature on the library:
    Mongos> use adminswitched to db adminmongos> Db.runcommand ({"enablesharding": "Test"}) {"OK": 1}mongos> Db.runC Ommand ({"Shardcollection": "Test.person", "key": {"name": 1}}) {"collectionsharded": "Test.person", "OK": 1}


Building a MongoDB cluster (replica set + Shard)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.