title: Could not create a collection in MongoDB Sharded Cluster other than db:admin?
body: <p>Recently I am setting up a MongoDB Sharded Cluster with version 5.0.6 by following the official documents from MongoDB and up to a point that I am able to create and shard collections in admin db.</p>
  <p>However, when I am trying to use other databases to  create collections, I am getting an error as below:</p>
  <pre><code>[direct: mongos] admin&gt; use demo
  switched to db demo
  [direct: mongos] demo&gt; db.createCollection(&quot;test&quot;)
  MongoServerError: Cannot accept sharding commands if not started with --shardsvr
  </code></pre>
  <p>This demo database has been enabled for sharding as below:</p>
  <pre><code>[direct: mongos] admin&gt; sh.status()
  shardingVersion
  {
    _id: 1,
    minCompatibleVersion: 5,
    currentVersion: 6,
    clusterId: ObjectId(&quot;62610026b29d0a3348769adc&quot;)
  }
  
  shards
  [
    {
      _id: 'rsA',
      host: 'rsA/demo2:27011,demo3:27011,demo4.nfcc.gov.my:27011',
      state: 1,
      topologyTime: Timestamp({ t: 1650524874, i: 4 })
    }
  ]
  ---
  active mongoses
  [ { '5.0.7': 2 } ]
  ---
  autosplit
  { 'Currently enabled': 'yes' }
  
  balancer
  {
    'Currently enabled': 'yes',
    'Currently running': 'no',
    'Failed balancer rounds in last 5 attempts': 5,
    'Last reported error': 'Request 2829483 timed out, deadline was 2022-05-30T15:                                                                                                                                                             57:32.036+08:00, op was RemoteCommand 2829483 -- target:[demo2:27011] db:admin expDate:2022-05-30T15:57:32.036+08:00 cmd:{ features: 1, maxTi                                                                                                                                                          meMS: 30000 }',
    'Time of Reported error': ISODate(&quot;2022-05-30T07:57:32.036Z&quot;),
    'Migration Results for the last 24 hours': 'No recent migrations'
  }
  
  databases
  [
    {
      database: { _id: 'config', primary: 'config', partitioned: true },
      collections: {
        'config.system.sessions': {
          shardKey: { _id: 1 },
          unique: false,
          balancing: true,
          chunkMetadata: [ { shard: 'rsA', nChunks: 1024 } ],
          chunks: [
            'too many chunks to print, use verbose if you want to force print'
          ],
          tags: []
        }
      }
    },
    {
      database: {
        _id: 'demo',
        primary: 'rsA',
        partitioned: true,
        version: {
          uuid: UUID(&quot;7ef98ae6-552b-4a6f-be2c-3342ec19b2da&quot;),
          timestamp: Timestamp({ t: 1652195331, i: 1 }),
          lastMod: 1
        }
      },
      collections: {}
    }
  ]
  </code></pre>
  <p>The rs.status() output are as below:</p>
  <pre><code>rsA [direct: primary] admin&gt; rs.status()
  {
    set: 'rsA',
    date: ISODate(&quot;2022-08-04T07:13:11.199Z&quot;),
    myState: 1,
    term: Long(&quot;9&quot;),
    syncSourceHost: '',
    syncSourceId: -1,
    heartbeatIntervalMillis: Long(&quot;2000&quot;),
    majorityVoteCount: 2,
    writeMajorityCount: 2,
    votingMembersCount: 3,
    writableVotingMembersCount: 3,
    optimes: {
      lastCommittedOpTime: { ts: Timestamp({ t: 1659597182, i: 1 }), t: Long(&quot;9&quot;) },
      lastCommittedWallTime: ISODate(&quot;2022-08-04T07:13:02.783Z&quot;),
      readConcernMajorityOpTime: { ts: Timestamp({ t: 1659597182, i: 1 }), t: Long(&quot;9&quot;) },
      appliedOpTime: { ts: Timestamp({ t: 1659597182, i: 1 }), t: Long(&quot;9&quot;) },
      durableOpTime: { ts: Timestamp({ t: 1659597182, i: 1 }), t: Long(&quot;9&quot;) },
      lastAppliedWallTime: ISODate(&quot;2022-08-04T07:13:02.783Z&quot;),
      lastDurableWallTime: ISODate(&quot;2022-08-04T07:13:02.783Z&quot;)
    },
    lastStableRecoveryTimestamp: Timestamp({ t: 1659597132, i: 1 }),
    electionCandidateMetrics: {
      lastElectionReason: 'electionTimeout',
      lastElectionDate: ISODate(&quot;2022-06-29T14:12:33.355Z&quot;),
      electionTerm: Long(&quot;9&quot;),
      lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1656511934, i: 1 }), t: Long(&quot;8&quot;) },
      lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1656511934, i: 1 }), t: Long(&quot;8&quot;) },
      numVotesNeeded: 2,
      priorityAtElection: 1,
      electionTimeoutMillis: Long(&quot;10000&quot;),
      priorPrimaryMemberId: 0,
      numCatchUpOps: Long(&quot;0&quot;),
      newTermStartDate: ISODate(&quot;2022-06-29T14:15:00.471Z&quot;),
      wMajorityWriteAvailabilityDate: ISODate(&quot;2022-06-29T14:15:02.472Z&quot;)
    },
    electionParticipantMetrics: {
      votedForCandidate: true,
      electionTerm: Long(&quot;8&quot;),
      lastVoteDate: ISODate(&quot;2022-05-30T07:59:18.999Z&quot;),
      electionCandidateMemberId: 0,
      voteReason: '',
      lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1653897530, i: 1 }), t: Long(&quot;6&quot;) },
      maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1653897530, i: 1 }), t: Long(&quot;6&quot;) },
      priorityAtElection: 1
    },
    members: [
      {
        _id: 0,
        name: 'demo2:27011',
        health: 1,
        state: 2,
        stateStr: 'SECONDARY',
        uptime: 3085085,
        optime: { ts: Timestamp({ t: 1659597182, i: 1 }), t: Long(&quot;9&quot;) },
        optimeDurable: { ts: Timestamp({ t: 1659597182, i: 1 }), t: Long(&quot;9&quot;) },
        optimeDate: ISODate(&quot;2022-08-04T07:13:02.000Z&quot;),
        optimeDurableDate: ISODate(&quot;2022-08-04T07:13:02.000Z&quot;),
        lastAppliedWallTime: ISODate(&quot;2022-08-04T07:13:02.783Z&quot;),
        lastDurableWallTime: ISODate(&quot;2022-08-04T07:13:02.783Z&quot;),
        lastHeartbeat: ISODate(&quot;2022-08-04T07:13:10.508Z&quot;),
        lastHeartbeatRecv: ISODate(&quot;2022-08-04T07:13:10.507Z&quot;),
        pingMs: Long(&quot;0&quot;),
        lastHeartbeatMessage: '',
        syncSourceHost: 'demo4:27011',
        syncSourceId: 2,
        infoMessage: '',
        configVersion: 1,
        configTerm: 9
      },
      {
        _id: 1,
        name: 'demo3:27011',
        health: 1,
        state: 1,
        stateStr: 'PRIMARY',
        uptime: 7357672,
        optime: { ts: Timestamp({ t: 1659597182, i: 1 }), t: Long(&quot;9&quot;) },
        optimeDate: ISODate(&quot;2022-08-04T07:13:02.000Z&quot;),
        lastAppliedWallTime: ISODate(&quot;2022-08-04T07:13:02.783Z&quot;),
        lastDurableWallTime: ISODate(&quot;2022-08-04T07:13:02.783Z&quot;),
        syncSourceHost: '',
        syncSourceId: -1,
        infoMessage: '',
        electionTime: Timestamp({ t: 1656512091, i: 1 }),
        electionDate: ISODate(&quot;2022-06-29T14:14:51.000Z&quot;),
        configVersion: 1,
        configTerm: 9,
        self: true,
        lastHeartbeatMessage: ''
      },
      {
        _id: 2,
        name: 'demo4:27011',
        health: 1,
        state: 2,
        stateStr: 'SECONDARY',
        uptime: 5699623,
        optime: { ts: Timestamp({ t: 1659597182, i: 1 }), t: Long(&quot;9&quot;) },
        optimeDurable: { ts: Timestamp({ t: 1659597182, i: 1 }), t: Long(&quot;9&quot;) },
        optimeDate: ISODate(&quot;2022-08-04T07:13:02.000Z&quot;),
        optimeDurableDate: ISODate(&quot;2022-08-04T07:13:02.000Z&quot;),
        lastAppliedWallTime: ISODate(&quot;2022-08-04T07:13:02.783Z&quot;),
        lastDurableWallTime: ISODate(&quot;2022-08-04T07:13:02.783Z&quot;),
        lastHeartbeat: ISODate(&quot;2022-08-04T07:13:09.590Z&quot;),
        lastHeartbeatRecv: ISODate(&quot;2022-08-04T07:13:09.589Z&quot;),
        pingMs: Long(&quot;0&quot;),
        lastHeartbeatMessage: '',
        syncSourceHost: 'demo3:27011',
        syncSourceId: 1,
        infoMessage: '',
        configVersion: 1,
        configTerm: 9
      }
    ],
    ok: 1,
    '$gleStats': {
      lastOpTime: Timestamp({ t: 0, i: 0 }),
      electionId: ObjectId(&quot;7fffffff0000000000000009&quot;)
    },
    lastCommittedOpTime: Timestamp({ t: 1659597182, i: 1 }),
    '$configServerState': { opTime: { ts: Timestamp({ t: 1659597185, i: 1 }), t: Long(&quot;-1&quot;) } },
    '$clusterTime': {
      clusterTime: Timestamp({ t: 1659597185, i: 1 }),
      signature: {
        hash: Binary(Buffer.from(&quot;5cf953d19db646d2022ea09e401c0d71490e2fad&quot;, &quot;hex&quot;), 0),
        keyId: Long(&quot;7088947451666628632&quot;)
      }
    },
    operationTime: Timestamp({ t: 1659597182, i: 1 })
  }
  </code></pre>
  <p>The MongoDB Sharded Cluster Configurations are as below:</p>
  <p><strong>Mongos Router</strong></p>
  <p>Run Command:</p>
  <pre><code>/usr/bin/mongos --config /opt/mongodb/config/mongos.conf
  </code></pre>
  <p>Configuration:</p>
  <pre><code>systemLog:
    destination: file
    logAppend: true
    path: /opt/mongodb/log/mongos.log
  
  # network interfaces
  net:
    port: 27012
    bindIp: 0.0.0.0
    tls:
      mode: requireTLS
      certificateKeyFile: /home/admin/ssl/demo.pem
      CAFile: /home/admin/ssl/ca.pem
  
  
  processManagement:
    timeZoneInfo: /usr/share/zoneinfo
  
  security:
    keyFile: /opt/mongodb/security/keyfile.pem
  
  
  sharding:
    configDB: rsA/demo2:27010,demo3:27010,demo4:27010
  
  </code></pre>
  <p><strong>Mongo Config Server</strong></p>
  <p>Run Command:</p>
  <pre><code>/usr/bin/mongod --config /opt/mongodb/config/mongocs.conf
  </code></pre>
  <p>Configuration:</p>
  <pre><code>storage:
    dbPath: /opt/mongodb/data/cs
    journal:
      enabled: true
  
  
  # where to write logging data.
  systemLog:
    destination: file
    logAppend: true
    path: /opt/mongodb/log/mongocs.log
  
  # network interfaces
  net:
    port: 27010
    bindIp: 0.0.0.0
    tls:
      mode: requireTLS
      certificateKeyFile: /home/admin/ssl/demo.pem
      CAFile: /home/admin/ssl/ca.pem
  
  # how the process runs
  processManagement:
    timeZoneInfo: /usr/share/zoneinfo
  
  security:
    authorization: enabled
    keyFile: /opt/mongodb/security/keyfile.pem
  #operationProfiling:
  
  replication:
    replSetName: rsA
  sharding:
    clusterRole: configsvr
  </code></pre>
  <p><strong>Mongo Shard Server</strong></p>
  <p>Run Command:</p>
  <pre><code>/usr/bin/mongod --config /opt/mongodb/config/mongoshrd.conf
  </code></pre>
  <p>Configuration:</p>
  <pre><code>storage:
    dbPath: /opt/mongodb/data/rsA
    journal:
      enabled: true
  #  engine:
  #  wiredTiger:
  
  # where to write logging data.
  systemLog:
    destination: file
    logAppend: true
    path: /var/log/mongodb/mongoshrd.log
  
  # network interfaces
  net:
    port: 27011
    bindIp: 0.0.0.0
    tls:
      mode: requireTLS
      certificateKeyFile: /home/admin/ssl/demo.pem
      CAFile: /home/admin/ssl/ca.pem
  
  # how the process runs
  processManagement:
    timeZoneInfo: /usr/share/zoneinfo
  
  security:
    authorization: enabled
    keyFile: /opt/mongodb/security/keyfile.pem
  #operationProfiling:
  
  replication:
    replSetName: rsA
  
  sharding:
    clusterRole: shardsvr
  </code></pre>
  <p>Any idea what went wrong or are there any missing steps?</p>
  <p>I am stucked with this error and unable to proceed with create a sharded collection in a shard enabled database.</p>