Search code examples
mongodbreplicationsharding

mongodb replication in sharded machine


I have done Replication in one single machine with three different port(Say 27018[master],27019,27020). Also I have done Sharding in one single machine with two different port(Say 27021,27022).

Now I have to implement replication for the sharded machine port. I need to implement replication for 27021 and 27022. How can I do this? Please help me to resolve this issue.

Steps followed :

Part 1: Set up 3 port for replication

  1. mongod --replSet rs0
  2. mongod --port 27018 --dbpath F:\Data1 --replSet rs0
  3. mongod --port 27020 --dbpath F:\Data2 --replSet rs0
  4. mongo localhost:27017
  5. rs.initiate()
  6. rs.add("ComputerName:27018")
  7. rs.add("ComputerName:27020")

Part 2: Now set up for sharding

  1. mongod --configsvr --replSet configReplSet
  2. mongod --port 27021 --dbpath F:\Data4 --replSet configReplSet
  3. mongod --port 27025 --dbpath F:\Data5 --replSet configReplSet
  4. mongo 127.0.0.1:27019
  5. rs.initiate({_id:"configReplSet",configsvr:true,members:[{_id: 0,host: "127.0.0.1:27021"},{_id:1,host: "127.0.0.1:27025"}]})

When I run this code I am getting an error:

{
    "ok" : 0,
    "errmsg" : "No host described in new configuration 1 for replica set configReplSet maps to this node",
    "code" : 93
}

So remaining steps I am not able to execute. Any idea how can I overcome this issue?


Solution

  • Got one method to achieve my requirement. Steps as follows

    Config Server and its replication

    • mongod --configsvr --dbpath F:\Data1\configdb\ --replSet rs2 --port 27017
    • mongod --configsvr --dbpath F:\Data2\configdb\ --replSet rs2 --port 27018
    • mongod --configsvr --dbpath F:\Data3\configdb\ --replSet rs2 --port 27019
    • mongo machineip:27017
    • config= {_id:"rs2", members :[{ _id:0, host:'10.18.0.225:27017'}, {_id:1, host:'10.18.0.225:27018'}, {_id:2, host:'10.18.0.225:27019'}]}
    • rs.initiate (config)
    • rs.status()

    Routing Server

    • mongos -configdb machineip:27017 --port 27020

    Shard1 and its replication

    • mongod --dbpath F:\Data4\db\ --shardsvr --replSet rs0 --port 27021
    • mongod --dbpath F:\Data5\db\ --shardsvr --replSet rs0 --port 27022
    • mongod --dbpath F:\Data6\db\ --shardsvr --replSet rs0 --port 27023

    Shard2 and its replication

    • mongod --dbpath F:\Data7\db\ --shardsvr --replSet rs1 --port 27024
    • mongod --dbpath F:\Data8\db\ --shardsvr --replSet rs1 --port 27025
    • mongod --dbpath F:\Data9\db\ --shardsvr --replSet rs1 --port 27026

    Replication configuration for Shard1

    • mongo machineip:27021
    • config= {_id:"rs0", members :[{ _id:0, host:'machineip:27021'},{_id:1,host:'machineip:27022'},{_id:2,host:'machineip:27023'}]}
    • rs.initiate(config)
    • rs.status()

    Replication configuration for Shard2

    • mongo machineip:27024
    • config= {_id:"rs0", members :[{ _id:0, host:'machineip:27024''},{_id:1,host:'machineip:27025''},{_id:2,host:'machineip:27026''}]}
    • rs.initiate(config)
    • rs.status()

    Sharding Configuration in route server

    • sh.status()
    • sh.addShard ("rs0/machineip:27021")
    • sh.addShard ("rs0/machineip:27024")
    • sh.status()
    • sh.enableSharding ("test")
    • sh.shardCollection ("test.stud", {id: 1})
    • sh.status()

    Hope this will help :-)