Introduction
MongoDB is an open-source NoSQL database that stores data in a flexible document-based format. MongoDB replica sets provide data replication and help improve database availability by maintaining multiple copies of the data across different hosts.
This guide explains how to install MongoDB on CentOS, configure replication, create a MongoDB cluster using a replica set, add an arbiter, and check the replication status using MongoDB commands.
Prerequisites
Before setting up the MongoDB cluster, make sure you have:
- Multiple CentOS servers/hosts.
- Root or
sudoaccess to the servers. - Network connectivity between all MongoDB hosts.
- Port
27017accessible between the MongoDB servers. - MongoDB installed on all hosts.
- Hostnames or IP addresses of all MongoDB servers.
Implementation
Step 1: Create the MongoDB Repository
Create a repository file named mongodb-org-3.2.repo under /etc/yum.repos.d/ with the following content:
[mongodb-org-3.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
Step 2: Install MongoDB
Install the MongoDB server using yum:
yum install -y mongodb-org
Step 3: Modify the MongoDB Data Path
If you want to use a different location for storing MongoDB data, modify the dbPath in /etc/mongodb.conf.
For example:
dbPath: /opt/mongodata
Step 4: Start the MongoDB Service
Start the MongoDB service:
sudo service mongod start
Enable MongoDB to start automatically after a server reboot:
chkconfig mongod on
Step 5: Configure the Bind IP
If MongoDB is accessed only locally, the existing bind IP configuration can be retained.
If MongoDB needs to be accessed from remote servers, configure the bind IP appropriately in /etc/mongodb.conf.
The original configuration can be commented out:
#bindIp:
Note: If MongoDB is exposed to remote hosts, make sure firewall rules and authentication are properly configured. Avoid exposing MongoDB directly to the public internet.
For additional MongoDB installation information, refer to the official MongoDB documentation.
Setting Up MongoDB Cluster
Step 1: Verify Connectivity Between Hosts
Make sure that all MongoDB hosts can communicate with each other over port 27017.
The MongoDB servers should be able to connect to one another before configuring replication.
Step 2: Configure Replication
Modify /etc/mongodb.conf on all MongoDB hosts and add the replication configuration:
replication:
replSetName: <replicationname>
Use the same replica set name on every MongoDB host.
Step 3: Restart MongoDB
After updating the configuration, restart the MongoDB service on all hosts.
sudo service mongod restart
Make sure the service starts successfully on each server before continuing.
Step 4: Initialize the Replica Set
On the primary host, open the MongoDB shell:
mongo
Execute the following configuration:
conf = {
_id: "<replicationName>",
members:
[
{_id : 0, host : "Host1IPAddress:27017"},
{_id : 1, host : "Host2IPAddress:27017"},
{_id : 2, host : "Host3IPAddress:27017"}
]
}
Initialize the replica set:
rs.initiate(conf)
MongoDB will use the configured hosts as members of the replica set.
Step 5: Check Replication Status
To verify the replica set status, execute:
rs.status()
This command displays information about the replica set members and their current states.
Adding a MongoDB Arbiter
MongoDB also supports an arbiter.
An arbiter does not store a copy of the dataset and cannot become a primary. It can participate in elections by providing an additional vote for selecting the primary.
Step 1: Remove the Host from the Existing Cluster
If the host is already part of the cluster, remove it first:
rs.remove("HOSTIPADDRESS:27017");
Step 2: Add the Host as an Arbiter
From the primary replica set member, execute:
rs.addArb("HOSTIPAddress:27017");
Step 3: Verify the Configuration
Check the replica set configuration:
rs.conf()
The arbiter member should show:
"arbiterOnly" : true
Bonus MongoDB Commands
Check Current Running Queries
To view currently running MongoDB operations:
db.currentOp();
Make the Primary Become Secondary
To step down the current primary:
rs.stepDown(300)
This allows another eligible replica set member to become primary.
Check Replication Status
Use:
rs.status()
Check Whether the Host Is Primary
Use:
db.isMaster()
This command can be used to check the role/status of the MongoDB server.
Conclusion
Setting up a MongoDB replica set allows MongoDB data to be replicated across multiple servers and provides better availability. By configuring the same replica set name on each host, adding the required members, and initialisingTalk to our experts
Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here. the replica set, you can create a basic MongoDB cluster.
MongoDB arbiters can also be used when an additional voting member is required without maintaining another copy of the dataset. Commands such as rs.status(), rs.conf(), and db.currentOp() help administrators monitor and manage the MongoDB environment.
FAQs
1. What is a MongoDB replica set?
A MongoDB replica set is a group of MongoDB instances that maintain copies of the same dataset and work together to provide replication and high availability.
2. Which port does MongoDB use by default?
MongoDB uses port 27017 by default. All replica set members should be able to communicate with each other over this port.
3. What is a MongoDB arbiter?
An arbiter is a MongoDB replica set member that does not store data. It participates in elections by providing a voting member without maintaining a copy of the dataset.
4. How can I check the MongoDB replica set status?
Run the following command from the MongoDB shell:
rs.status()
5. How can I check the replica set configuration?
Use:
rs.conf()
This displays the configuration and members of the replica set, including whether a member is configured as an arbiter.
Related Articles
- Using GraphQL to Create Blogs with Image Uploads in MongoDB – Learn how to use GraphQL with MongoDB to build a blog application and handle image uploads.
Read the article - Data Types and Data Model in MongoDB – Understand MongoDB data types and the fundamentals of designing data models for MongoDB applications.
Read the article
Talk to our experts.
Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.