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

How to create a sharding set by using sharding cluster in mongodb

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

It is believed that many inexperienced people don't know what to do about how to use sharding cluster to create sharding collection in mongodb. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. To create a database, just use it directly. If not, if you create a collection, he will create it automatically!

Use liuwenhe

two。 Insert data, or automatically create a hezi collection if it does not exist

For (var I = 0; I

< 100; i++) { db.hezi.insert({name: i}); } 3.为hezi集合的name列添加索引,因为如果要以name列作为分片键,需要有索引; 如果集合是空的,可以不创建索引直接进行下一步的分片,会自动创建索引;如果集合不为空,必须为分片建创建索引才行!,如果你要以hash规则分片,需要创建hash索引! mongos>

Db.hezi.createIndex ({"name": 1})

Mongos > db.hezi.createIndex ({name:'hashed'}); create hash index

4. Open sharding for the database so that the collection under the database liuwenhe can be sliced!

Mongos > sh.enableSharding ("liuwenhe")

5. Open the fragmentation of the collection hezi!

1: ascending order,-1 descending order, hashed: hash distribution

Sh.shardCollection ("liuwenhe.hezi", {name:1})-value-based fragmentation

Sh.shardCollection ("liuwenhe.hezi", {"name": "hashed"})-hash rule slicing

6. Check whether the collection is enabled for sharding:

Mongos > use liuwenhe

Mongos > db.hezi.stats (). Sharded

True

7.sh.status () to view the information of the shards:

You can see that the main shard of the liuhe library is s1092111427021, and then the sharding key of the liuhe.hezi collection is the ascending order of the name column. Liuhe.hezi this collection currently has only one chunk, on the s1092111427021 node, so at this time you go to other nodes, for example, s1092111427020, there is no such library and collection. When you insert data into liuhe.hezi, it is not possible to have this collection in other shards until chunk migration is triggered!

Mongos > sh.status ()

{"_ id": "liuwehehe", "primary": "s1092111427021", "partitioned": true, "version": {"uuid": UUID ("46373baa-3f93-42c3-a7fd-51225ef00636"), "lastMod": 1}}

Liuwenhe.hezi

Shard key: {"name": 1}

Unique: true

Balancing: true

Chunks:

S1092111427021 1

{"name": {"$minKey": 1}}-- > {"name": {"$maxKey": 1}} on: s1092111427021 Timestamp (1,0)

8. When the data exceeds 1 chunk, it is evenly distributed over multiple nodes, as shown below:

Keep inserting data until chunk migration is triggered:

For (var I = 0; I

< 1000000000000000; i++) { db.hezi.insert({name: i}); } 不断查看分片集群状态,如下所示三个节点上基本均匀的分布了 20、19、20个chunk: mongos>

Sh.status ()

Liuwenhe.hezi

Shard key: {"name": 1}

Unique: false

Balancing: true

Chunks:

S1092111427019 20

S1092111427020 19

S1092111427021 20

After reading the above, have you mastered how to use sharding clusters to create sharding collections in mongodb? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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