MongoDB sharding on Ubuntu 24.04 LTS

MongoDB sharding on Ubuntu 24.04 LTS

[email protected] ? Chirag's MongoDB DBA Tutorial? ? ? ? ?https://www.chirags.in

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

MongoDB sharding on Ubuntu 24.04 LTS

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

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


要查看或添加评论,请登录

Chitt Ranjan Mahto的更多文章

社区洞察