EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

MongoDB Sharding

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » MongoDB Tutorial » MongoDB Sharding

MongoDB Sharding

Introduction to MongoDB Sharding

MongoDB Sharding is a method that was used to distribute the data across multiple machines; basically, sharding is used to deploy large data set with high throughput. A single server is not handling the large data set and high throughput, to increase the high throughput from the database system we have use sharding. It consists of three components config servers, mongos and shard, MongoDB shard consisting of data at a collection level. Sharding is nothing but a process used to store data across multiple machines, and it is an approach to meet the demand for data growth.

Syntax

Below is the syntax to implement sharding:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

1. The first step is to create a database of the config server.

# mkdir /data_directory

— Data directory define as MongoDB data storage location.

2. The second step is to start the database server in configuration mode. Below syntax shows to configure database instance.

# mongod –configd server_name: port_no

— Mongod is an instance or daemon service of database.

— Configd is used to configure the new database instance.

— Port no is used to connect the database server.

Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)4 Online Courses | 2 Hands-on Projects | 22+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,421 ratings)
Course Price

View Course

Related Courses
Oracle Training (14 Courses, 8+ Projects)All in One Data Science Bundle (360+ Courses, 50+ projects)SQL Training Program (7 Courses, 8+ Projects)Oracle DBA Database Management System Training (2 Courses)MS SQL Training (13 Courses, 11+ Projects)

3. The third step is to start the MongoDB server by specifying the port number.

# mongos–configd server_name: port_no

— Mongos is used to start the database server by using server name and port no parameter.

4. The fourth step is to connect to mongos instance by using the mongo shell.

# mongo

— Mongo is a shell which was used to connect the mongos instance.

5. The fifth step consists of adding another server to the cluster. Below syntax shows that add server to the sharding cluster.

# sh.addShard (“server_name: port_no”)

— Add shard is defined as adding a new server to the existing sharded cluster.

— Server name and the port number is defined as name and port number of the server which we have added to the sharded cluster.

6. The sixth step is defined as to enable sharding for a specific database. Below syntax shows that add database to the sharding cluster.

# sh.enableSharding (database_name)

— Enable sharding is defined as to enable sharding for the specific database in MongoDB.

— Database name is defined as the name of the database which we have used in sharding.

7. We can also enable sharding at the collection level in MongoDB. Below syntax show as enabling sharding for collection level.

# sh.shardCollection (“db.collection_name”)

— Shard collection is defined as to enable sharding at collection level in MongoDB.

— Collection name is defined as the name of the collection which we have used in sharding.

How does Sharding work in MongoDB?

Below is the working of sharding in MongoDB.

1. MongoDB sharded cluster is consist of main tree components as follows.

  • Shard
  • Mongos
  • Config servers1.

Shard: Shard in MongoDB is basically used to store the data. Shard will provide high availability and data consistency of the database server. We can implement each replica set to have a separate shard. Each shard will contain a subset of sharded data.

Mongos: Mongos in sharding will act as a query router; it will be used to provide the interface between sharded cluster and client applications. Mongos is also known as a query router. Mongos is used to process the operations with shards and return results to the client. We have implemented more than one mongos instance to divide the load or client request.

Config Servers: Config server is used to store the metadata of the cluster server. This metadata contains information of cluster data set mapping. Query router or mongos is used this metadata information to perform operations on specific shards. We can implement three config server sharded clusters in a production environment. 

2. The below figure shows the component description of sharding in MongoDB.

component description of sharding in MongoDB

3. In the above example, two app servers are used, connected to the shard by using a mongos instance.

4. MongoDB is also used the shard key to distribute the collected data across the shards which we have used in sharding.

5. The shard key in MongoDB consists of a field consisting of every document in a target collection.

6. In MongoDB, we have to create sharded and unshared collections. Sharded collections are partitioned in shards cluster.

7. Unsharded collection in MongoDB is basically stored in the primary shard of the sharding cluster.

8. Sharding in MongoDB is required at least two shards to implement sharding in MongoDB.

9. While using multiple mongos or routers will increase the database server’s high availability and scalability.

10. we can create a zone of data, and we have creating zone based on the shard key.

11. We can associate each zone with one or more shards.

12. There are two types of sharding available in MongoDB are as follows.

  • Range-based sharding: Range-based sharding define as in which MongoDB will divide data sets into the different ranges. This sharding is based on the values of shard keys.
  • Hash-based sharding: In hash sharding, it will first calculate hash value after calculating hash value, it will create chunks, based on the hash value.

Examples to Implement of MongoDB Sharding

The below example shows that implement sharding in MongoDB is as follows. We need to add a replica set to the sharding cluster for adding database and collection to the sharding. Below is the procedure to create sharding in MongoDB are as follows.

#1 – Create Admin User

Code:

use admin
db.createUser({user: "shard", pwd: "shard", roles:[{role: "root", db: "admin"}]})

Output:

MongoDB Sharding Example 1

#2 – Generate Key File

Code:

openssl rand -base64 756 > mongo-keyfile
mkdir /opt/mongo
mv ~/mongo-keyfile /opt/mongo
chmod 400 /opt/mongo/mongo-keyfile

Output:

MongoDB Sharding Example 2

#3 – Initialize Config Server

Code:

vi /etc/mongod.conf

Output:

MongoDB Sharding Example 3

rs.initiate{{ _id: “configReplSet”, configsvr: true, members: [{ _id: 0, host: “127.0.0.1:27017”}]}}

Output:

MongoDB Sharding Example 3

#4 – Cgure Query Routeronfi

Code:

vi /lib/system/system/mongos.service
ystemctl stop mongod
systemctl enable mongos.service
systemctl start mongos

Output:

Routeronfi Example 4

To confirm the connection with mongos instance, we need to use the following command are as follows.

Code:

db.isMaster()

Output:

MongoDB Sharding Example 4

#5 – Add Shard to Cluster

Code:

configReplSet:PRIMARY> sh.addShard( "Mongo-1:27017" )

Output:

Cluster Example 5

configReplSet:PRIMARY> sh.addShard( "Mongo-1:27017" )

Output:

Cluster Example 5

Recommended Article

This is a guide to MongoDB Sharding. Here we discuss the Introduction and how Sharding Work in MongoDB and its Syntax along with its examples. You can also go through our other suggested articles to learn more –

  1. MongoDB MapReduce
  2. MongoDB Data Types
  3. MongoDB Geospatial
  4. MongoDB count

MongoDB Training Program (4 Courses, 2 Projects)

4 Online Courses

2 Hands-on Projects

22+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Auto Increment
    • MongoDB Projection
    • Replication in MongoDB
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • MongoDB Limit()
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - MongoDB Training Program (4 Courses, 2 Projects) Learn More