Deploy a Sharded Cluster

时间:2022-02-13 15:12:32
  • Start the Config Server Database Instances
for example : 
mongod --configsvr --dbpath <path> --port <port>
mongod --configsvr --dbpath /data/configdb --port 27019
In production deployments, you must deploy exactly three config server instances, each running on different servers
to assure good uptime and data safety. In test environments, you can run all three instances on a single server.
  • Start the mongos Instances
for example : 
mongos --configdb <config server hostnames>
mongos --configdb cfg0.example.net:27019,cfg1.example.net:27019,cfg2.example.net:27019
  • deploy replica sets for each shard
A shard can be a standalone mongod or a replica set. In a production environment, each shard should be a replica set.
for example : 
cfg = ...

rs.initiate(cfg)

rs.conf()
rs.status()
rs.add("<hostname><:port>")
  • Add Shards to the Cluster
for example : 
sh.addShard("rs1/mongodb0.example.net:27017")
  • Enable Sharding for a Database
for example : 
sh.enableSharding("<database>")
  • Enable Sharding for a Collection

for example :

sh.shardCollection("<database>.<collection>", shard-key-pattern)
sh.shardCollection("events.alerts", { "_id": "hashed" } )