Could not find host matching read preference { mode: "primary" }

I am getting the error while adding shard as below:

Command
sh.addShard("test-shardsrv-replica/shard1:27018");

Error
MongoServerError: Could not find host matching read preference { mode: "primary" } for set test-shardsrv-replica

My Shard Replica Set Status

{
  set: 'test-shardsrv-replica',
  date: ISODate("2023-06-29T13:42:41.974Z"),
  myState: 1,
  term: Long("4"),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 1,
  writeMajorityCount: 1,
  votingMembersCount: 1,
  writableVotingMembersCount: 1,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1688046154, i: 1 }), t: Long("4") },
    lastCommittedWallTime: ISODate("2023-06-29T13:42:34.937Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1688046154, i: 1 }), t: Long("4") },
    appliedOpTime: { ts: Timestamp({ t: 1688046154, i: 1 }), t: Long("4") },
    durableOpTime: { ts: Timestamp({ t: 1688046154, i: 1 }), t: Long("4") },
    lastAppliedWallTime: ISODate("2023-06-29T13:42:34.937Z"),
    lastDurableWallTime: ISODate("2023-06-29T13:42:34.937Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1688046144, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate("2023-06-28T07:46:23.215Z"),
    electionTerm: Long("4"),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1687938379, i: 1 }), t: Long("3") },
    numVotesNeeded: 1,
    priorityAtElection: 1,
    electionTimeoutMillis: Long("10000"),
    newTermStartDate: ISODate("2023-06-28T07:46:23.216Z"),
    wMajorityWriteAvailabilityDate: ISODate("2023-06-28T07:46:23.217Z")
  },
  members: [
    {
      _id: 0,
      name: 'shard1:27018',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 107779,
      optime: { ts: Timestamp({ t: 1688046154, i: 1 }), t: Long("4") },
      optimeDate: ISODate("2023-06-29T13:42:34.000Z"),
      lastAppliedWallTime: ISODate("2023-06-29T13:42:34.937Z"),
      lastDurableWallTime: ISODate("2023-06-29T13:42:34.937Z"),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1687938383, i: 1 }),
      electionDate: ISODate("2023-06-28T07:46:23.000Z"),
      configVersion: 59857,
      configTerm: -1,
      self: true,
      lastHeartbeatMessage: ''
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1688046154, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1688046154, i: 1 })
}

Any Help would be Appreciated, Thanks!

Hi @Jay_Bhanushali1,
What are you trying to do?
You’re trying to add a new member in a replica set of shard or you’ re trying to add a new shard?
In the first case, you should use the following command:

rs.add(hostname:Port)

In the second case, you should use the following command:

sh.add(hostname:Port) 

and not

sh.add(name_replica/hostname:Port)

Regards

is “shard1” a resolvable name?

Yes it is. Also, now I am not facing this issue (used security and authentication using keyFile within each instance of config server, shard and router) but another one.
I am unable to run any mongosh command as it shows below
MongoServerError: command createUser requires authentication

Have you initialised config servers?
What does rs.statu() show?
Have you created user and able to login with authenticate to admin db
Are clusterrole param ok in your config file under sharding for config&shard servers

Hi @Jay_Bhanushali1

Your first user needs to be created on the config replicaset. As you have enabled authorization you need to use the localhost exception to create the first user.

Run mongosh directly on the mongos server (it has to connect on localhost) and create your user in the admin database. Also create an admin user on each shard replicaset, the localhost exception will remain enabled until this is done(or explicitly disabled on the command line).

1 Like

Hi @chris,
I am able to create first user in shard and config server, however unable to do so in mongos instance as it gives me the error:
MongoServerError: command createUser requires authentication
I am running below command to use localhost exception
mongosh localhost

Hi @Kobe_W , yes it is. I tried to telnet and it was connecting to it

I think mongos uses user details from config server
Did you try to login to mongos using the user you created on config db?

1 Like

@Ramachandra_Tummala is correct here. The config replicaSet is used by mongos to authenticate users using the cluster.

You should be able to authenticate to mongos using the already created users and create additional users and roles for the cluster.

Hi @Ramachandra_Tummala , @chris ,
Yes i am able to use users from config server. I was using wrong password. Also, now I am getting ssl Handshake issue when i try to addShard from mongos.
MongoServerError: failed to run command { isMaster: 1 } when attempting to add shard shard1:27018 :: caused by :: HostUnreachable: Error connecting to shard1:27018 :: caused by :: Could not find address for shard1:27018: SocketException: Host not found (non-authoritative), try again later
I am able to telnet to shard1:27018. I tried using sslMode : disabled but still gives same error

Looks like you have moved to a host lookup issue.

From your host or the config server? All shard and config servers need to be able to resolve and connect to each other the mongos needs to be able to resolve and connect to both the shards and the config.

Hi @chris , thank you so much for your help. I wasn’t able to connect to shard from config server. I fixed it and now able to run the addShard.

1 Like