in the MongoDB There is another cluster, the Shard technology, with MySQL the table partition is similar, we know that when the amount of data reaches T level of time, our disk, memory is unbearable, for such a scenario we should deal with.
First, The Shard
MongoDB uses a solution that splits the collection and then splits the data into several slices.
Here I explain to this picture:
face: on behalf of the client, the client must say, you do not shard database shard with me, I told you to do what you do, there is no good to discuss.
MONGOs : First we need to understand the concept of "chip key", that is, what is the basis for splitting a set? To split a collection by what key value ....
Well,MONGOs is a routing server that will distribute the data to the Mongod cluster that it manages, according to the "Slice key" set by the administrator , and the correspondence between the data and the slices and the corresponding configuration information is saved in the config Server " on.
Mongod: An ordinary database instance, if not sharding, we will directly connect to the Mongod.
Second, actual combat
First we're going to prepare 4 mongodb programs, and here I am averaging in C,D,E ,F on disk, of course you can also do multiple folders in the form.
1. Open config server
I've said it before, MONGOs want to put Mongod between the configuration and the Config server inside, of course first open it, I set up here 2222 Port.
2. Open MONGOs Server
It's important to note that we're opening the MONGOs , not Mongod , while specifying the next Config server, I'll open it here. D on the plate MongoDB , the Port 3333 .
3. Start mongod Server
for the Shard, that is, to add a piece, here to open E , F Disk of MongoDB , the port is: 4444 , 5555 .
4. Service Configuration
Haha, is not very excited, and the last bit of configuration we can do it.
(1) as you can see in the previous diagram, we Client directly with MONGOs to deal with, it means we're connecting. MONGOs server, and then the 4444 , 5555 of the Mongod Give MONGOs, adding shards is also Addshard () .
It is important to note that the Addshard , we can also add replica sets to achieve higher stability.
(2) The pieces are clustered, but MONGOs do not know how to slice the data, which is what we said earlier, the chip key, in MongoDB to do two steps in setting the Tablet key
①: Open the database sharding function, the command is very simple enablesharding (), I'll open it here . Test database.
②: Specifies the slice key for the Shard in the collection, where I specify Person.name field.
5. View Effects
Well, now that our Shard operation is all over, we then insert a 10w record into MongoDB via mongos and then Printshardingstatus command to view MongoDB data fragmentation conditions.
Here are the main points to see three information:
① Shards We clearly see that it is not divided into two pieces,shard0000 and shard0001.
② databases: Here's a partitioned field that indicates whether the partition is partitioned, so it's clear that test is partitioned.
③ chunks: This is interesting, we found that the collection was cut into four segments:
Infinity-- jack0,jack0 --jack234813,jack234813- -jack9999,jack9999-Infinity.
The partitioning situation is:3:1, which can be seenfrom the back of the on shardxxxx .
Sixth Day Shard Technology