0

我在本地机器(Windows)中有 mongodb 分片集群。但我不明白如何测试这个。

我的配置是:

复制集 1:

mongod --replSet s0  --dbpath E:/mongo/data/shard0/rs0 --port 37017 --shardsvr 
mongod --replSet s0  --dbpath E:/mongo/data/shard0/rs1 --port 37018 --shardsvr
mongod --replSet s0  --dbpath E:/mongo/data/shard0/rs2 --port 37019 --shardsvr 

mongo --port 37017

config = { _id: "s0", members:[
          { _id : 0, host : "localhost:37017" },
          { _id : 1, host : "localhost:37018" },
          { _id : 2, host : "localhost:37019" }]};

rs.initiate(config)

副本集 2:

mongod --replSet s1   --dbpath E:/mongo/data/shard1/rs0 --port 47017   --shardsvr 
mongod --replSet s1  --dbpath E:/mongo/data/shard1/rs1 --port 47018   --shardsvr  
mongod --replSet s1  --dbpath E:/mongo/data/shard1/rs2 --port 47019   --shardsvr  

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)

副本集 3:

mongod --replSet s2  --dbpath E:/mongo/data/shard2/rs0 --port 57017   --shardsvr  
mongod --replSet s2  --dbpath E:/mongo/data/shard2/rs1 --port 57018   --shardsvr  
mongod --replSet s2  --dbpath E:/mongo/data/shard2/rs2 --port 57019   --shardsvr  

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 E:/mongo/data/config/config-a  --replSet conf --port 57040   --configsvr  
mongod  --dbpath E:/mongo/data/config/config-b --replSet conf --port 57041   --configsvr  
mongod  --dbpath E:/mongo/data/config/config-c --replSet conf --port 57042   --configsvr 

mongo --port 57040

config = { _id: "conf", members:[
          { _id : 0, host : "localhost:57040" },
          { _id : 1, host : "localhost:57041" },
          { _id : 2, host : "localhost:57042" }]};

rs.initiate(config)

连接到集群:

mongos  --configdb conf/localhost:57040,localhost:57041,localhost:57042  --port 57050

mongo --port 57050

添加分片:

db.adminCommand( { addshard : "s0/"+"localhost:37017" } ); 
db.adminCommand( { addshard : "s1/"+"localhost:47017" } );
db.adminCommand( { addshard : "s2/"+"localhost:57017" } );

使用分片键在数据库和集合上启用分片:

db.adminCommand({enableSharding: "chat"});
db.adminCommand({shardCollection: "chat.user", key: {userId:1}});

以下是当我使用命令 db.printShardingStatus() 检查分片状态时的日志;

--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5dfa6c3cb121a735f9ad8f6e")
  }
  shards:
        {  "_id" : "s0",  "host" : "s0/localhost:37017,localhost:37018,localhost
:37019",  "state" : 1 }
        {  "_id" : "s1",  "host" : "s1/localhost:47017,localhost:47018,localhost
:47019",  "state" : 1 }
        {  "_id" : "s2",  "host" : "s2/localhost:57017,localhost:57018,localhost
:57019",  "state" : 1 }
  active mongoses:
        "4.2.1" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "chat",  "primary" : "s1",  "partitioned" : true,  "ve
rsion" : {  "uuid" : UUID("233a3161-e3d0-4e49-8051-9ebe0580f5bb"),  "lastMod" :
1 } }
                chat.user
                        shard key: { "userId" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                s1      1
                        { "userId" : { "$minKey" : 1 } } -->> { "userId" : { "$m
axKey" : 1 } } on : s1 Timestamp(1, 0)
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                s0      1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey"
 : 1 } } on : s0 Timestamp(1, 0)

我已将 100k 文档插入到在 57050 上运行的数据库的用户集合中。整个数据仅存储在一个分片中,即第二个副本集(s1)。我不明白分片是如何在这里工作的。它不会将所有碎片分开。

当我检查 cache.databases 集合中的 configdb mongos(57040) 时,我只看到一个副本集,如下所示。

"_id" : "chat",
    "primary" : "s1",
    "partitioned" : true,
    "version" : {
        "uuid" : UUID("233a3161-e3d0-4e49-8051-9ebe0580f5bb"),
        "lastMod" : 1
    }

稍后,如果我删除聊天数据库,我现在将在它存储的其他一些分片中创建新数据库。每次我们创建数据库时,它都会采用随机副本集。

4

0 回答 0