1. Fragmentation
Fragmentation is to split and save the data in the database on different machines;
Sharding includes manual sharding and automatic sharding;
MongoDB supports automatic sharding;
Two, mongos
MongoDB needs the routing process mongos, mongos knows where all the data is stored, and the application connects to mongos to send requests. mongos hides the fragmentation details from the application and forwards the request to the corresponding database machine;
3. Chip key
1. The slice key is used as the basis for data splitting, and MongoDB splits the data according to the slice key;
According to the addition and deletion of the number of machines, MongoDB will rebalance the data to make its traffic more balanced;
2. Shard key type
Shard keys include incremental shard keys and random shard keys;
Like “timestamp” is an incremental shard key, the feature is that the data writing will be concentrated on one server, and the data query speed is very fast;
The choice of shard key will affect the operation of data query
4. Create shards
Fragmentation consists of three components, namely: slice, mongos, configuration server;
Slices: collection data containers
mongos: router process
Configuration server: store cluster configuration information
1. Start the configuration server
mkdir -p ~/dbs/config
./mongod –dbpath ~/dbs/config –port 20000
2. Start the configuration server
./mongos –port 3000 –configdb localhost:20000
3. Create shards
mkdir -p ~/dbs/db1
./mongod –dbpath ~/dbs/shard1 –port 10000
4. Add slices
connect mongos
./mongo localhost:30000/admin
Add slices through the addshard command (allowLocal allows configuration locally)
db.runCommand({addshard:”localhost:10000″,allowLocal:true})
{
“added”:”localhost:10000″,
“ok”:true
}
5. Segment the test collection of the database will
First enable the sharding function of the will database
db.runCommand({“enablesharding”:”will”})
Fragment the collection again (based on _id)
db.runCommand({“shardcollection”:”will.test”,”key”:{“_id”:1}})
5. Ensure the robustness of the cluster
1. Multiple configuration servers
Method: Repeat configuration server creation steps;
When starting mongos ./mongos –configdb
localhost:20001, localhost:20002
2. Multiple mongos processes
Method: multi-start
3. Data fault tolerance
Method: create a copy of the data
6. Manage shards
1. View slice
2. View the database list information on the chip
In the result:
_id: database name
partitioned: Whether to enable fragmentation
primary: database base camp
3. View block information