To avoid data loss that may occure due to hardware failure or some service interruptions, you need to ensure your availability of database. to increase availability you can run the same instance of a database on different locations.
Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers. Replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.
In some cases, replication can provide increased read capacity as clients can send read operations to different servers.
Replica Set in MongoDB
A replica set is a group of mongodb instances that maintain the same data set. In a replica set, one node is primary node that receives all write operations. All other instances, such as secondaries, apply operations from the primary so that they have the same data set. Replica set can have only one primary node. There also is an optional arbiter node.
The primary records all changes to its data sets in its operation log
(oplog). the secondaries replicate the primary's oplog and apply the operations to their data sets such that the secondaries' data sets reflect the primary's data set.
When the primary goes down, one of secondaries will hold an election to elect itself the new primary.
Arbiters do not maintain any data set. It's purpose is to maintain a share in a replica set by responding to heartbeat and election requests by other replica set members. So they can function with a cheaper resource than a fully functional replica set member. Arbiters stay arbiter during election, and changing in primary member does not affect the arbiter.
to create a replica set, you need at least three mongodb installed (on three servers). these servers need to communicate with each other.
1. Configuring Hosts
each member of replica set should have a hostname that identifies it as a member of the set. add the following lines to
/etc/hosts file on each member.
2. MongoDB Authentication
Members of a replica set must use the same authentication, so they can communicate with each other. we will create a key file that will be used to secure authentication between the members.
copy this file to all members under
/opt/mongodb directory and assign the correct permissions.
3. Create Admin User
On the server that you intend to use as the primary member, create an administrative user with root privileges.
4. Configure MongoDB
make the following changes to your
/etc/mongod.conf file on each machine. change
192.168.1.1 to your machine's ip.
after changes, restart the mongod service.
5. Start Replication and Add Member
log int to primary machine and connect to mongodb shell using the administrator user:
start the replication:
you should an output like this:
add other members to replica set:
check the configuration of replica set:
you should see the members and the replica set configurations.
for checking the replica set status you can call
rs.status() function on your shell on each machine.
you can also add arbiter node by this command:
When a primary does not communicate with the other members of the set for more than the configured period (
electionTimeoutMillis = 10 seconds by default), an eligible secondary calls for an election to nominate itself as the new primary. The cluster attempts to complete the election of a new primary and resume normal operations. The replica set cannot process write operations until the election completes successfully.