1. Introduction of Shards
Sharding (sharding) is the process of splitting data and spreading it across different machines. MongoDB supports automatic sharding, which allows the database schema to be invisible to the application. For applications, it seems as if you are always using a single-machine MongoDB server, on the other hand, MongoDB automatically handles the distribution of data on shards and makes it easier to add and remove shards.
Keep in mind that replication allows multiple servers to have the same copy of the data, and each server is mirrored by other servers, and each shard has a different subset of data than the other shards.
Typically, shards can be used to:
- Increase the available memory
- Increase the available disk space
- Reduce the load on a single server
- Handle throughput that cannot be sustained by a single Mongod server
2. MongoDB Shard Cluster composition
MongoDB's shard cluster consists of the following parts:
Shard: Each shard contains a subset of the Shard data, each shard can be deployed as a replica set
MONGOs: As a query router, provides an interface between a client application and a shard cluster.
config servers: Configures metadata and configuration information for the server storage cluster. beginning with MongoDB version 3.4, the configuration server must deploy a replica set.
The configuration of the Shard cluster is divided into Production and development configurations, one for the production environment, and the other for the development or test environment.
2.1. Production configuration (production environment)
In a production cluster (production cluster), if the collection data is redundant and the system is configured enough to produce a fragmented cluster deployment, there are a few things to be satisfied:
- Deploying a configuration server with three-member replica set (deploy Config Servers as a 3 member replica set)
- Deploy each shard can be a replica set of three members (deploy each Shard as a 3 member replica set)
- Deployment can have one or more routes (deploy-one or more MONGOs routers)
The structure diagram is as follows:
2.2. Development configuration (development environment)
Test or development environment, you can deploy a shard cluster with the fewest components that contain the following components:
- Replica set configuration server with a member (a replica set config server with one member)
- At least one shard as a member replica set (at least one shard as a single-member replica set)
- A MONGOs instance (one MONGOs instance)
The structure diagram is as follows:
Note: Use the test cluster structure for testing or development only.
Here is an example of a simpler development Configuration.
3. Example
Next we use the example to specify the process of the Shard, where three servers are prepared, respectively, 218.245.4.11, 218.245.4.12, 218.245.4.13.
218.245.4.11 creates a member replica set on the server as a configuration server (config servers). In the actual project, a replica set of three members is required.
218.245.4.12 acts as a shard (shard) server and also creates a replica set of members on that server. In a real-world project, you can create multiple shards, each of which can also be a replica set.
218.245.4.13 as a MONGOs server. In a real project, you can create multiple MONGOs.
3.1 Creating the Configuration server replica set (Create the config Replica set)
3.1.1 Starts a member of the configuration Server replica set (start a member of the Config server replica set), where the replica set named "Configrep" is defined, the--CONFIGSVR parameter needs to be specified, and the actual project is--b IND_IP specify a specific IP address
Mongod--port 27017--bind_ip 218.245.4.11--configsvr
3.1.2 Connect to one of the configuration servers, initialize the replica set with Rs.initiate ()
Rs.initiate ( { _id: "Configrep", configsvr:true, Members : [ {_id:0, Host: "218.245.4.11:27017" } ] })
3.2 Creating a Shard replica set (Create the Shard Replica sets)
3.2.1 Starts a member of the Shard Server replica set (start a member of the Shard replica set), which defines the replica set with the name "Shardrep" and needs to specify the--SHARDSVR parameter. This can be used instead of a replica set, which can be a single Mongod server.
Mongod--port 27017--bind_ip 218.245.4.12--shardsvr
3.2.2 Connecting to the Shard server, initializing the replica set with Rs.initiate ()
Rs.initiate ( { _id: "Shardrep" members : [ {_id:0, Host: "218.245.4.12:27017"} ] })
3.3 Starting MONGOs Instances
MONGOs--configdb configrep/218.24.5.4.11:27017--port 27017
In MongoDB version 3.4, the value of the parameter--configdb must contain the name of the replica set, which is why the configuration server is being deployed as a replica set for the MongoDB 3.4 version.
3.4 Adding shards to a cluster
The operation of adding shards must be operated under MONGOs, that is, to connect the MONGOs first. You can add shards using the Sh.addshard () method
Sh.addshard ("shardrep/218.245.4.12:27017")
If the Shard does not have a replica set, you can add shards like this
Sh.addshard ("218.245.4.12:27017")
3.5 Setting up a database for a shard store
MongoDB uses the Sh.enablesharding () method to set up a database that requires shard storage, connected to MONGOs.
Sh.enablesharding ("Liruihuan")
3.6 Set up a collection that requires shards
Before setting up a shard collection, it is necessary to understand the concept of the slice key (Shard key) . What does MongoDB do to a collection of shards? This is the time to use the tablet key. The slice key is a key to the collection, and MongoDB splits the data according to this key. For example, if you choose to Shard based on "name", MongoDB pairs shards according to different names: "Name1" to "name100" in the first piece, "name101" to "name200" in the second piece, and so on.
To set the Shard, you need to use the Sh.shardcollection () method to specify the collection and the chip key needs to be fragmented, the following example is to the user collection of shards, the key is name, connected to the MONGOs.
Sh.shardcollection ("Liruihuan.user", {name:1})
Note that if this collection contains data, before using Shardcollection (), we must use the Db.collection.createIndex () method to create an index on the slice key. If this collection is empty, MongoDB will create an index when using shardcollection ().
3.7 Adding 200,000 data to the user collection
Add 200,000 data to the user collection to see how the collection is so fragmented
for (var i = 0; I <200000; i++) { Db.user.insert ({"Name": "LRH" +i, "age": 18})}
Use Sh.status () to view shard conditions
---sharding Status--- sharding version: {"_id": 1, "mincompatibleversion": 5, "CurrentVersion": 6, "Clusterid": OBJ Ectid ("58fcb9e3f6420984b3570e11")} shards:{ "_id": "Shardrep", "host": "shardrep/218.245.4.12:27017" , "state": 1} Active mongoses: "3.4.3": 1 databases:{ "_id": "Liruihuan", "PRIMARY": "Shardrep",
We can see that the collection of data is divided into a shard named Shardrep replica set, we only use a shard here, you can start multiple shards, see how the data is divided.
Diligence, desolate and journeys by chance, destroyed by the following.
If you think this article is good or helpful to you, you can give bloggers a little encouragement and support through the "reward" function on the right.
MongoDB Basic Tutorial Series--Nineth MongoDB Shard