Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of mongodb slicing Technology

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you the "sample analysis of mongodb sharding technology", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of mongodb sharding technology".

There is another kind of cluster in mongodb, that is, slicing technology. When the amount of data reaches T level, our disks and memory will be overwhelmed. How should we deal with such a scenario?

One: slice

Mongodb uses a solution that splits the collection and then distributes the split data over several slices.

Let me explain to this picture:

Face: on behalf of the client, the client must say that it has nothing to do with me whether your database is fragmented or not. I can do whatever I tell you to do. There is nothing to discuss.

Mongos: first of all, we need to understand the concept of "slice key", that is, what is the basis for splitting a collection? According to what key value to split the collection.

All right, mongos is a routing server. According to the "chip key" set by the administrator, it will distribute the data to the mongod cluster managed by itself.

The corresponding relationship of the fragments and the corresponding configuration information are saved on the "config server".

Mongod: an ordinary database instance. If it is not shredded, we will connect to mongod directly.

Two: actual combat

First of all, we prepare four mongodb programs, I here are all distributed on the C-Magi Djue E, F disk, of course, you can also do the form of multiple folders.

1: start the config server

As mentioned earlier, mongos wants to put the configuration between mongod into the config server, of course, open it first, I will set up port 2222 here.

2: start the mongos server

It should be noted here that we open mongos, not mongod, and specify the config server. Here, I will open mongodb on D disk, port 3333.

3: start the mongod server

For slicing, it is time to add slices. Here, the mongodb of E and F disks is opened, and the port is 4444 and 5555.

4: service configuration

Haha, are you very excited? we are still short of the last configuration and we can finish it.

As can be seen in the previous figure, our client deals directly with mongos, which means that we need to connect to the mongos server, and then transfer 4444 to 5555 mongod

Give it to mongos, and add shards, that is, addshard ().

It is important to note that in addshard, we can also add replica sets to achieve higher stability.

The slice has been clustered, but mongos doesn't know how to split the data, that is, the slice key we mentioned earlier. Setting the slice key in mongodb takes two steps.

①: enable database sharding, the command is very simple enablesharding (), here I will open the test database.

②: specify the shard key in the collection, which I'll specify as the person.name field here.

5: view the effect

All right, now that our slicing operation is all over, let's insert a 10w record into mongodb through mongos, and then use the printShardingStatus command

Check the data sharding of mongodb.

Here are three main points of information:

① shards: we clearly see that it has been divided into two films, shard0000 and shard0001.

② databases: there is a partitioned field indicating whether or not to partition. Here you can clearly see that test has been partitioned.

③ chunks: this is interesting. We found that the collection was cut into four segments:

Infinitesimal-- jack0,jack0-- jack234813,jack234813--jack9999,jack9999-- infinity.

The partition is 3:1, which can also be seen from the on shardXXXX at the back.

The above is all the contents of this article "sample Analysis of mongodb slicing Technology". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report