Sharding is a horizontal extension that splits data into different machines to store more data and handle larger loads. You can select to be fragmented according to the specified document key value.
Configuration
Overall deployment scenario:
Start three Shard service, two do data storage, another as config,
Configure the Shard replica set, create an administrator user, close Mongod, open keyfile, start MONGOs, configure the Shard service.
Role
Shards generally have three components:
Shard Service (Shard server), mongod instance, more than 2, is responsible for storing the actual data shards, a Shard server in production environment can be replaced by a replica set of several servers to avoid the host single point of failure;
Routing service (Routing process), MONGOs instances, more than 1, it is responsible for the management of shards, the client is routed through the front-end access, and make the entire cluster look like a single database, client applications can be used transparently, Routing Process does not store data, Data from config Server;
Configuration Services (config Server), Mongod instances, more than 1, is responsible for storing the entire cluster configuration information: that is, the data and the corresponding relationship between slices.
Because the testing resources are limited, three virtual machines are taken,
The schema diagram for the Shard is as follows:
192.168.100.101 all configurations are as follows:
########### #config-1########### #configsvr=trueReplset=Configport=30001DBPath=/opt/mongo/data/config-1LogPath=/opt/mongo/logs/config-1. Loglogappend=trueFork=true Profile=1slowms= -keyfile=/opt/mongo/config/Keyfilemaxconns=20000Storageengine=wiredtiger########### #route ########### #configdb= config/192.168.100.101:30001,192.168.100.102:30002,192.168.100.103:30003Port=20000LogPath=/opt/mongo/logs/Route.loglogappend=trueFork=true#chunkSize= thekeyfile=/opt/mongo/config/Keyfilemaxconns=20000########### #rs1-1########### #port=10001Fork=trueDBPath=/opt/mongo/data/rs1-1LogPath=/opt/mongo/logs/rs1-1. Logreplset=Test1logappend=true Profile=1slowms= -Directoryperdb=truekeyfile=/opt/mongo/config/Keyfilemaxconns=20000Storageengine=wiredtiger########### #rs2-a########### #port=20003Fork=trueDBPath=/opt/mongo/data/rs2-Alogpath=/opt/mongo/logs/rs2-A.logreplset=Test2logappend=true Profile=1slowms= -Directoryperdb=truekeyfile=/opt/mongo/config/Keyfilemaxconns=20000Storageengine= Wiredtiger
192.168.100.102 all configurations are as follows:
########### #config-2########### #configsvr=trueReplset=Configport=30002DBPath=/opt/mongo/data/config-2LogPath=/opt/mongo/logs/config-2. Loglogappend=trueFork=true Profile=1slowms= -keyfile=/opt/mongo/config/Keyfilemaxconns=20000Storageengine=wiredtiger########### #route ########### #configdb= config/192.168.100.101:30001,192.168.100.102:30002,192.168.100.103:30003Port=20000LogPath=/opt/mongo/logs/Route.loglogappend=trueFork=true#chunkSize= thekeyfile=/opt/mongo/config/Keyfilemaxconns=20000########### #rs1-2########### #port=10002Fork=trueDBPath=/opt/mongo/data/rs1-2LogPath=/opt/mongo/logs/rs1-2. Logreplset=Test1logappend=true Profile=1slowms= -Directoryperdb=truekeyfile=/opt/mongo/config/Keyfilemaxconns=20000Storageengine=wiredtiger########### #rs2-2########### #port=20002Fork=trueDBPath=/opt/mongo/data/rs2-2LogPath=/opt/mongo/logs/rs2-2. Logreplset=Test2logappend=true Profile=1slowms= -Directoryperdb=truekeyfile=/opt/mongo/config/Keyfilemaxconns=20000Storageengine= Wiredtiger
192.168.100.103 all configurations are as follows:
########### #config-3########### #configsvr=trueReplset=Configport=30003DBPath=/opt/mongo/data/config-3LogPath=/opt/mongo/logs/config-3. Loglogappend=trueFork=true Profile=1slowms= -keyfile=/opt/mongo/config/Keyfilemaxconns=20000Storageengine=wiredtiger########### #route ########### #configdb= config/192.168.100.101:30001,192.168.100.102:30002,192.168.100.103:30003Port=20000LogPath=/opt/mongo/logs/Route.loglogappend=trueFork=true#chunkSize= thekeyfile=/opt/mongo/config/Keyfilemaxconns=20000########### #rs1-a########### #port=10003Fork=trueDBPath=/opt/mongo/data/rs1-Alogpath=/opt/mongo/logs/rs1-A.logreplset=Test1logappend=true Profile=1slowms= -Directoryperdb=truekeyfile=/opt/mongo/config/Keyfilemaxconns=20000Storageengine=wiredtiger########### #rs2-1########### #port=20001Fork=trueDBPath=/opt/mongo/data/rs2-1LogPath=/opt/mongo/logs/rs2-1. Logreplset=Test2logappend=true Profile=1slowms= -Directoryperdb=truekeyfile=/opt/mongo/config/Keyfilemaxconns=20000Storageengine= Wiredtiger
OpenFile can be generated using commands:
> keyfilechmod keyfile
Ensure consistent keyfile of each machine
Create a profile and store directory log directory by following the contents of the configuration file above
Create complete, start all mongod nodes in turn
Note that the KeyFile option should be commented out at this point, otherwise the role will not be created after start authentication, unable to enter the operation
ls | grep Xargs sed " s/^keyfile/#keyFile/g "
After you start, connect each shard in turn to do the following:
>config ={_id:"test1", Members: [{_id:0, Host:"192.168.100.101:10001"}, {_id:1, Host:"192.168.100.102:10002"}, {_id:2, Host:"192.168.100.103:10003", Arbiteronly:true }]}>rs.initiate (config)>rs.status ()>Db.ismaster ()>Use admin;>db.createrole ({role:"Superman", Privileges:[{resource:{anyresource:true}, actions:["anyaction"]}], roles:["Root"]})>db.createuser ({User:"Test",pwd:"Test", Roles:[{role:"Superman"Db:"Admin"}]})
After successful execution,
To stop all Mongod nodes:
for in 'seqten'; Do killall Mongod; Done
Open KeyFile
ls | grep Xargs sed " s/^ #keyFile/keyfile/g "
Then start the Mongod node in turn
Then start the MONGOs node
Connecting MONGOs nodes
mongos> useadmin;mongos>db.auth ("test","test" ) MONGOs>sh. Addshard ("test1/192.168.100.101:10001" ) MONGOs>sh. Addshard ("test2/192.168.100.103:20001" ) MONGOs>sh. Status ()
This completes the Shard configuration.
MongoDB Cluster mode-shard + replica set mode