How to Set Up a MongoDB Replica Set on CentOS – Step‑by‑Step Guide
This article explains MongoDB’s key features, compares its advantages and disadvantages, and provides a detailed, step‑by‑step guide to installing, configuring, and verifying a replica set on CentOS 6.6, including commands for package installation, configuration file editing, service startup, node addition, and data synchronization verification.
Introduction
MongoDB is a C++‑written, distributed, NoSQL database that offers scalable high‑performance storage for web applications. This guide focuses on MongoDB replica sets and sharding.
Overview
MongoDB bridges relational and non‑relational databases, supporting flexible data structures, powerful query language, indexing, replication, and automatic sharding. It stores data in BSON (an extended JSON) and provides drivers for many languages.
Key Features
High performance, easy deployment and use
Document‑oriented storage, schema‑free
Dynamic queries and full indexing, including embedded objects
Replication and automatic failover
Binary storage for large objects (e.g., videos)
Automatic sharding for cloud‑scale expansion
Multi‑language support: Ruby, Python, Java, C++, PHP, etc.
Advantages and Disadvantages
Advantages
Eventual consistency for faster reads
Document model simplifies data access
Built‑in GridFS for large file storage
Native sharding support
Rich third‑party ecosystem
Strong performance
Disadvantages
Limited transaction support
Higher storage footprint
Immature maintenance tools
Replica Set Types
MongoDB offers Master/Slave (deprecated) and Replica Set replication. The modern approach uses Replica Sets, which require at least three odd‑numbered nodes and may include arbiter nodes for elections.
Replica Set Architecture
Configuration Steps
1. Install Required Packages
# cd mongodb/
# yum install *.rpm -y # install three RPM packagesRepeat on all nodes.
2. Edit the Configuration File
# vim /etc/mongod.conf
logpath=/var/log/mongodb/mongod.log
logappend=true
fork=true
dbpath=/mongodb/data # data location
pidfilepath=/var/run/mongodb/mongod.pid
#bind_ip=127.0.0.1 # comment to listen on all interfaces
httpinterface=true # enable web interface
rest=true
replSet=testSet # replica set name
replIndexPrefetch=_id_only3. Distribute the Config File
# scp /etc/mongod.conf node3:/etc
# scp /etc/mongod.conf node4:/etc4. Create Data Directories
# mkdir -p /mongodb/data
# chown -R mongod:mongod /mongodb5. Start the Service
# service mongod start
# ss -tnl | grep 27017 # verify listening on port 27017Initialize the Replica Set
# mongo
> rs.initiate()
{ "info2" : "no configuration explicitly specified -- making one", "me" : "node1.scholar.com:27017", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 }
> rs.status()
{ "set" : "testSet", "date" : ISODate("2015-07-13T12:33:27Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "node1.scholar.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1111, "optime" : Timestamp(1436790736,1), "self" : true } ], "ok" : 1 }The node becomes PRIMARY.
Add Secondary Nodes
> rs.add("172.16.10.125")
{ "ok" : 1 }
> rs.add("172.16.10.126")
{ "ok" : 1 }
> rs.status()
... (output shows PRIMARY and SECONDARY members) ...Verify Data Synchronization
On the primary:
> use testdb
> db.students.insert({name:"ZhangSan",age:"21"})
WriteResult({ "nInserted" : 1 })On a secondary (after enabling reads):
> rs.slaveOk()
> use testdb
> db.students.findOne()
{ "_id" : ObjectId("55a3b494ebcafd9edbdfce4d"), "name" : "ZhangSan", "age" : "21" }Attempting writes on a secondary fails, confirming that only the primary accepts writes.
Conclusion
The replica set is now configured; if the primary fails, an automatic election will promote a secondary to primary.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
