Sharding is a MongoDB process to store data-set across different machines. It allows you to perform a horizontal scale of data and to partition all data across independent instances. Sharding allows you to add more machines based on data growth to your stack.
¶Sharding and Replication
Let’s make it simple. When you have collections of music, Sharding
will save and keep your music collections in diferent folders on different instances or replica sets while Replication
is just syncing your music collections to other instances.
¶Threee Sharding Components
- Shard Used to store all data. And in a production environment, each shard is replica sets. Provides high-availability and data consistency.
- Config Server Used to store cluster metadata, and contains a mapping of cluster data set and shards. This data is used by mongos/query server to deliver operations. It’s recommended to use more than 3 instances in production.
- Mongos/Query Router This is just mongo instances runing as application interfaces. The application will make requests to the
mongos
instance, and thenmongos
will deliver the requests using shard key to the shards replica sets.
¶Prerequisites
- 2 centOS 7 server as Config Replica Sets
- 10.0.15.31 configsvr1
- 10.0.15.32 configsvr2
- 4 CentOS 7 server as Shard Replica Sets
- 10.0.15.21 shardsvr1
- 10.0.15.22 shardsvr2
- 10.0.15.23 shardsvr3
- 10.0.15.24 shardsvr4
- 1 CentOS 7 server as mongos/Query Router
- 10.0.15.11 mongos
- Root privileges
- Each server connected to another server
¶Step 1 - Disable SELinux and Configure Hosts
In this tutorial, we will disable SELinux. Change SELinux configuration from ‘enforcing’ to ‘disabled’.
Connect to all nodes through OpenSSH.
1 | ssh root@SERVERIP |
Disable SELinux by editing the configuration file.
1 | vim /etc/sysconfig/selinux |
Change SELINUX value to ‘disabled’.
1 | SELINUX=disabled |
Save and exit.
Next, edit the hosts file on each server.
1 | vim /etc/hosts |
Paste the following hosts configuration:
1 | 10.0.15.31 configsvr1 |
Save and exit.
Now restart all servers using the reboot command.
reboot
¶Step 2 - Install MongoDB on all instances
We will use the latest MongoDB version (3.4) for all instances. Add new MongoDB repository by executing the following command:
1 | cat <<'EOF' >> /etc/yum.repos.d/mongodb.repo |
Now install MongoDB 3.4 from mongodb repository using the following yum command.
1 | sudo yum -y install mongodb-org |
After mongodb is installed, you can use the ‘mongo’ or ‘mongod’ command.
1 | mongod --version |
¶Step 3 - Create Config Server Replica Set
In the ‘prerequisites’ section, we’ve already defined config server with 2 machines ‘configsvr1’ and ‘configsvr2’. And in this step, we will configure it to be a replica set.
If there is a mongod service running on the server, stop it using the systemctl command.
1 | systemctl stop mongod |
Edit the default mongodb configuration ‘mongod.conf’ using the Vim editor.
1 | vim /etc/mongod.conf |
Change the DB storage path to your own directory. We will use ‘/data/db1’ for the first server, and ‘/data/db2’ directory for the second config server.
1 | storage: |
Change the value of the line ‘bindIP’ to your internal network addres - ‘configsvr1’ with IP address 10.0.15.31, and the second server with 10.0.15.32.
1 | bindIP: 10.0.15.31 |
On the replication section, set a replication name.
1 | replication: |
And under sharding section, define a role of the instances. We will use these two instances as ‘configsvr’.
1 | sharding: |
Save and exit.
Next, we must create a new directory for MongoDB data, and then change the owner of that directory to ‘mongod’ user.
1 | mkdir -p /data/db1 |
After this, start the mongod service with the command below.
1 | mongod --config /etc/mongod.conf |
You can use the netstat command to check whether or not the mongod service is running on port 27017.
1 | netstat -plntu |
Configsvr1 and Configsvr2 are ready for the replica set. Connect to the ‘configsvr1’ server and access the mongo shell.
1 | ssh root@configsvr1 |
Initiate the replica set name with all configsvr member using the query below.
1 | rs.initiate( |
If you get a results { "ok" : 1 }
, it means the configsvr is already configured with replica set.
and you will be able to see which node is master and which node is secondary.
1 | rs.isMaster() |
The configuration of Config Server Replica Set is done.
¶Step 4 - Create the Shard Replica Sets
In this step, we will configure 4 ‘centos 7’ servers as ‘Shard’ server with 2 ‘Replica Set’.
- 2 server - ‘shardsvr1’ and ‘shardsvr2’ with replica set name: ‘shardreplica01’
- 2 server - ‘shardsvr3’ and ‘shardsvr4’ with replica set name: ‘shardreplica02’
Connect to each server, stop the mongod service (If there is service running), and edit the MongoDB configuration file.
1 | systemctl stop mongod |
Change the default storage to your specific directory.
1 | storage: |
On the ‘bindIP’ line, change the value to use your internal network address.
1 | bindIP: 10.0.15.21 |
On the replication section, you can use ‘shardreplica01’ for the first and second instances. And use ‘shardreplica02’ for the third and fourth shard servers.
1 | replication: |
Next, define the role of the server. We will use all this as shardsvr instances.
1 | sharding: |
Save and exit.
Now, create a new directory for MongoDB data.
1 | mkdir -p /data/db1 |
Start the mongod service.
1 | mongod --config /etc/mongod.conf |
Check MongoDB is running using the following command:
1 | netstat -plntu |
You will see MongoDB is running on the local network address.
Next, create a new replica set for these 2 shard instances. Connect to the ‘shardsvr1’ and access the mongo shell.
1 | ssh root@shardsvr1 |
Initiate the replica set with the name ‘shardreplica01’, and the members are ‘shardsvr1’ and ‘shardsvr2’.
1 | rs.initiate( |
If there is no error, you will see results as below.
Results from shardsvr3 and shardsvr4 with replica set name shardreplica02
.
Redo this step for shardsvr3 and shardsvr4 servers with different replica set name shardreplica02
.
Now we’ve created 2 replica sets - shardreplica01
and shardreplica02
- as the shard.
¶Step 5 - Configure mongos/Query Router
The ‘Query Router’ or mongos is just instances that run ‘mongos’. You can run mongos with the configuration file, or run with just a command line.
Login to the mongos server and stop the MongoDB service.
1 | ssh root@mongos |
Run mongos with the command line as shown below.
1 | mongos --configdb "replconfig01/configsvr1:27017,configsvr2:27017" |
Use the ‘–configdb’ option to define the config server. If you are on production, use at least 3 config servers.
You should see results similar to the following.
1 | Successfully connected to configsvr1:27017 |
mongos instances are running.
¶Step 6 - Add shards to mongos/Query Router
Open another shell from the previous step, connect to the mongos server again, and access the mongo shell.
1 | ssh root@mongos |
Add shard server with the sh mongodb query.
For shardreplica01
instances:
1 | sh.addShard( "shardreplica01/shardsvr1:27017") |
For shardreplica02
instances:
1 | sh.addShard( "shardreplica02/shardsvr3:27017") |
Make sure there is no error and check the shard status.
1 | sh.status() |
You will see sharding status similar to the way what the following screenshot shows.
We have 2 shard replica set and 1 mongos instance running on our stack.
¶Step 7 - Testing
To test the setup, access the mongos server mongo shell.
1 | ssh root@mongos |
Enable Sharding for a Database
Create a new database and enable sharding for the new database.
1 | use lemp |
Now see the status of the database, it’s has been partitioned to the replica set ‘shardreplica01’.
Enable Sharding for Collections
Next, add new collections to the database with sharding support. We will add new collection named ‘stack’ with shard collection ‘name’, and then see database and collections status.
1 | sh.shardCollection("lemp.stack", {"name":1}) |
New collections ‘stack’ with shard collection ‘name’ has been added.
Add documents to the collections ‘stack’.
Now insert the documents to the collections. When we add documents to the collection on sharded cluster, we must include the ‘shard key’.
In the example below, we are using shard key ‘name’, as we added when enabling sharding for collections.
1 | db.stack.save({ |
As shown in the following screenshots, documents have been successfully added to the collection.
If you want to test the database, you can connect to the replica set shardreplica01
PRIMARY server and open the mongo shell. I’m logging in to the ‘shardsvr2’ PRIMARY server.
1 | ssh root@shardsvr2 |
Check database available on the replica set.
1 | show dbs |
You will see that the database, collections, and documents are available in the replica set.
MongoDB Sharded Cluster on CentOS 7 has been successfully installed and deployed.