用3臺pc搭建mongodb的分布式系統,在三臺pc上都運行以下命令:$MONGODB_PATH是我設定好的一個變數
mongod --configsvr --dbpath ${MONGODB_PATH}/config/data --port 21000 --logpath ${MONGODB_PATH}/config/log/config.log --fork
mongod --shardsvr --replSet shard1 --port 22001 --dbpath ${MONGODB_PATH}/shard1/data --logpath ${MONGODB_PATH}/shard1/log/shard1.log --fork --nojournal --oplogSize 10
mongod --shardsvr --replSet shard2 --port 22002 --dbpath ${MONGODB_PATH}/shard2/data --logpath ${MONGODB_PATH}/shard2/log/shard2.log --fork --nojournal --oplogSize 10
mongod --shardsvr --replSet shard3 --port 22003 --dbpath ${MONGODB_PATH}/shard3/data --logpath ${MONGODB_PATH}/shard3/log/shard3.log --fork --nojournal --oplogSize 10
在172.24.222.32這臺服務器上運行
mongos --configdb 172.24.222.32:21000,172.24.222.39:21000,172.24.222.46:21000 --port 20000 --logpath ${MONGODB_PATH}/mongos/log/mongos.log --fork
分片服務器設定好之后查看配置
mongos> db.runCommand( { listshards : 1 } )
{
"shards" : [
{
"_id" : "shard1",
"host" : "shard1/172.24.222.32:22001,172.24.222.39:22001,172.24.222.46:22001"
},
{
"_id" : "shard2",
"host" : "shard2/172.24.222.32:22002,172.24.222.39:22002,172.24.222.46:22002"
},
{
"_id" : "shard3",
"host" : "shard3/172.24.222.32:22003,172.24.222.39:22003,172.24.222.46:22003"
}
],
"ok" : 1
}
然后對kidwatch資料庫的table1集合進行分片
db.runCommand( { enablesharding :"kidwatch"});
#指定資料庫里需要分片的集合和片鍵
db.runCommand( { shardcollection : "kidwatch.table1",key : {key_id: 1} } )
回傳都成功之后
use kidwatch
然后我插入資料
for (var i = 1; i <= 3000000; i++)
db.table1.save({key_id:i,"testkey":"testval"});
成功之后查看分片情況
mongos> db.table1.stats()
{
"sharded" : true,
"ns" : "kidwatch.table1",
"count" : 2999999,
"numExtents" : 13,
"size" : 192000000,
"storageSize" : 243314688,
"totalIndexSize" : 181065696,
"indexSizes" : {
"_id_" : 97343456,
"key_id_1" : 83722240
},
"avgObjSize" : 64.00002133334044,
"nindexes" : 2,
"nchunks" : 1,
"shards" : {
"shard1" : {
"ns" : "kidwatch.table1",
"count" : 2999999,
"size" : 192000000,
"avgObjSize" : 64.00002133334044,
"storageSize" : 243314688,
"numExtents" : 13,
"nindexes" : 2,
"lastExtentSize" : 68579328,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 181065696,
"indexSizes" : {
"_id_" : 97343456,
"key_id_1" : 83722240
},
"ok" : 1
}
},
"ok" : 1
}
所有的資料都寫到一個chunk里面,而且chunksize我已經設定為1MB
mongos> db.settings.find()
{ "_id" : "chunksize", "value" : 1 }
現在我不知道什么原因導致mongodb不會分塊,請大神們解答,萬分感謝
uj5u.com熱心網友回復:
你分片集群,沒有開啟自動分片功能,也就是沒有開啟balancer,在mongos下執行sh.startBalancer();然后進入config庫,執行db.settings.find(),可以看到{ "_id" : "balancer", "stopped" : false }轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/77320.html
標籤:服務器
