叢集由以下3個服務組成:
- Shards Server: 每個shard由一個或多個mongod進程組成,用於儲存資料
- Config Server: 用於儲存叢集的Metadata資訊,包括每個Shard的資訊和chunks資訊
- Route Server: 用於提供路由服務,由Client串連,使整個Cluster看起來像單個DB伺服器
另外,Chunks是指MongoDB中一段連續的資料區塊,預設大小是200M,一個Chunk位於其中一台Shard伺服器上
下面,搭建一個Cluster,它由4台伺服器組成,包括2個Shard,3個Config,1個Route
其中每個Shard由一個Replica Set組成,每個Replica Set由2個Mongod節點,1個vote節點群組成
需要分別建立目錄/opt/soft/mongodb/data/data ,/opt/soft/mongodb/log
192.168.11.229
/opt/soft/mongodb/bin/mongod –fork –shardsvr –port 10000 –replSet set1 –dbpath /opt/soft/mongodb/data/data –logpath /opt/soft/mongodb/log/log.log
/opt/soft/mongodb/bin/mongod –fork –shardsvr –port 10001 –replSet set2 –dbpath /opt/soft/mongodb/data/data1 –logpath /opt/soft/mongodb/log/log1.log
192.168.11.17
/usr/local/mongodb/bin/mongod –fork –shardsvr –port 10000 –replSet set1 –dbpath /opt/soft/mongodb/data/data –logpath /pvdata/mongodb_log/mongod.log
192.168.11.227
/usr/local/mongodb/bin/mongod –fork –shardsvr –port 10000 –replSet set2 –dbpath /opt/soft/mongodb/data/data –logpath /opt/soft/mongodb/log/mongod.log
/usr/local/mongodb/bin/mongod –fork –shardsvr –port 10001 –replSet set1 –dbpath /opt/soft/mongodb/data/data1 –logpath /opt/soft/mongodb/log/mongod1.log
192.168.11.228
/usr/local/mongodb/bin/mongod –fork –shardsvr –port 10000 –replSet set2 –dbpath /opt/soft/mongodb/data/data –logpath /opt/soft/mongodb/log/mongod.log
192.168.11.229上mongo –port 10000串連mongo
config = {_id: 'set1', members: [
{_id: 0, host: '192.168.11.229:10000'},
{_id: 1, host: '192.168.11.17:10000'},
{_id: 2, host: '192.168.11.227:10001', arbiterOnly: true}
]}
rs.initiate(config)
rs.status()
192.168.11.227上mongo –port 10000串連mongo
config = {_id: 'set2', members: [
{_id: 0, host: '192.168.11.227:10000'},
{_id: 1, host: '192.168.11.228:10000'},
{_id: 2, host: '192.168.11.229:10001', arbiterOnly: true}
]}
rs.initiate(config)
rs.status()
192.168.11.229:;192.168.11.17;192.168.11.228上啟動進程
mongod --configsvr --fork --logpath /opt/soft/mongodb/log/config.log --logappend --dbpath /opt/soft/mongodb/data/configdata --port 20000
192.168.11.227上啟動進程
mongos --fork --configdb "192.168.11.229:20000,192.168.11.17:20000,192.168.11.228:20000" --logpath /opt/soft/mongodb/log/mongos.log
192.168.11.227
mongos> use admin
mongos> db.adminCommand({ addShard : "set1/192.168.11229:10000,192.168.11.17:10000"})
mongos> db.adminCommand({ addShard :"set2/192.168.11.227:10000,192.168.11.228:10000"})
mongos> db.adminCommand({enablesharding:'test'})
mongos> db.adminCommand({listshards:1})
mongos> printShardingStatus()
mongos> db.adminCommand({shardcollection:'test.test2', key:{_id:1}, unique : true})
匯入文本資料(資料以,號隔開)
mongoimport -h 192.168.11.227 -d test -c test --type csv -f ip,sid,uid,cateid,type --file /opt/dm/meta/uidip_20111017