MongoDB manual pre-sharding (9)

MongoDB manual pre-sharding (9)

1. The book is linked to the above

In the last article, I introduced building mongodb shards, but since chunks store data on the primary shard first, then compare the number of chunks on the primary shard with the number of chunks on other shards. If the number of chunks on the primary shard is If it is greater than the number of chunks on other shards, move the chunks on the primary shard to other shards. This will cause IO busy between different shard servers. In this article, manual sharding is used to solve this problem.

The address of the previous article: https://blog.csdn.net/m0_38039437/article/details/80234567

Second, start by saying

1. Before sharding, declare the database and table of the shard. For example, shard the user table of the shop database, and the shards are split according to the user_id field.

sh.shardCollection('shop.user',{user_id:1})

2. Manual sharding

①. Sharding syntax: sh.splitAt(database.table,{split strategy})

for(var i=1;i<=10;i++){sh.splitAt('shop.user',{user_id:i*1000})}

Divide the user table of the shop database according to the number of user_id fields in advance. Every 1K pieces of data are divided into a chunk for a total of 10 times. Such a boundary is cut into chunks (although the chunks are empty), and these chunks will be evenly moved to each slice. .

2. Check the status of chunk allocation after manual fragmentation. Execute: sh.status()


3. Insert data and observe the data stored on each shard

for(var i=1;i<=50000;i++){db.user.insert({user_id:i,name:'hell user'+i})}

Log in to the shard server at each point to view the amount of data, and find that the data has been stored according to the pre-defined rules.

27017 Data of node fragmentation


27018 data on node shards


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325978166&siteId=291194637