Mongo Replication
Brief description for mongodb replication.
What is mongoDB Replication ?
It is nothing but multiple copies of same data on different server. So whenever one of database is failed it will not impact on your server execution. Also it provide increase read capacity as we can read data from different mongo db server.
What is Primary, secondary and Arbiter ?
- Primary: There will be only one primary instance in database replication. All write operation will receive to your primary instance only.
- Secondary: Secondary is same copy of your primary database. It have same data which is in Primary. When Primary goes down then one of secondary become Primary. (Secondary instances will hold election to elect itself a new primary)
- Arbiter: It is light weight instance. Arbiter don’t hold any data. If there is even number of secondary instance is in your replication then there will be difficulty for secondary instance to elect themselves as primary. So Arbiter will come in picture and it will help for election. Also it keep checking health of all instance. Make a note Arbiter is light weight instance which does not hod any data. So you can keep it on low memory instance as well.
/etc/mongod.conf
- We need to run mongod with configuration file where we have updated replica set and some other thing ie /ect/mongod.conf
- mongod.conf file contain all configuration regarding you mongodb. Like at which location you have stored data, Where is you log are stored etc. Also it have information regarding your replication name and bind ip and much more.
- For replication edit /etc/mongod.conf file and do following changes for replication.
replication:
replSetName: "replicaQA"
net:
bindIp: 0.0.0.0 // It will accessible for public network, Instead you can give IP address of your instance from where you are accessing this node
- replSetName is your replication name. You need to update it in all your mongodb Instances. (primary, Secondary, Arbitrary)
- bind ip: It’s network from where your this mongodb Instance should be accessible. Make sure all IP address is provided here. (Remote location where your server is running, Secondary mongo instance, etc ) 0.0.0.0 is work for all.
Update /etc/mongod.conf file in all your mongo instance with same replica name.
/etc/hosts:
Most of the time we ignore to update this file when we use logical DNS hostname instead of an ip address.
Error: failed to connect to server [host_name_ip1:27017] on first connect
[MongoError: getaddrinfo ENOTFOUND [host_name_ArbiterIP][host_name_Arb
If you get above error, below will be the one of solution for this.
What is logical DNS hostname?
We can give logical Domain name for our IP address and which will be referred in our replication configuration and at server while connecting to database. This file is at /etc/hosts. Update like => 10.XX.XX.XXX mongodb1.example:
Note: Make sure you have updated this file from wherever you are trying to access mongodb instance (And at all mongo instance as well) . Because all server should know what is the domain name for this IP.
eg: If your server is running on 10.13.XX.XX and mongo is at 10.14.XX.XX and you have updated DNS at 10.14.XX.XX then you should update it at 10.13.XX.XX.
Replica Configuration:
- rs.initiate({_id: “replica_qa”, members:[
{_id: 0, host: mongodb1.example:27017}, // Consider this as primary
{_id: 1, host: mongodb2.example:27017}, // Consider this as Secondary
{_id: 2, host: mongodb3.example:27017} // Consider this as Arbiter
] })
Run your mongo client one of your mongo DB instance (It will show primary) and run above command. mongodb1.example is your host name which you have set in hosts file.
- rs.conf() This command will show configuration of your mongodb instance.
- rs.status() This command will show you status of your mongodb instance.
- rs.add() You can add other instance by using this command.
Make sure your other MongoDB is in running state.
Node Server with mongoose:
- URI String:
db.connect(“mongodb://[userName:password]@mongodb1.example:27017,mongodb2.example:27017?replicaSet=replicaQA”,{options})