Building a MongoDB cluster (replica set + Shard)

Source: Internet
Author: User
Tags scp command

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

Prepare four machines, each bluejoe1,bluejoe2,bluejoe3, and BLUEJOE0

The replica set and the Shard policy determine such as the following:

    • 3 replica sets will be created, named Shard1,shard2,shard3;
    • The above 3 replica sets as 3 shards;
    • Each replica set consists of 3 replicas (primary, secondary 1, and 2);
    • Copies are stored separately, that is, Shard1 exists bluejoe1, Bluejoe2, bluejoe3 on each part ...

      And so on

    • 3 Configuration library Instances will be created, one for each machine
    • Configuring a MONGOs on Bluejoe0 (MONGOs can be configured on the application side)
Draw a diagram:


Detailed operating procedures such as the following:
  1. Download the MONGDB installation package on the BLUEJOE1.
  2. Unzip to/usr/local/mongdb (note renaming).
  3. Create a DATA/DB1,DB2,DB3 folder, launch 3 Mongod instances, and note Replset's name:
    mkdir/usr/local/mongodb/data/db1/usr/local/mongodb/data/db2/usr/local/mongodb/data/db3nohup/usr/local/mongodb/ Bin/mongod--dbpath/usr/local/mongodb/data/db1--port 10001--replset db1--logpath=/usr/local/mongodb/log/db1.log &nohup/usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/db2--port 10002--replSet DB2--logpath=/ Usr/local/mongodb/log/db2.log &nohup/usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/db3--port 10003--replset db3--logpath=/usr/local/mongodb/log/db3.log &

  4. Use the SCP command. Copy the MongoDB folder to the Bluejoe2 and Bluejoe3 machines and start the 3 instances on each machine according to the 3rd step;
  5. Initialize replica:
    mongo bluejoe1:10001use Admindb.runcommand ({"replsetinitiate": {"_id" : "DB1", "members": [{"_id": 1, "host": "bluejoe1:10001"},{"_id": 2, "host": "bluejoe2:10001"},{"_id": 3, "host": " Bluejoe3:10001 "}]}}) MONGO Bluejoe1:10002use Admindb.runcommand ({" replsetinitiate ": {" _id ":" DB2 "," members ": [{" _id " : 1, "host": "Bluejoe1:10002"},{"_id": 2, "host": "Bluejoe2:10002"},{"_id": 3, "host": "Bluejoe3:10002"}]}}) MONGO Bluejoe1:10003use Admindb.runcommand ({"replsetinitiate": {"_id": "DB3", "members": [{"_id": 1, "host": "Bluejoe1:10003 "},{" _id ": 2," host ":" Bluejoe2:10003 "},{" _id ": 3," host ":" Bluejoe3:10003 "}]}}) 

  6. Observing the real-time replication characteristics of replica Sets
    1. Connect the bluejoe1:10001. See the word db1:primary>, indicating that it is the master node of DB1
    2. Create a new table. Create several test records
    3. Connect the bluejoe3:10002. See the word shard1:secondary>, indicating that it is a secondary node of the Shard1
    4. Note that the previously added record cannot be queried at this time, and the error is not master and Slaveok=false
    5. Back to bluejoe1:10001, set Slaveok
      Db.getmongo (). Setslaveok ()
    6. Back to bluejoe3:10002, you can see the records written by the master node.
  • Observing the failover characteristics of a replica set
    1. The process of killing bluejoe1:10001
    2. Connect the bluejoe3:10002 again. See the word shard1:primary>, that it has become the master node of Shard1
    3. If you start bluejoe1:10001 again, it turns out to be shard1:secondary>.
  • Create a data/configdb. To launch a configuration library instance:
    Mkdir/usr/local/mongodb/data/configdbnohup/usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/ ConfigDB--port 20000--logpath=/usr/local/mongodb/log/configdb.log &

    If you need to close the Mongod service, you can use the--shutdown option. Such as:
    /usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/configdb--shutdown

    There should be 12 instances of mongd up to now. Of these, 3 are the configuration library instances, and the remainder belong to 3 replica sets.
  • Next, manage the shards and start the mongs on BLUEJOE0:

    Nohup/usr/local/mongodb/bin/mongos--port 30000--configdb bluejoe1:20000,bluejoe2:20000,bluejoe3:20000 &
  • To connect the MONGOs, configure the Shard information:
    MONGO bluejoe0:30000mongos> use adminswitched to db Admindb.runcommand ({"Addshard": "db1/bluejoe1:10001"}) Db.runcommand ({"Addshard": "db2/bluejoe1:10002"}) Db.runcommand ({"Addshard": "Db3/bluejoe1:10003"})
  • View shard Condition:
    mongos>  db.runcommand ({listshards:1}) { " Shards ": [  {  "_id": "DB1",   "host": "Db1/bluejoe1:10001,bluejoe2:10001,bluejoe3:10001" Span style= "White-space:pre" > },  {  "_id": "DB2",   "host": "Db2/bluejoe1:10002,bluejoe2:10002,bluejoe3 : 10002 " },  {  "_id": "Db3",   "host": "Db3/bluejoe1:10003,bluejoe2:10003,bluejoe3 : 10003 " } ],  OK: 1} 
    It can be seen that although the register only provides the primary node of the replica set, MONGOs has known the auxiliary nodes;

  • To turn on the sharding feature for a library:
    Mongos> Db.runcommand ({"enablesharding": "Test"}) {"OK": 1}mongos> Db.runcommand ({"Shardcollection": " Test.person "," key ": {_id: ' hashed '}}) {" collectionsharded ":" Test.person "," OK ": 1}
  • Insert test data:
    Mongos> for (Var i=0;i<10;i++) {Db.person.insert ({name: "Bluejoe" +i});} Writeresult ({"ninserted": 1})
  • To view data Shard storage:
    [[email protected] ~]# MONGO Bluejoe3:10002mongodb Shell version:2.6.5connecting to:bluejoe3:10002/testshard1: Primary> Db.person.find () {"_id": ObjectId ("546ff1013a0d0ec68b42a914"), "name": "Bluejoe0"} {"_id": ObjectId ("546f f1013a0d0ec68b42a915 ")," name ":" Bluejoe1 "} {" _id ": ObjectId (" 546ff1013a0d0ec68b42a91a ")," name ":" Bluejoe6 "} {" _id " : ObjectId ("546ff1013a0d0ec68b42a91b"), "name": "Bluejoe7"} {"_id": ObjectId ("546ff1013a0d0ec68b42a91c"), "name": "BL Uejoe8 "}shard1:primary> exitbye[[email protected] ~]# MONGO Bluejoe2:10001mongodb Shell version: 2.6.5connecting to:bluejoe2:10001/testshard2:primary> Db.person.find () {"_id": ObjectId (" 546ff1013a0d0ec68b42a918 ")," name ":" Bluejoe4 "}shard2:primary> exitbye[[email protected] ~]# MONGO Bluejoe3 : 10001MongoDB Shell version:2.6.5connecting to:bluejoe3:10001/testshard3:primary> db.person.find () {"_id": ObjectId ("546ff1013a0d0ec68b42a916"), "name": "Bluejoe2"} {"_id": ObjectId ("546ff1013a0d0ec68b42a917 ")," name ":" Bluejoe3 "} {" _id ": ObjectId (" 546ff1013a0d0ec68b42a919 ")," name ":" Bluejoe5 "} {" _id ": Obje  CtId ("546ff1013a0d0ec68b42a91d"), "name": "Bluejoe9"}shard3:primary>

  • 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.