Another cluster exists in mongodb, which is the sharding technology. Similar to SQL server table partitions, we know that when the data volume reaches T level, our disks and memory
We can't afford it. How can we deal with such a scenario.
I. Parts
Mongodb uses a solution to split a set and evenly distribute the split data to several slices.
I will explain this figure below:
FACE: it represents the client. The client must say that it doesn't matter if your database sharding is not sharded. I told you to do what you did. There is nothing to discuss.
Mongos: first, we need to understand the concept of "chip key". That is to say, what is the basis for splitting a set? Sharding set based on key values ....
Well, mongos is a routing server. It distributes data to its managed mongod cluster based on the "chip key" set by the Administrator.
The relationship with the slice and the corresponding configuration information are saved on the "config server.
Mongod: a common database instance. If it is not sharded, it will be directly connected to mongod.
Ii. Practice
First, we prepare four mongodb programs. Here I share them on C, D, E, and F disks. Of course, you can also create multiple folders.
1: Enable the config Server
As mentioned earlier, mongos should put the Configuration between mongod In the config server. Of course, it should be enabled first. I will set up port 2222 here.
2: Enable the mongos Server
Note that we have enabled mongos, not mongod, and specify the config server. Here I will enable mongodb on the d disk and port 3333.
3: Start the mongod Server
For slice, that is, to add a slice. Here, the mongodb of drive E and drive F is enabled, and the port is 4444,5555.
4. Service Configuration
Haha, aren't you very excited? We can just complete the last configuration.
<1> As shown in the previous figure, we can see that our client is directly dealing with mongos, Which means we want to connect to the mongos server, and then change the mongod of 4444,5555
To mongos, add the shard, that is, addshard ().
Note that we can also add replica sets in addshard to achieve higher stability.
<2> the sliced data is already in the cluster, but mongos does not know how to split the data, that is, the previous piece key. Set the piece key in mongodb for two steps.
①: Enable the database sharding function. The command is simple: enablesharding (). Here I will enable the test database.
②: Specify the part key in the set. Here I will specify it as the person. name field.
5: view results
Now, all our sharding operations are complete. Next we insert 10 million records to mongodb through mongos, and then run the printShardingStatus command.
View the data shards of mongodb.
Here we mainly look at three points:
① Shards: we can clearly see that there are no shard0000 and shard0001.
② Databases: here there is a partitioned field indicating whether to partition. Here we can clearly see that test has been partitioned.
③ Chunks: This is very interesting. We found that the set was cut into four sections:
Infinitely small -- jack0, jack0 -- jack234813, jack234813 -- jack9999, jack9999 -- infinity.
The partition condition is: 3: 1, which can be seen from the on shardXXXX.