Updated February 27, 2023
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:
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.
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.
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:
#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:
#3 – Initialize Config Server
Code:
vi /etc/mongod.conf
Output:
rs.initiate{{ _id: “configReplSet”, configsvr: true, members: [{ _id: 0, host: “127.0.0.1:27017”}]}}
Output:
#4 – Cgure Query Routeronfi
Code:
vi /lib/system/system/mongos.service
ystemctl stop mongod
systemctl enable mongos.service
systemctl start mongos
Output:
To confirm the connection with mongos instance, we need to use the following command are as follows.
Code:
db.isMaster()
Output:
#5 – Add Shard to Cluster
Code:
configReplSet:PRIMARY> sh.addShard( "Mongo-1:27017" )
Output:
configReplSet:PRIMARY> sh.addShard( "Mongo-1:27017" )
Output:
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 –