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 . 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):
- Create data directories for each instance:
mkdir -m /db/data/1
mkdir -m /db/data/2
mkdir -m /db/data/3
- 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
- Connect to the first instance only:
- Initiate your replica set:
- Optional: check your configuration with
- Add two more members to your replica set:
- Done! You can check your set by running
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!