Create a MongoDB Sharding Cluster

Source: Internet
Author: User
Tags mongodb sharding

MongoDB has officially supported Sharding since version 1.6.

MongoDB also released Replica Sets to replace earlier versions of Replica Pairs.

By combining Sharding and Replica Sets, we can build a distributed, highly available, and automatically horizontally Scalable Cluster.

A typical cluster structure is as follows:

A cluster consists of the following three services:

  1. Shards Server: each shard is composed of one or more mongod processes used to store data.
  2. Config Server: used to store the Metadata information of a cluster, including the information of each Shard and chunks.
  3. Route Server: used to provide the routing service, connected by the Client, so that the entire Cluster looks like a single DB Server

In addition, Chunks are continuous data blocks in MongoDB. The default size is 200 MB, and a Chunk is located on one of the Shard servers.

Next, build a Cluster, which consists of four servers, including two Shard, three Config, and one Route

Each Shard is composed of a Replica Set, and each Replica Set is composed of two Mongod nodes and one vote node.

The configuration process is as follows:

1. The four servers start the corresponding Mongod process respectively:
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10000-replSet set1-dbpath/pvdata/mongodb_data-logpath/pvdata/mongodb_log/mongod. log
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10001-replSet set2-dbpath/pvdata/mongodb_data1-logpath/pvdata/mongodb_log/mongod1.log
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10000-replSet set1-dbpath/pvdata/mongodb_data-logpath/pvdata/mongodb_log/mongod. log
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10000-replSet set2-dbpath/pvdata/mongodb_data-logpath/pvdata/mongodb_log/mongod. log
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10001-replSet set1-dbpath/pvdata/mongodb_data1-logpath/pvdata/mongodb_log/mongod1.log
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10000-replSet set2-dbpath/opt/mongodb_data-logpath/opt/mongodb_log/mongod. log

2. Configure two groups of Replica Sets:
Mongo-port 10000
Config = {_ id: 'set1', members :[
{_ Id: 0, host: '192. 168.95.216: 100 '},
{_ Id: 1, host: '192. 168.95.217: 100 '},
{_ Id: 1, host: '192. 168.95.218: 100', arbiterOnly: true}
Rs. initiate (config)
Rs. status ()
Mongo-port 10000
Config = {_ id: 'set2', members :[
{_ Id: 0, host: '192. 168.95.218: 100 '},
{_ Id: 1, host: '192. 168.95.20.: 100 '},
{_ Id: 1, host: '192. 168.95.216: 100', arbiterOnly: true}
Rs. initiate (config)
Rs. status ()

Note: The Mongod corresponding to 10001 on the two servers is only responsible for vote election of a new master after a node is down. They do not store data backup.

3. Configure three Config Servers:

Mongod-configsvr-fork-logpath/pvdata/mongodb_log/config. log-dbpath/pvdata/mongodb_config_data-port 20000

4. Configure one Route Server:
/Usr/local/mongodb/bin/mongos-fork-chunkSize 1-configdb " 20000,192.168 .95.217: 20000,192.168 .95.218: 20000"-logpath/pvdata/mongodb_log/mongos. log

The chunkSize parameter is used to set the chunk block size. For testing, set it to 1 MB.

  • 1
  • 2
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.