插入分片的mongodb数据库时出错

时间:2017-10-02 05:31:02

标签: mongodb sharding

我有一个当前设置,其中我有2个配置服务器和2个分片和1个路由器。我可以从他们所有人的日志中看到他们都已连接 现在我创建了一个名为' testdb'的新数据库。我在' testdb'上启用了分片并尝试插入' testdb'中的集合。当我尝试时,我收到一个错误。另一个奇怪的事情是我没有看到数据库' testdb'当我尝试显示dbs'。下面是终端的视图。

mongos> use testdb
switched to db testdb
mongos> sh.enableSharding("testdb")
{ "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("59d1c43767d73804222be772")
}
  shards:
    {  "_id" : "ReplicaSet",  "host" : "ReplicaSet/192.168.1.3:20011,192.168.1.3:20012",  "state" : 1 }
  active mongoses:
    "3.4.1" : 1
 autosplit:
    Currently enabled: yes
  balancer:
    Currently enabled:  yes
    Currently running:  no
        Balancer lock taken at Mon Oct 02 2017 10:14:40 GMT+0530 (IST) by ConfigServer:Balancer
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours:
        No recent migrations
  databases:
    {  "_id" : "testdb",  "primary" : "ReplicaSet",  "partitioned" : true }

mongos> db.testcoll.insert({_id:1,name:"testvalue"})
WriteResult({
    "nInserted" : 0,
    "writeError" : {
        "code" : 193,
        "errmsg" : "Cannot accept sharding commands if not started with --shardsvr"
    }
})
mongos> show dbs
admin   0.000GB
config  0.000GB
mongos> db
testdb

配置服务器的状态为:

ReplicaSet:PRIMARY> rs.status()
{
    "set" : "ReplicaSet",
    "date" : ISODate("2017-10-02T05:33:42.973Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "configsvr" : true,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1506922413, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1506922413, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1506922413, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1506922413, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "192.168.1.3:20002",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 3048,
            "optime" : {
                "ts" : Timestamp(1506922413, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2017-10-02T05:33:33Z"),
            "electionTime" : Timestamp(1506919478, 1),
            "electionDate" : ISODate("2017-10-02T04:44:38Z"),
            "configVersion" : 1,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "192.168.1.3:20003",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 2954,
            "optime" : {
                "ts" : Timestamp(1506922413, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1506922413, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2017-10-02T05:33:33Z"),
            "optimeDurableDate" : ISODate("2017-10-02T05:33:33Z"),
            "lastHeartbeat" : ISODate("2017-10-02T05:33:42.557Z"),
            "lastHeartbeatRecv" : ISODate("2017-10-02T05:33:42.557Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "192.168.1.3:20002",
            "configVersion" : 1
        }
    ],
    "ok" : 1
}  

分片的状态:

ReplicaSet:PRIMARY> rs.status()
{
    "set" : "ReplicaSet",
    "date" : ISODate("2017-10-02T05:39:08.968Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1506922743, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1506922743, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1506922743, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "192.168.1.3:20011",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 3355,
            "optime" : {
                "ts" : Timestamp(1506922743, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2017-10-02T05:39:03Z"),
            "electionTime" : Timestamp(1506919442, 1),
            "electionDate" : ISODate("2017-10-02T04:44:02Z"),
            "configVersion" : 1,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "192.168.1.3:20012",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 3317,
            "optime" : {
                "ts" : Timestamp(1506922743, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1506922743, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2017-10-02T05:39:03Z"),
            "optimeDurableDate" : ISODate("2017-10-02T05:39:03Z"),
            "lastHeartbeat" : ISODate("2017-10-02T05:39:08.532Z"),
            "lastHeartbeatRecv" : ISODate("2017-10-02T05:39:07.254Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "192.168.1.3:20011",
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

我哪里错了?

2 个答案:

答案 0 :(得分:2)

我终于找到了答案。错误是我对所有配置服务器以及分片服务器都有相同的副本集名称。在这种情况下,路由器试图将文档插入配置服务器而不是分片服务器,因此它使用选项--shardsvr抛出了服务器未启动的错误。

答案 1 :(得分:0)

我在mongo 4.2上遇到了同样的问题,我的副本集名称不一样,类似于:rs_dbname_01,rs_dbname_02,rs_dbname_03,因此我将名称更改为:rs1,rs2,rs3,最后我将其修复了...也许mongo可以将副本集名称的长度截短或用符号“ _”剪掉

相关问题