Distributed deployment of MongoDB

Source: Internet
Author: User
Tags database sharding

  First, the concept of fragmentation

Sharding (sharding) refers to the process of splitting data into different machines based on the chip key. This makes it possible to store large amounts of data and handle higher loads without the need for powerful machines such as functionality or configuration.

  Second, the principle and thought of sharding

   The basic idea of mongodb shards is to cut the set into small chunks. The blocks are scattered across several slices, each of which is only part of the total data.

For the client, there is no need to know that the data is being split, and there is no need to know which shard of the server corresponds to which data. The data needs to run a routing process before the Shard, with the process named MONGOs. This router knows where all the data is stored and knows the correspondence between the data and the slice. To the client, it only knows that a common mongod is connected, and in the process of requesting the data, it is routed to the slice on which the target data resides through the correspondence between the data and the slice on the router, and if the request responds, the router collects it back to the client.

   

  Second, the simple implementation of the Shard

   (1) The concept of the chip key

When you set up a shard, you need to select a key from the collection and use the value of the key as the basis for splitting the data. This key is called the Slice key (Shard key).

{department: "IT", Name: "Zhangsan"},{department: "HR", Name: "Lisi"},{department: "Support", Name: "Zhaowu"}

Take this data as an example, the name of the staff and the Department, if we set up the Department (department) as the chip key, then the first piece may hold the name of the letter a-f beginning of the department, the second piece of the name to g~p the beginning of the department, the third deposit q~z, and so on. With the addition or deletion of slices, MongoDB will rebalance the data so that each piece of traffic is more balanced and the amount of data is within reasonable range.

(2) Local simulation implementation

A, first Mongod open the server 3 nodes, the port is 8080,8081,8082

b, open config server. MONGOs to put the configuration between the Mongod in the config server, so first open it, here use 8083 port. The command is:

Mongod--dbpath E:\sharding\config_node--port 8083

C, turn on the MONGOs server. Port 8084, specifying the next config server. The command is:

MONGOs--port 8084--configdb=127.0.0.1:8083

     D, routing specifies the server-side node. The client deals directly with MONGOs, which means that we want to connect the MONGOs server, and then give 8080,8081,8082 Mongod to MONGOs, adding shards that is Addshard (). You need to go into the admin database of the routing node to configure. Allowlocal represents the server-side node that can be directly connected by the client without routing.

Db.runcommand ({"Addshard": "127.0.0.1:8080", allowlocal:true})

Db.runcommand ({"Addshard": "127.0.0.1:8081", allowlocal:true})

Db.runcommand ({"Addshard": "127.0.0.1:8082", allowlocal:true})

E, open the database sharding function, command enablesharding (), and specify the database name, the following specifies the test database for each Mongod

Db.runcommand ({"enablesharding": "Test"})

F, specifies the slice key for the Shard in the collection, which is specified as the Company.department key.

Db.runcommand ({"Shardcollection": "Company.department", "key": {"depatment": 1}})

G, view the data saved by each node by inserting 1 million data into the MONGOs node.

h, by inserting a piece of data into the MONGOs to see which slice the data is assigned to

Distributed deployment of MongoDB

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: info-contact@alibabacloud.com 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.