Step
Download the MongoDB MSI Installer Package
https://www.mongodb.com/docs/manual/installation/
Choose the package you want.
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
config folder ( config1, config2, config3 )
shard folder - the same as config
mongos folder
Tips:
- Pay attention to the difference between mongos and mongod
Configure nodes
Port plan
shard-a: shard-b: config-a: mongos:6000 |
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 |
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 |
Configure mongos node
Edit mongos.cfg in mongos directory
configdb=config-a/hostname:5001,hostname:5002,hostname:5003 |
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" |
mongos node
sc create MongoRouter binPath= "E:\mongodb\bin\mongos.exe --config=E:\mongodb\datas\mongos\mongos.cfg --service" |
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 |
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 |
Enable shard on database
- Use sh.enableSharding(“dbName”) to enable shard on database
- Use db.getSiblingDB(“config”).databases.find() to check
sh.enableSharding("testdb") |
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 |
Verify: Insert a large amount of data
use testdb |
You can see that the data is allocated to two slices for storage.