496 views

1 Answer

answered by

inchirags@gmail.com   Chirag's MongoDB DBA Tutorial         https://www.chirags.in

*****************************************************************************************

* MongoDB sharding on Ubuntu 24.04 LTS *

*****************************************************************************************

https://youtu.be/6NA82pLvqdw

Setting up MongoDB sharding on Ubuntu 24.04 involves configuring multiple MongoDB instances across different servers or containers. Below is a step-by-step guide for setting up MongoDB sharding on Ubuntu 24.04:

Prerequisites

Ubuntu 24.04 LTS installed on all machines involved in sharding.

Minimum three servers or virtual machines:

One Config Server (stores metadata about the cluster).

Server IP: 192.168.224.129

Two or more Shard Servers (stores the actual data).

Server IP: 192.168.224.130
Server IP: 192.168.224.131

One Mongos Router (distributes requests to appropriate shards).

Server IP: 192.168.224.132

Firewall: Open ports 27017 for each MongoDB instance and 27019 for config server communication.

Install MongoDB on each server.

Step 1: Install MongoDB on All Servers (192.168.224.129,192.168.224.130,192.168.224.131,192.168.224.132)

1. Import the Public Key

From a terminal, install gnupg and curl if they are not already available:

sudo apt-get install gnupg curl

To import the MongoDB public GPG key, run the following command:

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
   --dearmor

2. Create the List File

Create the list file /etc/apt/sources.list.d/mongodb-org-8.0.list for your version of Ubuntu.

Create the list file for Ubuntu 24.04 (Noble):

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

3.Reload the Package Database

Issue the following command to reload the local package database:

sudo apt-get update

4. Install MongoDB Community Server

You can install either the latest stable version of MongoDB or a specific version of MongoDB.

To install the latest stable version, issue the following

sudo apt-get install -y mongodb-org

5. Enable MongoDB.

sudo systemctl enable mongod

Step 2: Configure Config Servers (192.168.224.129)

Config servers store the metadata for the sharded cluster.

Edit MongoDB Config File (/etc/mongod.conf) on the Config Server:

vi /etc/mongod.conf

# /etc/mongod.conf
net:
  bindIp: 0.0.0.0  # Allows access from other machines
    port: 27019
sharding:
  clusterRole: "configsvr"
replication:
  replSetName: "configReplSet"

Start the Config Server:

sudo systemctl start mongod

Initialize Config Server Replica Set (from MongoDB shell):

mongosh --port 27019

Run the following:

rs.initiate({
  _id: "configReplSet",
  configsvr: true,
  members: [
    { _id: 0, host: "192.168.224.129:27019" }
  ]
});

Step 3: Configure Shard Servers (192.168.224.130 and 192.168.224.131)

Each shard server will store a subset of data for the cluster.

Edit MongoDB Config File (/etc/mongod.conf) on each Shard Server:

--(192.168.224.130)--

vi /etc/mongod.conf

sharding:
  clusterRole: "shardsvr"
replication:
  replSetName: "shardReplSet1"  # Use a unique name for each shard replica set
net:
  bindIp: 0.0.0.0
  port: 27017

Start Each Shard Server:

sudo systemctl start mongod

Initialize Shard Server Replica Sets:

Connect to each shard server’s MongoDB shell:

mongosh --port 27017

Run:

rs.initiate({
  _id: "shardReplSet1",
  members: [
    { _id: 0, host: "192.168.224.130:27017" }
  ]
});

--(192.168.224.131)--

Edit MongoDB Config File (/etc/mongod.conf) on each Shard Server:

vi /etc/mongod.conf

sharding:
  clusterRole: "shardsvr"
replication:
  replSetName: "shardReplSet2"  # Use a unique name for each shard replica set
net:
  bindIp: 0.0.0.0
  port: 27017

Start Each Shard Server:

sudo systemctl start mongod

Initialize Shard Server Replica Sets:

Connect to each shard server’s MongoDB shell:

mongosh --port 27017

Run:

rs.initiate({
  _id: "shardReplSet2",
  members: [
    { _id: 0, host: "192.168.224.131:27017" }
  ]
});

Step 4: Configure the Mongos Router (192.168.224.132)

Mongos acts as a query router for the sharded cluster.

Create a Configuration File for Mongos (/etc/mongos.conf):

vi /etc/mongos.conf

sharding:
  configDB: "configReplSet/192.168.224.129:27019"
net:
  bindIp: 0.0.0.0
  port: 27017

Start the Mongos Process:

sudo mongos --config /etc/mongos.conf

Verify the Mongos Router:

Connect to the Mongos instance:

mongosh --port 27017

If it connects successfully, your Mongos router is working.

Step 5: Add Shards to the Cluster (192.168.224.132)

From the Mongos shell, add each shard:

sh.addShard("shardReplSet1/192.168.224.130:27017");
sh.addShard("shardReplSet2/192.168.224.131:27017");

Repeat this for each shard.

Step 6: Enable Sharding on a Database and Collection

Enable Sharding on a Database:

sh.enableSharding("SalesOrders");

Shard a Collection (specify the shard key):

sh.shardCollection("SalesOrders.products", { shardKeyField: 1 });

Step 7: Verify the Sharding Status

To confirm that sharding is set up, use:

sh.status();

This command will show details about the configuration, shard distribution, and status of the sharded cluster.

MongoDB sharded cluster should now be ready for production use! Let me know if you'd like further details on any specific part.

For any doubts and query, please write on YouTube video comments section.

Note : Flow the Process shown in video.

Please, Subscribe and like for more videos:

https://youtube.com/@chiragstutorial

Don't forget to, Follow, Like,  Share &, Comment

Thanks & Regards,

Chitt Ranjan Mahto "Chirag"

_________________________________________________________________________________________

Note: All scripts used in this demo will be available in our website.

Link will be available in description.

#mongodb 

#mongodbtutorial 

#mongo

#mongosh

#mongodbtutorialforbeginners 

#chiragstutorial 

#chiragsdatabasetutorial 

#chirags 

#chiragsmahto

Install MongoDB 8,Authentication,MongoDB Tutorial,MongoDB Chirags Tutorial,MongoDB DBA Tutorial,Chirags Tutorial,Chirags DBA Tutorial,Chirag Mahto,Chitt Ranjan Mahto,Chirag DBA Tutorial,Database Tutorial,NoSQL Database Tutorial,MongoDB NoSQL Database,MongoDB Install,MongoDB Configuration,MongoDB Access,MongoDB Compas,Mongosh,Rockmongo,Mongo,MongoDB Authorization,MongoDB Authentication,MongoDB Authorisation

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...