MongoDB Shard (Cluster)

Source: Internet
Author: User
Tags mongo shell

Basic Environment:

Due to resource constraints, only 3 VMs have a relationship, only two replicaset, each machine configuration is as follows:

10.10.1.55 This machine installs Primary1,configserver1, Arbiter1

10.10.1.56 installation Primary2,configserver2, Arbiter2

10.10.1.57 installation Secondary1,secondary2,configserver3,mongos


1.55 the configuration file for the machine is as follows:

Primary1 's conf file:

dbpath=/data/mongodb/rs0_0logpath=/data/mongodb/log/rs0_0.loglogappend=trueport=40000bind_ip= 192.168.11.55,10.10.1.55oplogsize=10000fork=truejournal = True#noprealloc = Truereplset=rs0directoryperdb=true

Configuration file for Arbiter1:

dbpath=/data/mongodb/rs0_arbiterlogpath=/data/mongodb/log/rs0_arbiter.loglogappend=trueport=40002bind_ip= 192.168.11.55,10.10.1.55oplogsize=10000fork=truejournal = True#noprealloc = Truereplset=rs0directoryperdb=true

Configuration file for ConfigServer1:

dbpath=/data/mongodb/rs0_conflogpath=/data/mongodb/log/rs0_conf.loglogappend=trueport=40006bind_ip= 192.168.11.55,10.10.1.55fork=truejournal = True#noprealloc = Trueconfigsvr=truedirectoryperdb=true

Different MONGO processes are initiated by Mongod--config filename respectively, and Primary1 ports are assigned on the 1.55 machine after successful boot: 40000,arbiter1 port: 40002, ConfigureServer1 Port: 40006

1.56 configuration of the machine:

Configuration file for Primary2:

dbpath=/data/mongodb/rs1_primarylogpath=/data/mongodb/log/rs1_p.loglogappend=truebind_ip= 192.168.11.56,10.10.1.56directoryperdb=true port=40003oplogsize=10000fork=truejournal = Truenoprealloc = Truereplset=rs1

Arbiter2 configuration file:

dbpath=/data/mongodb/rs1_arbiterlogpath=/data/mongodb/log/rs1_a.loglogappend=truebind_ip= 192.168.11.56,10.10.1.56directoryperdb=true port=40005oplogsize=10000fork=truejournal = Truenoprealloc = Truereplset=rs1

ConfigureServer2 configuration file:

dbpath=/data/mongodb/rs1_conflogpath=/data/mongodb/log/rs1_conf.loglogappend=truebind_ip= 192.168.11.56,10.10.1.56directoryperdb=true port=40007oplogsize=10000fork=truejournal = Truenoprealloc = Trueconfigsvr=true

Different MONGO processes are initiated by Mongod--config filename respectively, and Primary2 ports are assigned on the 1.55 machine after successful boot: 40003,arbiter2 port: 40005, ConfigureServer2 Port: 40007



1.56 configuration of the machine:

Rs0_secondary1 configuration:

Dbpath=/data/mongodb/rs0_secondary1logpath=/data/mongodb/log/rs0_secondary1.loglogappend=trueport=40001bind_ip =192.168.11.57,10.10.1.57oplogsize=10000fork=truejournal = True#noprealloc = Truereplset=rs0directoryperdb=true

Rs1_secondary1 configuration:

dbpath=/data/mongodb/rs1_secondary1logpath=/data/mongodb/log/rs1_secondary1.loglogappend=truebind_ip= 192.168.11.57,10.10.1.57directoryperdb=true port=40004oplogsize=10000fork=truejournal = Truenoprealloc = Truereplset=rs1

ConfigureServer3 configuration:

dbpath=/data/mongodb/confsvr3logpath=/data/mongodb/log/conf3.loglogappend=truebind_ip= 192.168.11.57,10.10.1.57directoryperdb=true port=40008oplogsize=10000fork=truejournal = trueconfigsvr=true

MONGOs configuration: (Start the MONGOs router to note that multiple server time must be synchronized, or an error occurs)

Logpath=/data/mongodb/log/mongos.logport = 40009configdb=10.10.1.55:40006,10.10.1.56:40007,10.10.1.57:40008fork = True

The different MONGO processes are initiated by Mongod--config filename respectively, and the Rs0_secondary1 ports are assigned on the 1.55 machine after successful startup by Netstat view: 40001,rs1_ Secondary1 Port: 40004,configureserver3 Port: 40008,mongos Routing Port: 40009



Now configure REPLICASET0 with MONGO Shell login primary1, steps as follows:

cfg={"_id": "Rs0", "members": [{"_id": 0, "host": "10.10.1.55:40000"}, {"_id": 1, "host": "10.10.1.57:40001"} ] }

Rs.initiate (CFG)
Rs.status ()

Rs.addarb ("10.10.1.55:40002")

Now configure REPLICASET1 with MONGO Shell login Primary2, steps as follows:

cfg={"_id": "Rs1", "members": [{"_id": 0, "host": "10.10.1.56:40003"}, {"_id": 1, "host": "10.10.1.57:40004"} ] }

Rs.initiate (CFG)
Rs.status ()

Rs.addarb ("10.10.1.56:40005")


To add shard information using the MONGO Shell login MONGOs route:

Mongo --host 10.10.1.57 --port 40009mongos> sh.addshard ("rs0/ 10.10.1.55:40000,10.10.1.57:40001 ") { " shardadded " : " Rs0 ", " OK " : 1 }mongos > sh.addshard ("rs1/10.10.1.56:40003,10.10.1.57:40004") {  "shardadded"  :  "rs1",  "OK"  : 1 }mongos> sh.status ()--- Sharding Status ---    sharding version: {     "_id"  : 1,     "version"  : 4,     "Mincompatibleversion"  : 4,     " CurrentVersion " : 5,    " Clusterid " : objectid (" 561C7BDD4315B18F9862ADB4 ")}  shards:    {  " _id " : " Rs0 ",    "Host"  :  "rs0/10.10.1.55:40000,10.10.1.57:40001"  }    {    "_id"  :  "Rs1",   "host"  :  "rs1/10.10.1.56:40003,10.10.1.57:40004 " }  databases:    {  " _id " : " Admin ",  " partitioned " : false,  " primary " : " config " }

Now create a new database to test the shards:

Mongos> Use people
Switched to DB people
Mongos> for (var i=1;i<10;i++) Db.customers.insert ({name: "Jordan" +i,country: "American"})
Writeresult ({"ninserted": 1})
Mongos> for (var i=1;i<10;i++) Db.customers.insert ({name: "Gaga" +i,country: "American"})
Writeresult ({"ninserted": 1})
Mongos> for (var i=1;i<10;i++) Db.customers.insert ({name: "Ham" +i,country: "UK"})
Writeresult ({"ninserted": 1})
Mongos> for (var i=1;i<10;i++) Db.customers.insert ({name: "Brown" +i,country: "UK"})
Writeresult ({"ninserted": 1})
Mongos> for (var i=1;i<10;i++) Db.customers.insert ({name: "Ramda" +i,country: "Malaysia"})
Writeresult ({"ninserted": 1})


To start creating shards:

Mongos> db.customers.ensureindex ({country:1,_id:1}) {     "Raw"  : {          "rs0/10.10.1.55:40000,10.10.1.57:40001"  : {              "Createdcollectionautomatically"  : false ,             "Numindexesbefore"  : 1,              "Numindexesafter"  : 2,              "OK"  : 1         }    },     "OK"  : 1}mongos>  Sh.shardcollection ("People.customers", {country:1,_id:1}) {  "collectionsharded"  :  " People.customers ", " OK " : 1 }mongos> sh.status ()--- sharding status  ---   sharding version: {     "_id"  : 1,     "version"  : 4,      "Mincompatibleversion"  : 4,     "CurrentVersion" &NBSP;:  5,     "Clusterid"  : objectid ("561C7BDD4315B18F9862ADB4")}   shards:    {   "_id"  :  "Rs0",   "host"  :  "rs0/ 10.10.1.55:40000,10.10.1.57:40001 " }    {  " _id " : " Rs1 ",    "host"  :  "rs1/10.10.1.56:40003,10.10.1.57:40004"  }  databases:     {   "_id"  :  "admin",   "partitioned"  : false,   " Primary " : " config " }    {  " _id " : " test ",   "Partitioned"  : false,   "primary"  :  "Rs0"  }    {    "_id"  :  "People",    "Partitioned"  : true,   "primary"  :  "Rs0"  }         people.customers             shard key: {  "Country"  : 1,  "_id"  : 1 }             chunks:                 rs0    1             {  "Country"  : {  "$minKey"  : 1  },  "_id"  : {  "$minKey"  : 1 } } -->> {  " Country " : { " $maxKey " : 1 }, " _id " : { " $maxKey "&NBSP;:  1 } } on : rs0 timestamp (1, 0)

Now that the data is not much more than one shard on Rs0, you can increase the number of shards by increasing the amount of data:

for (Var i=10;i<10000;i++) Db.customers.insert ({name: "Ham" +i,country: ' UK '}) for (Var i=10;i<10000;i++) Db.customers.insert ({name: "Ramda" +i,country: "Malaysia"})

Mongos> Sh.status ()
---sharding Status---
Sharding version: {
"_id": 1,
"Version": 4,
"Mincompatibleversion": 4,
"CurrentVersion": 5,
"Clusterid": ObjectId ("561C7BDD4315B18F9862ADB4")
}
Shards:
{"_id": "Rs0", "host": "Rs0/10.10.1.55:40000,10.10.1.57:40001"}
{"_id": "Rs1", "host": "Rs1/10.10.1.56:40003,10.10.1.57:40004"}
Databases
{"_id": "admin", "partitioned": false, "PRIMARY": "Config"}
{"_id": "Test", "partitioned": false, "PRIMARY": "Rs0"}
{"_id": "People", "partitioned": true, "PRIMARY": "Rs0"}
People.customers
Shard Key: {"Country": 1, "_id": 1}
Chunks
Rs1 2
Rs0 1
{"Country": {"$minKey": 1}, "_id": {"$minKey": 1}}-->> {"Country": "American", "_id": ObjectId ("561c7d A73af7c7865defefb1 ")} on:rs1 Timestamp (2, 0)
{"Country": "American", "_id": ObjectId ("561C7DA73AF7C7865DEFEFB1")}-->> {"Country": "UK", "_id": ObjectId (" 561c7db63af7c7865defefd4 ")} On:rs0 Timestamp (3, 1)
{"Country": "UK", "_id": ObjectId ("561c7db63af7c7865defefd4")}-->> {"Country": {"$maxKey": 1}, "_id": {" $maxKey ": 1}} on:rs1 Timestamp (3, 0)

Now there is a shard on the Rs0, and there are two shards on the rs1

This article is from the "Technical Blog" blog, please be sure to keep this source http://raytech.blog.51cto.com/7602157/1702465

MongoDB Shard (Cluster)

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.