Starting with MongoDB 3.0, a running instance is required when the Mongorestore is restored. Earlier versions did not have this requirement.
1. Deploy a replication set for each shard
(1) Each member of the replication set initiates a Mongod
Mongod--dbpath/mdb/data/s11--logpath/mdb/mlog/s11.log--fork--port 27017--replset s1--smallfiles &mongod--DBPA TH/MDB/DATA/S12--logpath/mdb/mlog/s12.log--fork--port 27018--replset s1--smallfiles &mongod--dbpath/mdb/data/ S21--logpath/mdb/mlog/s21.log--fork--port 27019--replset s2--smallfiles &mongod--dbpath/mdb/data/s22--logpath /mdb/mlog/s22.log--fork--port 27020--replset S2--smallfiles &
(2) Connect to the instance via MONGO, run:
MONGO--port=27017>rs.initiate () >rs.add ("11.11.11.195:27018")
MONGO--port=27019>rs.initiate () >rs.add ("11.11.11.195:27020")
2. Deploying the config server
Mongod--dbpath/mdb/data/sc--logpath/mdb/mlog/sc.log--fork--port 27021--configsvr &
3. Launch the MONGOs instance
MONGOs--logpath/mdb/mlog/ss.log--fork--port 30000--configdb 11.11.11.195:27021
4. Adding shards to a cluster
Login router./mongo--port 30000 Add slice node Sh.addshard ("s1/11.11.11.195:27018") Sh.addshard ("s2/11.11.11.195:27020")
mongos> sh.status ()---sharding status--- sharding version: { "_id": 1, "mincompatibleversion": 5, "CurrentVersion": 6, "Clusterid": ObjectId ("5722c003710922b361783847")} Shards: { "_id": "S1 ", " host ":" s1/11.11.11.195:27018,11.11.11.195:27017 "} { " _id ":" S2 ", " host ":" s2/ 11.11.11.195:27020,11.11.11.195:27019 "} Balancer: currently enabled: Yes currently running: No Failed balancer rounds in last 5 attempts: 0 Migration Results for the last Hours: No Recent migrations< C21/>databases: { "_id": "admin", "partitioned": false,
5. Close MONGOs Instances
After the Shard cluster is started, close the MONGOs instance
6. Restoring shard Data
Mongorestore--drop/mdb/bin/s1--port 27017
2016-04-29t10:20:20.643+0800 Building a list of DBS and collections to restore FROM/MDB/BIN/S1 dir2016-04-29t10:20:20. 657+0800 reading metadata file from/mdb/bin/s1/snps/elegans.metadata.json2016-04-29t10:20:20.658+0800 reading Metad ATA file from/mdb/bin/s1/test/system.users.metadata.json2016-04-29t10:20:20.658+0800 reading metadata file from/mdb/ bin/s1/snps/system.users.metadata.json2016-04-29t10:20:20.658+0800 restoring Snps.elegans from file/mdb/bin/s1/ snps/elegans.bson2016-04-29t10:20:20.658+0800 restoring test.system.users from file/mdb/bin/s1/test/ system.users.bson2016-04-29t10:20:20.666+0800 restoring snps.system.users from file/mdb/bin/s1/snps/ system.users.bson2016-04-29t10:20:22.974+0800 restoring indexes for collection snps.system.users from metadata2016-04-29t10:20:22.975+0800 finished restoring snps.system.users2016-04-29t10:20:23.073+0800 restoring IND EXEs for collection test.system.users from metadata2016-04-29t10:20:23.074+0800 finished restoring test.system.users2016-04-29t10:20:23.644+0800 [# # .............] Snps.elegans 1.6 mb/13.3 MB (11.8%) 2016-04-29t10:20:26.644+0800 [############## ...] Snps.elegans 7.9 mb/13.3 MB (59.3%) 2016-04-29t10:20:29.239+0800 restoring indexes for collection Snps.elegans from me tadata2016-04-29t10:20:29.660+0800 finished restoring snps.elegans2016-04-29t10:20:29.660+0800 done
Mongorestore--drop/mdb/bin/s2--port 27019
2016-04-29t10:20:44.153+0800 Building a list of DBS and collections to restore FROM/MDB/BIN/S2 dir2016-04-29t10:20:4 4.165+0800 Reading metadata file from/mdb/bin/s2/snps/elegans.metadata.json2016-04-29t10:20:44.165+0800 Restoring Snps.elegans from file/mdb/bin/s2/snps/elegans.bson2016-04-29t10:20:44.184+0800 restoring indexes for Collection Snps.elegans from metadata2016-04-29t10:20:44.186+0800 finished restoring snps.elegans2016-04-29t10 : 20:44.186+0800 Done
Then close all the Shard instances
7. Restore config server data
Mongorestore--drop/mdb/bin/config_server--port 27021
2016-04-29t10:26:24.294+0800 Building a list of DBS and collections to restore From/mdb/bin/config_server dir2016-04-2 9t10:26:24.296+0800 reading metadata file from/mdb/bin/config_server/config/changelog.metadata.json2016-04-29t10 : 26:24.296+0800 reading metadata file from/mdb/bin/config_server/config/locks.metadata.json2016-04-29t10:26:24.297 +0800 restoring config.locks from file/mdb/bin/config_server/config/locks.bson2016-04-29t10:26:24.297+0800 reading Metadata file from/mdb/bin/config_server/config/actionlog.metadata.json2016-04-29t10:26:24.302+0800 Reading Metadata file from/mdb/bin/config_server/config/chunks.metadata.json2016-04-29t10:26:24.302+0800 restoring Config.chunks from file/mdb/bin/config_server/config/chunks.bson2016-04-29t10:26:24.303+0800 restoring indexes for Collection config.locks from metadata2016-04-29t10:26:24.303+0800 restoring Config.actionlog from File/mdb/bin/config _server/config/actionlog.bson2016-04-29t10:26:24.303+0800 Restoring indexes for collection config.chunks from metadata2016-04-29t10:26:24.304+0800 restoring Config.changelog F Rom file/mdb/bin/config_server/config/changelog.bson2016-04-29t10:26:24.304+0800 finished restoring config.locks2016-04-29t10:26:24.306+0800 restoring indexes for collection Config.actionlog from metadata2016-04-29t10 : 26:24.306+0800 reading metadata file from/mdb/bin/config_server/config/shards.metadata.json2016-04-29t10 : 26:24.306+0800 restoring Config.shards from file/mdb/bin/config_server/config/shards.bson2016-04-29t10:26:24.307+ 0800 finished restoring config.chunks2016-04-29t10:26:24.307+0800 finished restoring Config.actionlog2016-04-29t10:2 6:24.307+0800 restoring indexes for collection config.shards from metadata2016-04-29t10:26:24.308+0800 restoring ind EXEs for collection Config.changelog from metadata2016-04-29t10:26:24.308+0800 reading metadata file from/mdb/bin/conf ig_server/config/databases.metadata.json2016-04-29t10:26:24.308+0800 restoring config.databases from file/mdb/bin/config_server/config/databases.bson2016-04-29t10:26:24.308+ 0800 reading metadata File from/mdb/bin/config_server/config/lockpings.metadata.json2016-04-29t10:26:24.308+0800 fi nished restoring config.shards2016-04-29t10:26:24.308+0800 reading metadata file from/mdb/bin/config_server/config/ collections.metadata.json2016-04-29t10:26:24.308+0800 finished restoring config.changelog2016-04-29t10:26:24.309+ 0800 restoring config.lockpings from file/mdb/bin/config_server/config/lockpings.bson2016-04-29t10:26:24.309+0800 R Estoring config.collections from file/mdb/bin/config_server/config/collections.bson2016-04-29t10:26:24.325+0800 Reading metadata file from/mdb/bin/config_server/config/mongos.metadata.json2016-04-29t10:26:24.325+0800 restoring Indexes for collection config.databases from metadata2016-04-29t10:26:24.326+0800 restoring Config.mongos from FILE/MD b/bin/config_server/config/mongos.bson2016-04-29t10:26:24.326+0800 restoring indexes for collection config.lockpings from metadata2016-04-29t10:26:24.327+0800 resto Ring indexes for collection config.collections from metadata2016-04-29t10:26:24.327+0800 finished restoring Config.data bases2016-04-29t10:26:24.327+0800 finished restoring config.lockpings2016-04-29t10:26:24.328+0800 reading metadata F Ile from/mdb/bin/config_server/config/version.metadata.json2016-04-29t10:26:24.328+0800 restoring Config.version From file/mdb/bin/config_server/config/version.bson2016-04-29t10:26:24.328+0800 reading metadata file from/mdb/bin/ config_server/config/settings.metadata.json2016-04-29t10:26:24.328+0800 restoring config.settings from file/mdb/ Bin/config_server/config/settings.bson2016-04-29t10:26:24.328+0800 finished restoring config.collections2016-04-29t10:26:24.328+0800 reading metadata file from/mdb/bin/config_server/config/ tags.metadata.json2016-04-29t10:26:24.328+0800 restoring config.tags from FILE/MDb/bin/config_server/config/tags.bson2016-04-29t10:26:24.366+0800 restoring indexes for collection config.tags from metadata2016-04-29t10:26:24.366+0800 restoring indexes for collection config.settings from metadata2016-04-29t10 : 26:24.366+0800 restoring indexes for collection config.version from metadata2016-04-29t10:26:24.375+0800 restoring Indexes for collection Config.mongos from metadata2016-04-29t10:26:24.376+0800 finished restoring config.settings2016- 04-29t10:26:24.376+0800 finished restoring config.tags2016-04-29t10:26:24.376+0800 finished restoring Config.mongos2 016-04-29t10:26:24.376+0800 finished restoring config.version2016-04-29t10:26:24.376+0800 done
8. Start MONGOs Instance
MONGOs--logpath/mdb/mlog/ss.log--fork--port 30000--configdb 11.11.11.195:27021
2016-04-29t10:27:56.855+0800 W sharding Running with 1 config server should are done only for testing purposes and are not r ecommended for Productionabout to fork child process, waiting until server was ready for connections.forked process:25444c Hild Process started successfully, parent exiting
9. If the hostname of the Shard has changed, the Config database needs to be updated
10. Restart all Shard Mongod instances
11. Restart other MONGOs instances
12. Verifying the cluster environment
mongos> db.printshardingstatus ()---sharding Status---sharding version: {"_id": 1, "Mincompatibleve Rsion ": 5," CurrentVersion ": 6," Clusterid ": ObjectId (" 553f0cc819d7841961ac8f4b ")} shards: {" _i D ":" S1 "," host ":" s1/11.11.11.195:27018,11.11.11.195:27017 "} {" _id ":" S2 "," host ":" s2/11.11.11.195:27020 , 11.11.11.195:27019 "} balancer:currently Enabled:yes currently running:no Failed balancer Roun DS in last 5 attempts:0 migration Results for the last Hours:no recent migrations databases: {"_id": "admin", "partitioned": false, "PRIMARY": "Config"} {"_id": "SNPs", "partitioned": true, "PRIMARY": "S1"} Snps.elegans Shard key : {"SNP": 1} chunks:s1 1 s2 1 {"SNP": {"$minKey": 1}}-->> {"SNP": "haw100000"} on:s2 Timestamp (2, 0) {"SNP": "haw100000"}-->> {"SNP" : {"$maxKey": 1} on:s1 Timestamp (2, 1) {"_id": "Test", "partitioned": false, "PRIMARY": "S1"}mongo S>
MongoDB Shard Cluster Restore