Environment:
Windows operating system
MongoDB 3.4 Community Edition
Goal:
Configure a shard cluster that contains two shards of one configuration server. Each of these shards and a configuration server is configured as a separate replica set. As shown in the following:
Note: Each shard should be configured on a separate server device. For convenience, this paper simulates the configuration of a shard cluster by simulating the components on different servers through different ports on the same machine. (The same is the configuration of the production environment, just use your own hostname, port, path, etc.).
The Shard cluster schema configured for this article, where any node (replica set node and Shard node) is extensible.
1. Create data directory for config server, shard-1, shard-2, respectively
Each shard member is configured as a replica set, so each component contains its own replica set data directory.
The replication sets for config server, shard-1, and Shard-2 are Rs1, RS2, and Rs3, respectively.
Each replica set contains three nodes rsx-0 (Master node), Rsx-1, Rsx-2.
Where the config server replica set data directory is as follows: Shard-1, shard-2 the replica set directory for the Shard is similar, and so on.
The port assignment and startup configuration for each member are as follows:
2. Create config server replica set
Mongod--configsvr--replset <setname>--dbpath <path>
--configsvr declares that the instance is the Configuration service database for the Shard cluster
--replset specifying the replica set name
--dbpath specifying the data storage directory
Start and initialize the three members of the Config server replica set by using the following command, respectively:
Mongod--configsvr--port 27020--dbpath E:\devInstall\MongoDB\shard\consrv\rs1-0--replset rs1--smallfiles-- Oplogsize 128
Mongod--configsvr--port 27021--dbpath E:\devInstall\MongoDB\shard\consrv\rs1-1--replset rs1--smallfiles-- Oplogsize 128
Mongod--configsvr--port 27022--dbpath E:\devInstall\MongoDB\shard\consrv\rs1-2--replset rs1--smallfiles-- Oplogsize 128
After the initialization succeeds, connect to one of the 3 configuration servers using the MONGO shell:
MONGO--host
After the connection succeeds, set the replication set and its nodes:
rs.initiate ({_id:"<replSetName>", Configsvr:true, Members: [{_id:0, Host:"127.0.0.1:27020"}, {_id:1, Host:"127.0.0.1:27021"}, {_id:2, Host:"127.0.0.1:27022" } ] })
After the replication set is set up successfully, use rs.conf () to view the relevant status:
2. Create a shard copy set (Take Shard-1 as an example)
Mongod--shardsvr--replset <replSetname>
--shardsvr declares that this instance is a fragmented DB instance of a cluster
--replset specifying the replica set name
Start and initialize the three members of the shard-1 replica set by using the following command, respectively:
Shard-1:
Mongod--shardsvr--port 27023--dbpath E:\devInstall\MongoDB\shard\shard-1\rs2-0--replset rs2--smallfiles-- Oplogsize 128
Mongod--shardsvr--port 27024--dbpath E:\devInstall\MongoDB\shard\shard-1\rs2-1--replset rs2--smallfiles-- Oplogsize 128
Mongod--shardsvr--port 27025--dbpath E:\devInstall\MongoDB\shard\shard-1\rs2-2--replset rs2--smallfiles-- Oplogsize 128
After the initialization succeeds, connect to one of the 3 shard servers using the MONGO shell:
MONGO--host
After the connection succeeds, set the replication set and its nodes:
rs.initiate ( { <replicaSetName>, Members : [ 0] 127.0.0.1:27023" }, 1"127.0.0.1:27024" }, 2"127.0.0.1:27025" } ] })
After the setting is successful, use Rs.status () to view the replication set and the status of each node:
Shard-2 replica set configuration.
3. MONGOs Configuration
Initiates a MONGOs (route) connection to the current shard cluster. You need to use the--CONFIGDB option to specify a config server replica set.
Format:<configreplsetname>/cfg1.example.net:27017,cfg2.example.net:27017,...
Here, we start the MONGOs routing service on port 27030 using the first node of the config server replica set:
" rs1/127.0.0.1:27020 " 27030
Connect to the MONGOs instance using the MONGO shell:
Use Db.ismster () to determine whether to the MONGOs instance, if there is "MSG": "Isdbgrid", indicating a successful connection.
4. Adding shards to the cluster
The MONGO shell window is still connected to the MONGOs.
Use the Sh.addshard ("<replsetname>/s1-mongo1.example.net:27017") method to add shard-1 and shard-2 two shards to the cluster.
5. Partitioning the collection with hash shards
To turn on the database sharding feature:
Sh.enablesharding ("<database>")
Specifies that the field in the database table key is a sharding key and uses a hash shard policy on it.
Sh.shardcollection ("<database>.<collection>", {<key>: "hashed"})
This concludes the settings for the Shard cluster.
Example: Turn on the Shard function of shard data. Specifies that the stunum of the student table is a hash index sharding key. Insert 7 data records into the student table. As follows:
By MONGO into the 1 shard shard-1, it was found that the student table contained 5 data.
By MONGO into the 2 shard Shard-2, it was found that the student table contained 2 data.
There are 7 data in the student table in this shard cluster. There are 5 on the shard-1 server, 2 on the shard-2 server. (MongoDB determines which shard to store by calculating the hash value of the Stunum field)
Users who use MONGOs do not care where the data is stored, and users only know that there are 7 student data records in the database of the cluster.
MongoDB Shard Cluster Configuration instance