How to Build a High‑Availability MongoDB Sharded Replica Set Cluster on Ubuntu
This step‑by‑step tutorial shows how to set up a highly available MongoDB cluster with sharding and replica sets on Ubuntu 18.04, covering key concepts, environment preparation, configuration of config servers, shard replica sets, the mongos router, enabling sharding, testing data distribution, and final deployment tips.
MongoDB is a widely used NoSQL database, now ranking among the top six. This guide explains how to build a high‑availability MongoDB cluster with sharding and replica sets on Ubuntu 18.04.
Key Concepts
1. mongos – the entry point for client requests, routing queries to the appropriate shard. 2. Config server – stores metadata for routing and sharding; runs as a replica set for reliability. 3. Shard – a partition of data stored on separate machines to scale storage and load. 4. Replica set – provides redundancy for each shard, preventing data loss. 5. Arbiter – a lightweight MongoDB instance that participates in elections without storing data.
Environment Preparation
Three Ubuntu 18.04.1 LTS servers (10.20.71.135, 10.20.73.182, 10.20.70.61) with the package mongodb-org-server_4.0.6_amd64.deb are used. Directories for conf, mongos, configsvr, and each shard ( shard1, shard2, shard3) are created under /usr/local/mongodb.
Install MongoDB Source
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt-get update
# sudo apt-get install -y mongodb-org
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selectionsConfigure Config Server
Create configsrv.conf:
storage:
dbPath: /data/mongodb/config/data
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /data/mongodb/config/log/configsrv.log
net:
port: 21000
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
keyFile: /data/mongodb/conf/key
authorization: enabled
replication:
replSetName: configs
sharding:
clusterRole: "configsvr"Service file mongod-configsrv.service (systemd):
[Unit]
Description=MongoDB Config Server
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
ExecStart=/usr/bin/mongod --config /data/mongodb/conf/configsrv.conf
PIDFile=/data/mongodb/config/log/configsrv.pid
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
LimitMEMLOCK=infinity
TasksMax=infinity
TasksAccounting=false
[Install]
WantedBy=multi-user.targetStart and enable:
systemctl daemon-reload
systemctl start mongod-configsrv.service
systemctl enable mongod-configsrv.serviceInitialize the config replica set:
mongo --port 21000
config = {_id: "configs", members: [{_id:0, host:"10.20.71.135:21000"},{_id:1, host:"10.20.73.182:21000"},{_id:2, host:"10.20.70.61:21000"}]}
rs.initiate(config)Configure Shard Replica Sets
Each shard (shard1, shard2, shard3) has its own shardX.conf (replace X with 1‑3) with appropriate dbPath, port (27017, 27018, 27019), and the same security settings. Example for shard1:
storage:
dbPath: /data/mongodb/shard1/data
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /data/mongodb/shard1/log/shard1.log
net:
port: 27017
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
keyFile: /data/mongodb/conf/key
authorization: enabled
replication:
replSetName: shard1
sharding:
clusterRole: "shardsvr"Corresponding systemd service mongod-shard1.service (similarly for shard2 and shard3) uses --config /data/mongodb/conf/shard1.conf. Start and enable each service, then initialize each replica set:
# For shard1
mongo --port 27017
use admin
config = {_id:"shard1", members:[{_id:0, host:"10.20.71.135:27017"},{_id:1, host:"10.20.73.182:27017"},{_id:2, host:"10.20.70.61:27017", arbiterOnly:true}]}
rs.initiate(config)Repeat with ports 27018 and 27019 for shard2 and shard3, adjusting host IPs accordingly.
Configure mongos Router
Create mongos.conf:
systemLog:
destination: file
logAppend: true
path: /data/mongodb/mongos/log/mongos.log
net:
port: 20000
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
keyFile: /data/mongodb/conf/key
clusterAuthMode: keyFile
replication:
localPingThresholdMs: 15
sharding:
configDB: "configs/10.20.71.135:21000,10.20.73.182:21000,10.20.70.61:21000"Systemd service mongos.service runs /usr/bin/mongos --config /data/mongodb/conf/mongos.conf. Start and enable:
systemctl daemon-reload
systemctl enable mongos.service
systemctl start mongos.serviceEnable Sharding
Connect to mongos and add each shard replica set:
mongo --port 20000
use admin
db.auth("weadmin","123456")
sh.addShard("shard1/10.20.71.135:27017,10.20.73.182:27017,10.20.70.61:27017")
sh.addShard("shard2/10.20.71.135:27018,10.20.73.182:27018,10.20.70.61:27018")
sh.addShard("shard3/10.20.71.135:27019,10.20.73.182:27019,10.20.70.61:27019")
sh.status()Enable sharding for database test and collection table1 on field id:
db.runCommand({enablesharding:"test"})
db.runCommand({shardcollection:"test.table1", key:{id:1}})Testing
Create admin and test users, insert 5,000 documents into test.table1, then verify distribution:
# Create admin user
mongo --port 20000 -u weadmin -p 123456 --authenticationDatabase admin
use admin
db.createUser({user:"weadmin", pwd:"123456", roles:[{role:"userAdminAnyDatabase", db:"admin"},{role:"clusterManager", db:"admin"}], passwordDigestor:"server"})
# Create test user and insert data
mongo --port 20000 -u test -p 123456 --authenticationDatabase test
db.createUser({user:"test", pwd:"123456", roles:[{role:"dbOwner", db:"test"}], passwordDigestor:"server"})
for (var i=1;i<=5000;i++) { db.table1.save({id:i, name:"shuke"}); }
# Verify sharding
mongo --port 20000 -u weadmin -p 123456 --authenticationDatabase admin
use admin
db.auth("weadmin","123456")
sh.enableSharding("test")
sh.shardCollection("test.table1", {id:1})
use test
db.table1.stats()Conclusion
Manually deploying a MongoDB sharded replica set cluster is complex; using a managed cloud service or an automation tool such as ansible-mongodb-cluster can simplify the process.
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.
