在單臺機(jī)器上測試:
? mkdir -p /data/mongodbtest136/mongos/log
? mkdir -p /data/mongodbtest137/mongos/log
? mkdir -p /data/mongodbtest138/mongos/log
? mkdir -p /data/mongodbtest136/config/data
? mkdir -p /data/mongodbtest137/config/data
? mkdir -p /data/mongodbtest138/config/data
? mkdir -p /data/mongodbtest138/config/log
? mkdir -p /data/mongodbtest137/config/log
? mkdir -p /data/mongodbtest136/config/log
? mkdir -p /data/mongodbtest136/shard1/data
? mkdir -p /data/mongodbtest136/shard2/data
? mkdir -p /data/mongodbtest136/shard3/data
? mkdir -p /data/mongodbtest136/shard3/log
? mkdir -p /data/mongodbtest136/shard2/log
? mkdir -p /data/mongodbtest136/shard1/log
? mkdir -p /data/mongodbtest137/shard1/log
? mkdir -p /data/mongodbtest137/shard2/log
? mkdir -p /data/mongodbtest137/shard3/log
? mkdir -p /data/mongodbtest137/shard3/data
? mkdir -p /data/mongodbtest137/shard2/data
? mkdir -p /data/mongodbtest137/shard1/data
? mkdir -p /data/mongodbtest138/shard1/data
? mkdir -p /data/mongodbtest138/shard2/data
? mkdir -p /data/mongodbtest138/shard3/data
? mkdir -p /data/mongodbtest138/shard3/log
? mkdir -p /data/mongodbtest138/shard2/log
? mkdir -p /data/mongodbtest138/shard1/log
?mongod --configsvr --replSet cfgReplSet --dbpath /data/mongodbtest136/config/data --port 21360 --logpath /data/mongodbtest136/config/log/config.log --fork
?mongod --configsvr --replSet cfgReplSet --dbpath /data/mongodbtest137/config/data --port 21370 --logpath /data/mongodbtest137/config/log/config.log --fork
?mongod --configsvr --replSet cfgReplSet --dbpath /data/mongodbtest138/config/data --port 21380 --logpath /data/mongodbtest138/config/log/config.log --fork
一條一條的輸入以下三條:?
mongos ?--configdb cfgReplSet/127.0.0.1:21360,127.0.0.1:21370,127.0.0.1:21380 ?--port 20006 ? --logpath ?/data/mongodbtest136/mongos/log/mongos.log --fork
?mongos ?--configdb cfgReplSet/127.0.0.1:21360,127.0.0.1:21370,127.0.0.1:21380 ?--port 20007 ? --logpath ?/data/mongodbtest137/mongos/log/mongos.log --fork
?mongos ?--configdb cfgReplSet/127.0.0.1:21360,127.0.0.1:21370,127.0.0.1:21380 ?--port 20008 ? --logpath ?/data/mongodbtest138/mongos/log/mongos.log --fork
?mongod --shardsvr --replSet shard1 --port 21361 --dbpath /data/mongodbtest136/shard1/data ?--logpath /data/mongodbtest136/shard1/log/shard1.log --fork --nojournal ?--oplogSize 10
? mongod --shardsvr --replSet shard2 --port 21362 --dbpath /data/mongodbtest136/shard2/data ?--logpath /data/mongodbtest136/shard2/log/shard2.log --fork --nojournal ?--oplogSize 10
? mongod --shardsvr --replSet shard3 --port 21363 --dbpath /data/mongodbtest136/shard3/data ?--logpath /data/mongodbtest136/shard3/log/shard3.log --fork --nojournal ?--oplogSize 10
? mongod --shardsvr --replSet shard1 --port 21371 --dbpath /data/mongodbtest137/shard1/data ?--logpath /data/mongodbtest137/shard1/log/shard1.log --fork --nojournal ?--oplogSize 10
? mongod --shardsvr --replSet shard2 --port 21372 --dbpath /data/mongodbtest137/shard2/data ?--logpath /data/mongodbtest137/shard2/log/shard2.log --fork --nojournal ?--oplogSize 10
? mongod --shardsvr --replSet shard3 --port 21373 --dbpath /data/mongodbtest137/shard3/data ?--logpath /data/mongodbtest137/shard3/log/shard3.log --fork --nojournal ?--oplogSize 10
? mongod --shardsvr --replSet shard1 --port 21381 --dbpath /data/mongodbtest138/shard1/data ?--logpath /data/mongodbtest138/shard1/log/shard1.log --fork --nojournal ?--oplogSize 10
? mongod --shardsvr --replSet shard2 --port 21382 --dbpath /data/mongodbtest138/shard2/data ?--logpath /data/mongodbtest138/shard2/log/shard2.log --fork --nojournal ?--oplogSize 10
? mongod --shardsvr --replSet shard3 --port 21383 --dbpath /data/mongodbtest138/shard3/data ?--logpath /data/mongodbtest138/shard3/log/shard3.log --fork --nojournal ?--oplogSize 10
-----im Start-------
#配置服務(wù)器
? mongo ?127.0.0.1:21360
?use admin
?#定義副本集配置
config = { _id:"cfgReplSet",
configsvr:true,
?members:[
? ? ? ? ? ? ? ? ? ? ?{_id:0,host:"127.0.0.1:21360"},
? ? ? ? ? ? ? ? ? ? ?{_id:1,host:"127.0.0.1:21370"},
? ? ? ? ? ? ? ? ? ? ?{_id:2,host:"127.0.0.1:21380"}
? ? ? ? ? ? ? ? ]
? ? ? ? ?}
#初始化副本集配置
rs.initiate(config);
?-----im End-------
?mongo ?127.0.0.1:21361
?use admin
?#定義副本集配置
config = { _id:"shard1", members:[
? ? ? ? ? ? ? ? ? ? ?{_id:0,host:"127.0.0.1:21361"},
? ? ? ? ? ? ? ? ? ? ?{_id:1,host:"127.0.0.1:21371"},
? ? ? ? ? ? ? ? ? ? ?{_id:2,host:"127.0.0.1:21381",arbiterOnly:true}
? ? ? ? ? ? ? ? ]
? ? ? ? ?}
#初始化副本集配置
rs.initiate(config);
?mongo ?127.0.0.1:21362
?use admin
?#定義副本集配置
config = { _id:"shard2", members:[
? ? ? ? ? ? ? ? ? ? ?{_id:0,host:"127.0.0.1:21362"},
? ? ? ? ? ? ? ? ? ? ?{_id:1,host:"127.0.0.1:21372"},
? ? ? ? ? ? ? ? ? ? ?{_id:2,host:"127.0.0.1:21382",arbiterOnly:true}
? ? ? ? ? ? ? ? ]
? ? ? ? ?}
#初始化副本集配置
rs.initiate(config);
?mongo ?127.0.0.1:21363
?use admin
?#定義副本集配置
config = { _id:"shard3", members:[
? ? ? ? ? ? ? ? ? ? ?{_id:0,host:"127.0.0.1:21363"},
? ? ? ? ? ? ? ? ? ? ?{_id:1,host:"127.0.0.1:21373"},
? ? ? ? ? ? ? ? ? ? ?{_id:2,host:"127.0.0.1:21383",arbiterOnly:true}
? ? ? ? ? ? ? ? ]
? ? ? ? ?}
#初始化副本集配置
rs.initiate(config);
? mongo ?127.0.0.1:20006
?#使用admin數(shù)據(jù)庫
use ?admin
#串聯(lián)路由服務(wù)器與分配副本集1
db.runCommand( { addshard : "shard1/127.0.0.1:21361,127.0.0.1:21371,127.0.0.1:21381"});
如里shard是單臺服務(wù)器篡殷,用 db.runCommand( { addshard : “[: ]” } )這樣的命令加入粥烁,如果shard是副本集是辕,用db.runCommand( { addshard : “replicaSetName/[:port][,serverhostname2[:port],…]” });這樣的格式表示 助币。
#串聯(lián)路由服務(wù)器與分配副本集2
db.runCommand( { addshard : "shard2/127.0.0.1:21362,127.0.0.1:21372,127.0.0.1:21382"});
#串聯(lián)路由服務(wù)器與分配副本集3
db.runCommand( { addshard : "shard3/127.0.0.1:21363,127.0.0.1:21373,127.0.0.1:21383"});
#查看分片服務(wù)器的配置
db.runCommand( { listshards : 1 } );
?#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定數(shù)據(jù)庫里需要分片的集合和片鍵 ?#均勻分布
db.runCommand( { shardcollection : "testdb.table1",key : {_id: 'hashed'} } )
db.runCommand( { shardcollection : "testdb.table2",key : {_id: 1,id:1} } )
db.runCommand( { shardcollection : "testdb.table3",key : {id: 1, _id:1} } )
#db.runCommand( { shardcollection : "testdb.table3",key : {_id: 'hashed',id:1} } )
db.runCommand( { shardcollection : "testdb.table4",key : {id: 'hashed'} } )
# db.table1.createIndex({_id: 'hashed'})
?# http://www.ttlsa.com/mongodb/how-to-choose-a-shard-key-for-mongodb/
?mongo ?127.0.0.1:20006
?#使用testdb
use ?testdb;
#插入測試數(shù)據(jù)
for (var i = 1; i <= 1000; i++) db.table1.save({id:i,"test1":"testval1"+i});
for (var i = 1; i <= 1000; i++) db.table2.save({id:i,"test2":"testval2"+i});
#for (var i = 1; i <= 1000; i++) db.table3.save({id:i,"test3":"testval3"+i});
for (var i = 1; i <= 1000; i++) db.table4.save({id:i,"test4":"testval4"+i});
db.table1.ensureIndex({id: 1, _id: 1}, {background: true})
#查看分片情況如下烈和,部分無關(guān)信息省掉了
db.table1.stats();
?#Robomongo.exe
?#查看分片信息
?db.getCollection('table1').stats();
?db.printShardingStatus();
?#這帶來一個(gè)問題是我們測開發(fā)測試的時(shí)候,往往希望通過觀察數(shù)據(jù)在遷移來證明分片是否成功湖雹,但64M200M顯然過大勤庐,解決方法是我們可以在啟動(dòng)mongos的時(shí)候用—chunkSize來制定塊的大小,單位是MB奥溺。
? ?mongos> use config
? ?mongos> db.settings.save( { _id:"chunksize", value: 1 } )
參考鏈接