Step

Download the MongoDB MSI Installer Package

https://www.mongodb.com/docs/manual/installation/


Choose the package you want.

install.png



Set up Folders

Planning cluster nodes

  • A config replica set ( 3 replica nodes, without arbitration node )

  • Two shard replica set ( each of them has 2 replica nodes + 1 arbitration node )

  • A mongos node


datas folder

datas1.png

datas2.png


config folder ( config1, config2, config3 )

config1.png

config2.png


shard folder - the same as config


mongos folder

mongos1.png

mongos2.png



Tips:

  • Pay attention to the difference between mongos and mongod


Configure nodes

Port plan

shard-a:               shard-b:               config-a:               mongos:6000
shard-a1:3001 shard-b1:4001 config1:5001
shard-a2:3002 shard-b2:4002 config2:5002
shard-a3:3003 shard-b3:4003 config3:5003

Configure shard nodes

Edit mongod.cfg in each shard directory and change dbpath, logpath, port and replSet to the corresponding values

dbpath=E:\mongodb\datas\shard-a1\data
logpath=E:\mongodb\datas\shard-a1\log\mongod.log
logappend=true
directoryperdb=true
rest=true
httpinterface=false
port=3001
shardsvr=true
replSet=shard-a

Configure config nodes

Edit mongod.cfg in each config directory and change dbpath, logpath and port to the corresponding values

dbpath=E:\mongodb\datas\config1\data
logpath=E:\mongodb\datas\config1\log\mongod.log
logappend=true
directoryperdb=true
rest=true
port=5001
configsvr=true
replSet=config-a

Configure mongos node

Edit mongos.cfg in mongos directory

configdb=config-a/hostname:5001,hostname:5002,hostname:5003
logpath=E:\mongodb\datas\mongos\log\mongos.log
logappend=true
port=6000

Host all nodes on Windows Service

Open the Command Prompt with Administrative Privileges

All shard and config nodes ( modify the service name and .cfg file path )

sc create MongoShardA1 binPath= "E:\mongodb\bin\mongod.exe --config=E:\mongodb\datas\shard-a1\mongod.cfg --service"
net start MongoShardA1

mongos node

sc create MongoRouter binPath= "E:\mongodb\bin\mongos.exe --config=E:\mongodb\datas\mongos\mongos.cfg --service"
net start MongoRouter

You can use netstat -nao command to check



Configure shard and config replica set

  • Switch to bin path location of the MongoDB files installed
  • Connect to any node in the shard-a
  • Initialize the replica set
  • Add the other two members of the replica set, a replica node and an arbitration node
  • You can use rs.status() to check the replica set status
mongo --port 3001
rs.initiate()
rs.add("hostname:3002")
rs.add("hostname:3003",{arbiterOnly:true}) # config: rs.add("hostname:5003")
rs.status()

shard-b and config-a are same to configure the replica set, the difference is that the config replica set does not need to specify an arbitrator node.



Add shard

  • Switch to bin path location of the MongoDB files installed
  • Connect to mongos node
  • Use sh.addShard to add sharded replica set for Router
  • Use db.getSiblingDB(“config”).shards.find() to check
mongo --port 6000                                
sh.addShard("shard-a/hostname:3001,hostname:3002,hostname:3003")
sh.addShard("shard-b/hostname:4001,hostname:4002,hostname:4003")
db.getSiblingDB("config").shards.find()

Enable shard on database

  • Use sh.enableSharding(“dbName”) to enable shard on database
  • Use db.getSiblingDB(“config”).databases.find() to check
sh.enableSharding("testdb")            
db.getSiblingDB("config").databases.find()

Slice Collection

  • Define a slice key ( you can use only one field as the slice key, or use a combination of slice key: sh.shardCollection(“dbName.collectionName”,{key1:1,key2:1})
  • Use db. getSiblingDB(“config”).collections.find() to check
sh.shardCollection("testdb.testform",{_id:1})  # use _id to slice
db.getSiblingDB("config").collections.find()

Verify: Insert a large amount of data

use testdb                              
for(i=0;i<1000000;i++){db.testform.insert({Uid:i,Desc:"This is a test data",Data:new Date()})}
mongo --port 6000
sh.status()

You can see that the data is allocated to two slices for storage.