在本文 mongos为 20000, config server 为 21000, shard1为 22001 , shard2为22002, shard3为22003.
mongod --configsvr --dbpath /ssd/mongodb/config/data --port 21000 --logpath /ssd/mongodb/config/log/config.log --fork
mongos --configdb 192.168.10.203:21000,192.168.10.204:21000,192.168.10.205:21000 --port 20000 --logpath /ssd/mongodb/mongos/log/mongos.log --fork
mongod --shardsvr --replSet shard1 --port 22001 --dbpath /ssd/mongodb/shard1/data --logpath /ssd/mongodb/shard1/log/shard1.log --fork --nojournal --oplogSize 10
mongod --shardsvr --replSet shard2 --port 22002 --dbpath /ssd/mongodb/shard2/data --logpath /ssd/mongodb/shard2/log/shard2.log --fork --nojournal --oplogSize 10
mongod --shardsvr --replSet shard3 --port 22003 --dbpath /ssd/mongodb/shard3/data --logpath /ssd/mongodb/shard3/log/shard3.log --fork --nojournal --oplogSize 10
任意一台机器:
mongo 127.0.0.1:22001
use admin
config = { _id:"shard1", members:[
{_id:0,host:"192.168.10.203:22001",priority:2},
{_id:1,host:"192.168.10.204:22001",priority:1},
{_id:2,host:"192.168.10.205:22001",arbiterOnly:true}
]
}
或
config = { _id:"shard1", members:[
{_id:0,host:"192.168.10.203:22001",priority:3},
{_id:1,host:"192.168.10.204:22001",priority:2},
{_id:2,host:"192.168.10.205:22001",priority:1}
]
}
rs.initiate(config);
mongo 127.0.0.1:22002
use admin
config = { _id:"shard2", members:[
{_id:0,host:"192.168.10.203:22002",priority:2},
{_id:1,host:"192.168.10.204:22002",priority:1},
{_id:2,host:"192.168.10.205:22002",arbiterOnly:true}
]
}
或
config = { _id:"shard2", members:[
{_id:0,host:"192.168.10.203:22002",priority:2},
{_id:1,host:"192.168.10.204:22002",priority:3},
{_id:2,host:"192.168.10.205:22002",priority:1}
]
}
rs.initiate(config);
mongo 127.0.0.1:22003
use admin
config = { _id:"shard3", members:[
{_id:0,host:"192.168.10.203:22003",priority:2},
{_id:1,host:"192.168.10.204:22003",priority:1},
{_id:2,host:"192.168.10.205:22003",arbiterOnly:true}
]
}
或
config = { _id:"shard3", members:[
{_id:0,host:"192.168.10.203:22003",priority:2},
{_id:1,host:"192.168.10.204:22003",priority:1},
{_id:2,host:"192.168.10.205:22003",priority:3}
]
}
rs.initiate(config);
mongo 127.0.0.1:20000
use admin
db.runCommand( { addshard : "shard1/192.168.10.203:22001,192.168.10.204:22001,192.168.10.205:22001"});
db.runCommand( { addshard : "shard2/192.168.10.203:22002,192.168.10.204:22002,192.168.10.205:22002"});
db.runCommand( { addshard : "shard3/192.168.10.203:22003,192.168.10.204:22003,192.168.10.205:22003"});
db.runCommand( { listshards : 1 } );
移除分片
db.runCommand( { removeshard: "192.168.10.205:22005" } )
db.runCommand( { movePrimary: "database_name", to: "192.168.10.203:22004" })
db.runCommand( { removeshard: "192.168.10.205:22005" } )
使分片对于具体数据库和表生效
db.runCommand( { enablesharding :"testdb"});
db.tab4.ensureIndex({"username" : "hashed"}) //创建索引
db.runCommand( { shardcollection : "testdb.table1",key : {"username": "hashed"} } )
或 db.runCommand( { shardcollection : "testdb.table1",key : {_id: "hashed"} } )
mongo 127.0.0.1:20000
use testdb;
for (var i = 1; i <= 30000; i++){
db.table1.save({id:i,"test1":"testval1"});
}
db.table1.stats();
use config
db.shards.find()
db.databases.find()
db.chunks.find()
db.printShardingStatus()