Replication: Replica Set Setup on Three Nodes Cluster
Replica Set Setup on Three Nodes Cluster
Add an Arbiter to the replica set
Add members to the replica set
Removes members from the replica set
Step 1: Launch EC2 instances (my blog for launching EC2 instances - mongodb-introduction-and-installation) One instance each for the ReplicaSet member node.
An instance for Mongos process which will connect our client ( MongoDB compass to our sharded MongoDB cluster).
Step 2: Configure the security group to all the inbound traffic/connection on the MongoDB server port.
A) Allow Traffic from anywhere
B) Add a port to all Three instances that you are going to use.
Step 3: Install MongoDB (on all AWS EC2 Ubuntu instances) (my blog for installing MongoDB on ubuntu - mongodb-introduction-and-installation).
Step 4: Replication setup (Ubuntu AWS EC2 instance):
Create directories on all the Three EC2 instances-
mkdir -p replicaset/member
Step 5: Start MongoDB with the following command on every EC2 instance
A) Enter the following command on your First EC2 instance.
(Note: Change the port that you are using and the public IPv4 DNS of your instance)
nohup mongod --port 28041 --bind_ip localhost,ec2-3-86-210-11.compute-1.amazonaws.com --replSet replica_demo --dbpath replicaset/member &
B) Enter the following command on your Second EC2 instance.
(Note: Change the port that you are using and the public IPv4 DNS of your instance)
nohup mongod --port 28042 --bind_ip localhost,ec2-3-82-236-231.compute-1.amazonaws.com --replSet replica_demo --dbpath replicaset/member &
C) Enter the following command on your Third EC2 instance.
(Note: Change the port that you are using and the public IPv4 DNS of your instance)
nohup mongod --port 28043 --bind_ip localhost,ec2-44-201-173-85.compute-1.amazonaws.com --replSet replica_demo --dbpath replicaset/member &ps -aef | grep mongo
D) The following command is used for listing the process running or not and also whether our MongoDB server running or not. If running then all good.
ps -aef | grep mongo
E) Then run the following command to check all directories and files and it will show the ReplicaSet members and files and processes.
ls -lRt
Step 6: Now Connect the first ec2 instance or server with the Mongosh by using the public IPv4 and port number of the first ec2 instance. Use the following command-
mongosh --host 3.86.210.11 --port 28041
Step 7: If you successfully enter in a mongoshell or inside mogosh where you can see the databases then all is good.
Then after that give the configuration to the ReplicaSet by using the rsconf() method. Define all the members of replicaset or all the instances that we launch with their public IPv4 and the port number of those instances.
rsconf = { _id: "replica_demo", members: [ { _id: 0, host: "3.86.210.11:28041" }, { _id: 1, host: "3.82.236.231:28042" }, { _id: 2, host: "44.201.173.85:28043" } ] }
Then Initilize the ReplicaSet members by using the following command.
rs.initiate(rsconf)
Then check the status of ReplicaSet. If running then all is good.
rs.status()
Then run the command rs.conf() command. It will give you all the details of the ReplicaSet member. for example- name of replicaSet, members, hostname, arbiterOnly, hidden, buildIndexes, priorities, secondary delays, voting, etc...
rs.conf()
Arbiter
Add an arbiter
Step 1: Create a new EC2 instance. This can be set up over an application server or some other server as well.
Create a directory for the EC2 instances-
mkdir -p replicaset/member
Enter the following command on your EC2 instance.
(Note: Change the port that you are using and the public IPv4 DNS of your instance)
nohup mongod --port 27017 --bind_ip localhost,ec2-44-202-164-113.compute-1.amazonaws.com --replSet replica_demo --dbpath replicaset/member &
Now, we have to add the arbiter to our primary server. Now go inside the mogosh of the first ec2 instance. Use the following command to add the arbiter-
(change the IPv4 public DNS of the instance and public IPv4 and port number of the instance)
rs.addArb("ec2-44-202-164-113.compute-1.amazonaws.com:27017") or rs.addArb("44.202.164.113:27017")
Now, if you want to remove the arbitor use the following command for removing the arbiter. (change the IPv4 public DNS of the instance and public IPv4 and port number of the instance)
rs.remove("ec2-44-202-164-113.compute-1.amazonaws.com:27017") or rs.remove("44.202.164.113:27017")
If it shows the following type of error
MongoServerError: Reconfig attempted to install a config that would change the implicit default write concern. Use the setDefaultRWConcern command to set a cluster-wide write concern and try the reconfig again.
then run the following command to give the permissions
db.adminCommand({ setDefaultRWConcern: 1, defaultWriteConcern: { w: 2 } })
Then again add the arbiter.
Repeat the Step 4.
Then check whether the arbiter was added or not by using the following command.
rs.status()
If the arbiter is showing then all is good.
To rename the hostname:
cfg = rs.conf()
cfg.members[0].host = "localhost,ec2-54-187-25-38.us-west-2.compute.amazonaws.com:28041"
rs.reconfig(cfg)