Knowledge Point: mongoDB Sharding is an automatic data Sharding. Data is automatically migrated in different data servers. The migration trigger conditions include the data volume and access traffic.
1. Create a sharding cluster in two parts: one is to start the corresponding mongoDB service, and the other is to decide how to split the data.
2. A sharding cluster that can provide services mainly consists of three parts:
(1) configuration server: A general mongoDB service process. It stores the correspondence between cluster data and slices. Configuration information is deployed.
(2) Routing Server: route request and forwarding result set. The Routing Server knows where the corresponding data is stored. It caches configuration server information but does not implement it, and synchronizes data from the configuration server.
(3) data server: a service process that truly stores data. It is a general mongoDB service process.
3. Create a sharded Cluster
1. Start the configuration service process. Because the Routing Server needs to synchronize data from the configuration server, it is necessary to start the routing service process early.
Mongod -- dbpath/exports/dbs/config -- port 20000
2. Start the Routing Server and specify the corresponding configuration server location. The routing service process does not need to specify the data file directory. It does not store data, but caches the data of the service process.
Mongos -- port 30000 -- configdb localhost: 20000
3. Start the data server. This is the mongodb process that actually stores data. (Use one at the beginning, and then add)
Mongod -- dbpath/exports/dbs/shard1 -- port 27017
4. Connect to the Routing Server to enable the slice. You must specify a set to a database.
Mongo localhost: 30000/admin
Db. runCommand ({addshard: "localhost: 27017", allowLocal: true });
5. Connect to the Routing Server and specify a set in a database to be split. And specify the shard key.
Db. runCommand ({enablesharding: "foo "});
Db. runCommand ({shardcollection: "foo. bar", key: {_ id: 1 }});