Search code examples
mongodbsharding

How to configure the configuration file for MONGOS?


I am not able to cofigure the configuration file for mongos. How do i set the configurations?


Solution

  • Step 1) Create a separate database for the config server.

    mkdir /data/configdb
    

    Step 2) Start the mongodb instance in configuration mode. Suppose if we have a server named Server D which would be our configuration server, we would need to run the below command to configure the server as a configuration server.

    mongod –configdb ServerD: 27019
    

    Step 3) Start the mongos instance by specifying the configuration server

    mongos –configdb ServerD: 27019
    

    Step 4) From the mongo shell connect to the mongo's instance

    mongo –host ServerD –port 27017
    

    Step 5) If you have Server A and Server B which needs to be added to the cluster, issue the below commands

    sh.addShard("ServerA:27017")
    sh.addShard("ServerB:27017")
    

    Step 6) Enable sharding for the database. So if we need to shard the Employeedb database, issue the below command

    sh.enableSharding(Employeedb)
    

    Step 7) Enable sharding for the collection. So if we need to shard the Employee collection, issue the below command

    Sh.shardCollection("db.Employee" , { "Employeeid" : 1 , "EmployeeName" : 1})