我使用的是windows和mongodb 3.2.22,我想在本地主机上运行分片,我在cmd终端上执行了这些命令
mongod --replSet s0 --dbpath C:\Users\Marco\Desktop\mongo3sharding\1 --port 37017 --shardsvr --smallfiles
mongod --replSet s0 --dbpath C:\Users\Marco\Desktop\mongo3sharding\2 --port 37018 --shardsvr --smallfiles
mongod --replSet s0 --dbpath C:\Users\Marco\Desktop\mongo3sharding\3 --port 37019 --shardsvr --smallfiles
mongo 127.0.0.1:37017
config = { _id: "s0", members:[
{ _id : 0, host : "localhost:37017" },
{ _id : 1, host : "localhost:37018" },
{ _id : 2, host : "localhost:37019" }]};
rs.initiate(config)
mongod --replSet s1 --dbpath C:\Users\Marco\Desktop\mongo3sharding\4 --port 47017 --shardsvr --smallfiles
mongod --replSet s1 --dbpath C:\Users\Marco\Desktop\mongo3sharding\5 --port 47018 --shardsvr --smallfiles
mongod --replSet s1 --dbpath C:\Users\Marco\Desktop\mongo3sharding\6 --port 47019 --shardsvr --smallfiles
mongo --port 47017
config = { _id: "s1", members:[
{ _id : 0, host : "localhost:47017" },
{ _id : 1, host : "localhost:47018" },
{ _id : 2, host : "localhost:47019" }]};
rs.initiate(config)
mongod --replSet s2 --dbpath C:\Users\Marco\Desktop\mongo3sharding\7 --port 57017 --shardsvr --smallfiles
mongod --replSet s2 --dbpath C:\Users\Marco\Desktop\mongo3sharding\8 --port 57018 --shardsvr --smallfiles
mongod --replSet s2 --dbpath C:\Users\Marco\Desktop\mongo3sharding\9 --port 57019 --shardsvr --smallfiles
mongo --port 57017
config = { _id: "s2", members:[
{ _id : 0, host : "localhost:57017" },
{ _id : 1, host : "localhost:57018" },
{ _id : 2, host : "localhost:57019" }]};
rs.initiate(config)
mongod --dbpath C:\Users\Marco\Desktop\mongo3sharding\c1 --port 57040 --configsvr --smallfiles
mongod --dbpath C:\Users\Marco\Desktop\mongo3sharding\c2 --port 57041 --configsvr --smallfiles
mongod --dbpath C:\Users\Marco\Desktop\mongo3sharding\c3 --port 57042 --configsvr --smallfiles
mongos --configdb localhost:57040,localhost:57041,localhost:57042
mongo
db.adminCommand( { addshard : "s0/localhost:37017" } );
db.adminCommand( { addshard : "s1/localhost:47017" } );
db.adminCommand( { addshard : "s2/localhost:57017" } );
sh.addShardTag('s0','shard0')
sh.addShardTag('s1','shard1')
sh.addShardTag('s2','shard2')
sh.addTagRange("mydatabase.parole",{sentiment:"anger"},{sentiment:"disgust"}, "shard0")
sh.addTagRange("mydatabase.parole",{sentiment:"fear"},{sentiment:"sadness"}, "shard1")
sh.addTagRange("mydatabase.parole",{sentiment:"surprise"},{sentiment:"trust"}, "shard2")sh.status ()命令的输出如下:
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5cd57e16918759db47284d69")
}
shards:
{ "_id" : "s0", "host" : "s0/localhost:37017,localhost:37018,localhost:37019", "tags" : [ "shard0" ] }
{ "_id" : "s1", "host" : "s1/localhost:47017,localhost:47018,localhost:47019", "tags" : [ "shard1" ] }
{ "_id" : "s2", "host" : "s2/localhost:57017,localhost:57018,localhost:57019", "tags" : [ "shard2" ] }
active mongoses:
"3.2.22" : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
2 : Success
databases:
{ "_id" : "mydatabase", "primary" : "s0", "partitioned" : true }
mydatabase.parole
shard key: { "sentiment" : 1 }
unique: false
balancing: true
chunks:
s0 2
s1 1
s2 1
{ "sentiment" : { "$minKey" : 1 } } -->> { "sentiment" : "anger" } on : s0 Timestamp(3, 1)
{ "sentiment" : "anger" } -->> { "sentiment" : "fear" } on : s0 Timestamp(1, 3)
{ "sentiment" : "fear" } -->> { "sentiment" : "surprise" } on : s1 Timestamp(2, 0)
{ "sentiment" : "surprise" } -->> { "sentiment" : { "$maxKey" : 1 } } on : s2 Timestamp(3, 0)
tag: shard0 { "sentiment" : "anger" } -->> { "sentiment" : "disgust" }
tag: shard1 { "sentiment" : "fear" } -->> { "sentiment" : "sadness" }
tag: shard2 { "sentiment" : "surprise" } -->> { "sentiment" : "trust" }我尝试使用这段python代码插入大量数据
import pymongo
from pymongo import MongoClient
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["parole"]
a=b=c=0
while a < 10000:
dic={"sentiment" : "anger" , "test" : a}
mycol.insert_one(dic)
dic={"sentiment" : "fear" , "test" : a}
mycol.insert_one(dic)
dic={"sentiment" : "surprise" , "test" : a}
mycol.insert_one(dic)
a+=1 插入和复制起作用,但分片不起作用。我理解这一点是因为文件夹1、2、3增加了它们的大小,但是文件夹4、5、6、7、8、9不改变它们的大小。
我能做什么?谢谢!问候
发布于 2019-07-14 21:50:29
我自己回答我的问题。分片不起作用,因为数据不够。使用命令
db.settings.save ({_id: "chunksize", value: 1})减小区块的大小
有关https://docs.mongodb.com/manual/tutorial/modify-chunk-size-in-sharded-cluster/中此命令的详细信息
https://stackoverflow.com/questions/56080340
复制相似问题