10gen MongoDB online courses for DBAs: Replica sets

I’ve already written about the excellent MongoDB online courses that launched a few weeks ago. I signed up for both developer and DBA tracks, and even though I knew that it would be super useful and great, let me tell you now that Iâ??m in week 4 – the value is immense, with all the detailed video lectures, quizzes and homework, especially considering that the courses are absolutely free!

Week 4 in the DBA track is about replica sets, and even though I did some projects with Mongo before, they weren’t of such scale that required replica sets. That’s why this week content was especially interesting to me. There’s one quick tip that I’ve learned and wanted to share.

When initiating and configuring a replica set, make sure you’re connected to the primary set member only. I thought that I had to connect to each instance and initiate the set on each, but that’s not the case, and I ended up with two primaries instead of one.

So the steps would be (example assumes all 3 instances on localhost):

  1. Create data directories for each instance:
    mkdir -m /db/data/1
    mkdir -m /db/data/2
    mkdir -m /db/data/3
  2. Start 3 instances of mongod – they will be members of your set:
    mongod --port 27001 --dbpath /db/data/1 --replSet rs0
    mongod --port 27002 --dbpath /db/data/2 --replSet rs0
    mongod --port 27003 --dbpath /db/data/3 --replSet rs0
  3. Connect to the first instance only:
    mongo localhost:27001
  4. Initiate your replica set:
    rs.initiate()
  5. Optional: check your configuration with rs.conf()
  6. Add two more members to your replica set:
    rs.add("localhost:27002")
    rs.add("localhost:27003")
  7. Done! You can check your set by running rs.status()

Man, it’s so cool that such a crucial concept like database replication is implemented so gracefully in Mongo, and you can learn and do it!

One thought on “10gen MongoDB online courses for DBAs: Replica sets

  1. shiva

    hai , i followed above steps ,but unable to solve the problem. as follows ..
    27002 and 27003 are both running as primary only. but we need to add them as secondary for 27001.

    in the mongo –port 27001 .

    week4_replicaset:FATAL> rs.conf();
    {
    “_id” : “week4_replicaset”,
    “version” : 7,
    “members” : [
    {
    “_id” : 0,
    “host” : “ABCD_HOST:27001”
    },
    {
    “_id” : 1,
    “host” : “ABCD_HOST:27002”,
    “priority” : 0
    },
    {
    “_id” : 2,
    “host” : “ABCD_HOST:27003”,
    “priority” : 0
    }
    ]
    }

    when i run mongo –port 27002
    it is giving

    connecting to: 127.0.0.1:27002/test
    type “help” for help
    week4_replicaset:PRIMARY> show dbs;
    local 0.09375GB
    week4_replicaset:PRIMARY> homework.c()
    something is wrong i don’t see 3 members

    can anyone suggest me .. please?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s