Step-by-Step Guide to Setting Up MongoDB Replication on EC2 instance
Introduction:
MongoDB replication offers high availability and data redundancy by maintaining multiple copies of your data across multiple servers. In this guide, we will walk you through the process of setting up MongoDB replication using a replica set. Replica sets provide automatic failover and allow you to distribute your data for improved performance and fault tolerance.
Prerequisites:
⦁ An active AWS Cloud account is required.
⦁ Launch three EC2 instances here I am running the Ubuntu 20.04 operating system.
⦁ Make sure to open port 27017 (for MongoDB) and port 22 (for SSH) in the security groups associated with the EC2 instances.
⦁ I have Chosen the t2.micro instance type when launching the EC2 instances.
⦁ Access the EC2 instances using Putty or any SSH client of your choice.
Please find the below architecture diagram for the same.
Step 1: Hostname Resolution
To enable communication between the MongoDB nodes using hostnames, edit the “/etc/hosts” file on each node and add the following lines:
nano /etc/hosts
Step 2: Installing MongoDB Server
⦁ On each node, install MongoDB by following these steps:
⦁ Switch to the root user: sudo su
⦁ Add the MongoDB GPG key: curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
⦁ Add the MongoDB repository: echo “deb [ arch=amd64,arm64 ] <https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4> multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
⦁ Update the package index: sudo apt update -y
⦁ Install MongoDB: sudo apt install mongodb-org
⦁ Start the mongo db service: sudo systemctl start mongod.service
Step 3: Creating an Admin User on the Primary Node
⦁ On the primary node, create an admin user for MongoDB authentication:
⦁ Connect to MongoDB: mongo
⦁ Switch to the “admin” database: use admin
⦁ Create an admin user with a password:
⦁ db.createUser({user: “mongo-admin”, pwd: “password”, roles:[{role: “root”, db: “admin”}]})
Exit the MongoDB shell: exit
Step 4: Configuring the Primary Node
On the primary node, configure the MongoDB main configuration file:
- Generate a key file:
2.Copy the key file to the second and third nodes: scp /opt/mongo-keyfile root@mongo-node2:/opt/ and scp /opt/mongo-keyfile root@mongo-node3:/opt/
If we get error while copying, we can also copy the keyfile to node2 and node3 using winscp.
3.Edit the MongoDB configuration file: nano /etc/mongod.conf
Update the configuration with the following settings:
Step 5: Configuring the Second and Third Nodes
⦁ On the second and third nodes, configure the MongoDB main configuration file:
⦁ Edit the MongoDB configuration file: nano /etc/mongod.conf
⦁ Update the configuration with the following settings:
Set ownership and permission for the key file on second and third node.
Step 6: Restarting MongoDB Service on All Nodes
Restart MongoDB on all nodes to apply
the configuration changes:
Step 7: Starting Replication and Adding Members
⦁ On the primary node, initiate the replica set and add the secondary nodes:
⦁ Connect to MongoDB using the admin user: mongo -u mongo-admin -p — authenticationDatabase admin
⦁ Initiate the replica set: rs.initiate()
⦁ Add the second node as a member: rs.add(“mongo-node2”)
⦁ Add the third node as a member: rs.add(“mongo-node3”)
⦁ Check the status of the replica set: rs.status()
Step 8: Testing Replication
⦁ On the mongo-node1, connect to the Mongo shell with the following command:
⦁ Connect to MongoDB using the admin user: mongo -u mongo-admin -p — authenticationDatabase admin
show databases
Create databse demo:
use demo
Insert the data in database:
db.collectionName.insertOne({ field1: “value1”, field2: “value2” })
show dbs
Now check the same values on Node2 and Node3.
Checking on Node2 .
mongo -u mongo-admin -p — authenticationDatabase admin
show dbs
But it will give error to resolve this error, use rs.slaveOk().
rs.slaveOk().
The command “rs.slaveOk()” is used in MongoDB to allow read operations on secondary replica set members. By default, replica set members that are not the primary are not allowed to serve read operations.
show dbs
You can see now demo database added.
use demo
switch to demo databse
show collections
we can see now the collections added.
Similarly, we can check on node3.
Conclusion:
Congratulations! We have successfully set up MongoDB replication using a replica set. By following this guide, we have ensured high availability and data redundancy for our MongoDB deployment. Replica sets provide automatic failover and enable you to scale your database for improved performance. Experiment with different configurations and explore advanced features to optimize your MongoDB replication setup.
Remember to adapt the instructions according to your specific environment and requirements. Happy replicating!
Note: Make sure to verify and test the steps mentioned in this guide before deploying in a production environment.
References:
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-20-04