Sharded Cluster
Data fragmentation in Mongodb is called chunk, which is a continuous data record in a Collection, but it has a size limit that cannot exceed 200M. If it exceeds, a new fragment will be generated.
The following is a simple shard cluster example
The composition of the shard cluster:
Shard server: mongod instance, used to store actual data blocks
Config server: mongod instance, used to store the entire Cluster Metadata, including chunk information.
route
server: mongos instance, as the front-end route of the entire cluster, from which the entire cluster is connected. This makes the entire cluster look like a single-process database.
Remarks: route as a route will forward the request to the actual target service process, combine multiple results and send them back to the client. The route does not store any data and status, all information is from Config when starting
server, when there is information update on the Config server, it will also be synchronized to the route server. the
Build a simple cluster
Cluster directory:
There are four mongodb in total, the directories are /home/scotte.ye/mongo1, mongo2, mongo3, mongo4
Among them, mongo1 and mongo2 are used as shard servers
mongo3 as config server
mongo4 as route server
1. Start the Shard server
$ cd /home/scotte.ye/mongo1
$ ./mongo -shardsvr -port 10000 -dbpath=/home/data/10000/ -fork
-logpath=/home/log/10000/null
$ all output going to: /home/log/10000/null
$ fork process: 10657
$ cd /home/scotte.ye/mongo2
$ ./mongo -shardsvr -port 10011 -dbpath=/home/data/10011/ -fork
-logpath=/home/log/10011/null
$ all output going to: /home/log/10011/null
$ fork process: 10661
$ cd /home/scotte.ye/mongo3
$ ./mongo -configsvr -port 20000 -dbpath=/home/data/20000/ -fork
-logpath=/home/log/20000/null
$ all output going to: /home/log/20000/null
$ fork process: 10857
$ cd /home/scotte.ye/mongo4
$ ./mongos -configdb 192.168.35.106:20000 -fork
-logpath=/home/log/20000/null
$ all output going to: /home/log/20000/null
$ fork process: 10900
// Note that when starting the Route server, the size of the chunk can also be configured through the -chunksize parameter
2. Configuration related
Configuration related command instructions:
addshard: add shard server to the cluster. Similar commands include listshards and removeshard
enablesharding: used to set which databases can be distributed storage
shardcollection: used to set the name of the specific sharded collection, and the share key must be specified, and the system will automatically create an index
Note: The collection of shardcollection must have only one unique index and must be a shard key
Start configuration:
$ cd /home/scotte.ye/mongo3/bin
$ ./mongo
$ >use admin
$# can only be operated in the admin database
$ switched to db admin
$ >db.runCommand({addshard:’192.168.35.106:10000′})
${“shardAdded”:”shard0000″,”OK”:1}
$ >db.runCommand({addshard:’192.168.35.106:10011′})
${“shardAdded”:”shard0001″,”OK”:1}
$#Add corresponding to shard server to shard cluster
$ >db.runCommand({enablesharding:’test’})
$# enables the corresponding database table test to be distributed and stored, and test can be replaced with the corresponding other database name
$
>db.runCommand({sahrdcollection:’test.user’,key:{_id:1}})
${“OK”:1}
$# indicates the corresponding collection and shard key
${“collectionsharded”:”test.user”,”OK”:1}
3. Commonly used status query commands
printShardingStatus(): View Sharding information
db..stats(): View specific shard storage information
isdbgrid: used to confirm whether the current sharding cluster
ismaster: judge whether it is master