如何在 mongodb 集群中添加分片?

How can I add shard at mongodb cluster?

我在我的 debian 机器上设置了 mongodb 3.2 集群,如下配置

# Config Server - mongod.conf

storage:
  dbPath: /data/mongodb
  journal:
    enabled: true

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 27019
  bindIp: 0.0.0.0

replication:
 replSetName: rs0

sharding:
 clusterRole: "configsvr"


# Shard Server 1 - mongod.conf

storage:
  dbPath: /data/mongodb
  journal:
    enabled: true

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 27017
  bindIp: 0.0.0.0

replication:
 replSetName: rs0


# Shard Server 2 - mongod.conf

storage:
  dbPath: /data/mongodb
  journal:
    enabled: true

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 27017
  bindIp: 0.0.0.0

replication:
 replSetName: rs0

和运行 mongos 在查询路由器服务器上添加分片

$)mongos --configdb mongo-config:27019

但是我无法添加分片。

mongos> sh.addShard("rs0/mongo-db1:27017")
{
    "ok" : 0,
    "errmsg" : "Cannot add rs0/mongo-db1:27017 as a shard since it is part of a config server replica set",
    "code" : 96
}
mongos> sh.addShard("rs0/mongo-db1:27017")
{
    "ok" : 0,
    "errmsg" : "Surprised to discover that mongo-db1:27017 does not believe it is a config server",
    "code" : 72
}

getSharmap 命令。

mongos> db.runCommand("getShardMap");
{
    "map" : {
        "config" : "rs0/mongo-db1:27017,mongo-db2:27017",
        "mongo-db1:27017" : "rs0/mongo-db1:27017,mongo-db2:27017",
        "mongo-db2:27017" : "rs0/mongo-db1:27017,mongo-db2:27017",
        "rs0/mongo-config:27019" : "rs0/mongo-config:27019",
        "rs0/mongo-db1:27017,mongo-db2:27017" : "rs0/mongo-db1:27017,mongo-db2:27017"
    },
    "ok" : 1
}

这里是 mongos 日志

2016-04-06T15:05:14.243+0000 I SHARDING [Balancer] 即将将元数据事件记录到操作日志中:{ _id: "mongo-router1-2016-04-06T15:05:14.243+0000-570525aa6b2b3378bb9daf56", server: "mongo-router1", clientAddr: "" , time: new Date(1459955114243), what: "balancer.round", ns: "", details: { executionTimeMillis: 3, errorOccured: true, errmsg: "could not get updated shard list from config server due to Surprised to discover that mongo-db1:27017 does not believe it is a config server" } }

我错过了什么.?

由于您尝试添加的分片位于 rs0 副本集中,并且由于 rs0 拥有您的配置服务器,因此它不会让您创建分片。尝试将其添加到不同的副本集(例如 rs1),这应该会起作用。