
Setting up a MongoDB replica set with three nodes involves several steps, including installing :-
MongoDB on each node
Configuring the nodes
Initiating the replica set
Here’s a step-by-step guide to help you through the process, as a domain we would be using www.v12technologies.com
Step 1: Install MongoDB on Each Node
For Ubuntu/Debian
Import the public key used by the package management system
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
Create a list file for MongoDB
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
Reload the local package database
sudo apt-get update
Install the MongoDB packages
sudo apt-get install -y mongodb-org
For CentOS/RHEL
Create a /etc/yum.repos.d/mongodb-org-4.4.repo file so that you can install MongoDB directly using yum
[mongodb-org-4.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
Install the MongoDB packages
sudo yum install -y mongodb-org
Step 2: Configure Each Node
Edit the MongoDB configuration file (/etc/mongod.conf) on each node to include the replica set name. For example, let's use rs0 as the replica set name
net: bindIp: 0.0.0.0 # Ensure the node listens on all IP addresses or specify particular IP addresses replication: replSetName: "rs0"
Start the MongoDB service on each node
sudo systemctl start mongod sudo systemctl enable mongod
Step 3: Initialize the Replica Set
Connect to the MongoDB instance on the first node using the MongoDB shell
mongo
Initiate the replica set
rs.initiate( { id: "rs0", members: [ { id: 0, host: "mongo1.v12technologies.com:27017" }, { id: 1, host: "mongo2.v12technologies.com:27017" }, { id: 2, host: "mongo3.v12technologies.com:27017" } ] } )
Check the status of the replica set to ensure that it has been properly configured
rs.status()
Step 4: Test the Setup
Connect to the MongoDB instance on any of the other nodes and verify that it has joined the replica set
mongo --host mongo2.v12technologies.com
Check the replica set status
rs.status()
Step 5: (Optional) Secure the Replica Set
Create an admin user with appropriate permissions
use admin db.createUser( { user: "admin", pwd: "password", roles: [ { role: "root", db: "admin" } ] } )
Enable authentication by editing the mongod.conf file on each node to include
security: authorization: "enabled"
Restart the MongoDB service on each node
sudo systemctl restart mongod
Connect using the created user to manage the replica set:
mongo -u admin -p password --authenticationDatabase admin
Setting up a MongoDB replica set provides redundancy and high availability, which are critical for production environments. By following these steps provided by V12 Technologies you’ll have a three-node MongoDB replica set configured and ready to handle your data with improved reliability and performance. You can also choose to increase or decrease the replica set using the same steps given above
---
If you are looking for Managed DevOps services, Managed Cloud services, Disaster Recovery Services and Cloud Migration Services, Please visit us at https://www.v12technologies.com for more details or email to vs@v12technologies.com
Comments