I have a current setup wherein I have 2 config servers and 2 shards and 1 router. I can see from the logs on all of them that they are all connected.
Now I created a new database called 'testdb'. I enabled sharding on 'testdb' and tried inserting into a collection in 'testdb'. When I try that, I get an error. Another weird thing is that I do not see the database 'testdb' when I try 'show dbs'. Below is a view of the terminal.
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
The status of config servers are:
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
}
The status of the shards:
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
}
Where am I going wrong?
I finally found the answer. The error was that I had the same replica set name for all the config servers and also the shard servers. In this situation, the router was trying to insert documents into the config server and not the shard server and hence it threw the error that the server was not started with the option --shardsvr.