Could not find host matching read preference { mode: “primary” } for set m103-repl

Hi All,

For the users facing the following error, please look at the below given possible solutions that have worked for users.

Error: Client experienced a timeout when connecting to ‘m103-repl’

Possible Solutions:

  1. You need to check if your replica sets and config servers are configured correctly as below:

  2. Mongos - 26000

  3. Config server: 26001, 26002, 26003

  4. Shard1: m103-repl - 27001, 27002, 27003

  5. Shard2: m103-repl-2 - 27004, 27005, 27006

  6. Check if all your nodes are up and running and also check that sharding is enabled for m103-repl replica set.

  7. Check if you are using the replica set name (m103-repl) and the hostnames being used are correctly mapped to the IP addresses in /etc/hosts file.

  8. If you are using hostname and the mapping is correct too, try replacing the hostname with IP address to make it work.

  9. Check if replica set is initiated and all nodes are added in the replica set.

  10. Check for authentication in mongos configuration file and also take a look at mongos log file for debugging.

  11. There are another few probable causes for this error, please check as below:

You cannot contact the replica set, because there is no primary.
There is no primary because 2/3 replicas are down.
The last remaining host is and stays a secondary.
To connect to a secondary you have to adjust your connection string.

If nothing works, then please share the configuration or post your issue in the forum so that I can help more.

Happy Learning!

Thanks,
Muskan
Curriculum Support Engineer

I am getting this error:
“ok” : 0,
“errmsg” : “Could not find host matching read preference { mode: “primary” } for set m103-repl”,
“code” : 133,
“codeName” : “FailedToSatisfyReadPreference”,
“operationTime” : Timestamp(1575287644, 1),
“$clusterTime” : {
“clusterTime” : Timestamp(1575287644, 1),
“signature” : {
“hash” : BinData(0,“1ZbrBq54MK2ZP6iKq+3ccviI5D0=”),
“keyId” : NumberLong(“6765793549674872858”)
}
}

@GAURAV_12451 suggest you create a new thread making sure it’s linked to the course.

Hi @GAURAV_12451,

Can you please make sure you have followed all the instructions properly mentioned in the post #1 ?

Please connect to the m103-repl replica set and run the rs.status() command and share the output.

Thanks,
Shubham Ranjan
Curriculum Support Engineer

Yes I have check for all things done in post 1.

Output from rs.isMaster():
MongoDB Enterprise m103-repl:PRIMARY> rs.isMaster()
{
“hosts” : [
“m103:27001”,
“m103:27002”,
“m103:27003”
],
“setName” : “m103-repl”,
“setVersion” : 8,
“ismaster” : true,
“secondary” : false,
“primary” : “m103:27001”,
“me” : “m103:27001”,
“electionId” : ObjectId(“7fffffff000000000000000b”),
“lastWrite” : {
“opTime” : {
“ts” : Timestamp(1575352895, 1),
“t” : NumberLong(11)
},
“lastWriteDate” : ISODate(“2019-12-03T06:01:35Z”),
“majorityOpTime” : {
“ts” : Timestamp(1575352895, 1),
“t” : NumberLong(11)
},
“majorityWriteDate” : ISODate(“2019-12-03T06:01:35Z”)
},
“maxBsonObjectSize” : 16777216,
“maxMessageSizeBytes” : 48000000,
“maxWriteBatchSize” : 100000,
“localTime” : ISODate(“2019-12-03T06:01:42.924Z”),
“logicalSessionTimeoutMinutes” : 30,
“minWireVersion” : 0,
“maxWireVersion” : 6,
“readOnly” : false,
“ok” : 1,
“operationTime” : Timestamp(1575352895, 1),
“$clusterTime” : {
“clusterTime” : Timestamp(1575352895, 1),
“signature” : {
“hash” : BinData(0,“NwXUlFFD7i2hh5I1ns/1Y0Xk7nE=”),
“keyId” : NumberLong(“6763504838912245761”)
}
}
}

Hi @GAURAV_12451,

Please run this command in your vagrant environment and share the output.

ping m103

Thanks,
Shubham Ranjan
Curriculum Support Engineer

vagrant@m103:~$ ping m103
PING m103.mongodb.university (127.0.1.1) 56(84) bytes of data.
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=3 ttl=64 time=0.026 ms
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=4 ttl=64 time=0.057 ms
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=5 ttl=64 time=0.029 ms
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=6 ttl=64 time=0.069 ms
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=7 ttl=64 time=0.090 ms
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=8 ttl=64 time=0.027 ms
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=9 ttl=64 time=0.053 ms
64 bytes from m103.mongodb.university (127.0.1.1): icmp_seq=10 ttl=64 time=0.032 ms

Hi @Shubham_Ranjan,
Can you please help me with this as today is the last day of my course.
Otherwise I have no other option then to left the course at this stage

Thanks

Hi @GAURAV_12451,

It seems that the hostname m103 is not getting mapped to 192.168.103.100.

Can you please follow the steps mentioned below and update the mapping in your /etc/hosts file ?

You can do this using the vim editor. Follow the steps mentioned below.

sudo vim /etc/hosts

This will open the hosts file in your terminal.
Now press i and hit enter to enter the insert mode

Add an entry -> 192.168.103.100 m103.mongodb.university

Also make sure m103.mongodb.university is not mapped to any other IP address. If so then remove it.

Press esc
Press :x and enter to save and exit

Please make sure all the nodes in m103-repl replica set is up and running when you are trying to add the replica set to the sharded cluster.

In addition to this, Please share the following information :

  • Configuration file of any node in m103-repl replica set.
  • Commands you are using to add the replica set in your Sharded cluster.

Thanks,
Shubham Ranjan
Curriculum Support Engineer

I have updated the hosts file in the terminal inside vagrant ssh.
config file for m103-repl-1 from the set:

sharding:
clusterRole: shardsvr
storage:
dbPath: /var/mongodb/db/1
wiredTiger:
engineConfig:
cacheSizeGB: .1
net:
bindIp: 192.168.103.100,localhost
port: 27001
security:
authorization: enabled
keyFile: /var/mongodb/pki/m103-keyfile
systemLog:
destination: file
path: /var/mongodb/db/mongod1.log
logAppend: true
processManagement:
fork: true
replication:2
replSetName: m103-repl

For adding the sharded cluster I am using :
vagrant@m103:~$ mongo --port 26000 --username m103-admin --password m103-pass --authenticationDatabase admin
MongoDB Enterprise mongos> sh.addShard(“m103-repl/192.168.103.100:27001”)

this also doesn’t work:
MongoDB Enterprise mongos> sh.addShard(“m103-repl/m103.mongodb.university:27001”)

The m103-repl set is up and running:
MongoDB Enterprise m103-repl:PRIMARY> rs.isMaster()
{
“hosts” : [
“m103:27001”,
“m103:27002”,
“m103:27003”
],
“setName” : “m103-repl”,
“setVersion” : 8,
“ismaster” : true,
“secondary” : false,
“primary” : “m103:27001”,
“me” : “m103:27001”,
“electionId” : ObjectId(“7fffffff000000000000000d”),
“lastWrite” : {
“opTime” : {
“ts” : Timestamp(1575367138, 1),
“t” : NumberLong(13)
},
“lastWriteDate” : ISODate(“2019-12-03T09:58:58Z”),
“majorityOpTime” : {
“ts” : Timestamp(1575367068, 1),
“t” : NumberLong(13)
},
“majorityWriteDate” : ISODate(“2019-12-03T09:57:48Z”)
},
“maxBsonObjectSize” : 16777216,
“maxMessageSizeBytes” : 48000000,
“maxWriteBatchSize” : 100000,
“localTime” : ISODate(“2019-12-03T09:59:03.123Z”),
“logicalSessionTimeoutMinutes” : 30,
“minWireVersion” : 0,
“maxWireVersion” : 6,
“readOnly” : false,
“ok” : 1,
“operationTime” : Timestamp(1575367138, 1),
“$clusterTime” : {
“clusterTime” : Timestamp(1575367138, 1),
“signature” : {
“hash” : BinData(0,“BM/m91P1l+1Sdoy1yEq61glEOEs=”),
“keyId” : NumberLong(“6763504838912245761”)
}
}
}

Hi @GAURAV_12451,

For some reason, the mongos process in not able to reach the primary of the m103-repl replica set. You recently made some changes in the /etc/hosts file. I will recommend you to kill all the processes once and restart them again.

If it does not work then please share the following information :

  • content of the mongos.log file
  • output of this command -> ping m103

Thanks,
Shubham Ranjan
Curriculum Support Engineer

I am stuck with this error and could not move furthur please help me with this .

errmsg" : “Could not find host matching read preference { mode: “primary” } for set m103-repl”,

the image of my config server file is:
image

And the image of my shard server file is:
image

The output of rs.status() is:

{
“set” : “m103-csrs”,
“date” : ISODate(“2019-12-13T16:04:44.797Z”),
“myState” : 1,
“term” : NumberLong(24),
“syncingTo” : “”,
“syncSourceHost” : “”,
“syncSourceId” : -1,
“configsvr” : true,
“heartbeatIntervalMillis” : NumberLong(2000),
“optimes” : {
“lastCommittedOpTime” : {
“ts” : Timestamp(1576253078, 1),
“t” : NumberLong(24)
},
“readConcernMajorityOpTime” : {
“ts” : Timestamp(1576253078, 1),
“t” : NumberLong(24)
},
“appliedOpTime” : {
“ts” : Timestamp(1576253078, 1),
“t” : NumberLong(24)
},
“durableOpTime” : {
“ts” : Timestamp(1576253078, 1),
“t” : NumberLong(24)
}
},
“members” : [
{
“_id” : 0,
“name” : “192.168.103.100:26001”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”,
“uptime” : 742,
“optime” : {
“ts” : Timestamp(1576253078, 1),
“t” : NumberLong(24)
},
“optimeDate” : ISODate(“2019-12-13T16:04:38Z”),
“syncingTo” : “”,
“syncSourceHost” : “”,
“syncSourceId” : -1,
“infoMessage” : “”,
“electionTime” : Timestamp(1576252354, 1),
“electionDate” : ISODate(“2019-12-13T15:52:34Z”),
“configVersion” : 11,
“self” : true,
“lastHeartbeatMessage” : “”
},
{
“_id” : 2,
“name” : “192.168.103.100:26003”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 731,
“optime” : {
“ts” : Timestamp(1576253078, 1),
“t” : NumberLong(24)
},
“optimeDurable” : {
“ts” : Timestamp(1576253078, 1),
“t” : NumberLong(24)
},
“optimeDate” : ISODate(“2019-12-13T16:04:38Z”),
“optimeDurableDate” : ISODate(“2019-12-13T16:04:38Z”),
“lastHeartbeat” : ISODate(“2019-12-13T16:04:44.598Z”),
“lastHeartbeatRecv” : ISODate(“2019-12-13T16:04:44.597Z”),
“pingMs” : NumberLong(0),
“lastHeartbeatMessage” : “”,
“syncingTo” : “192.168.103.100:26001”,
“syncSourceHost” : “192.168.103.100:26001”,
“syncSourceId” : 0,
“infoMessage” : “”,
“configVersion” : 11
},
{
“_id” : 3,
“name” : “192.168.103.100:26002”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 735,
“optime” : {
“ts” : Timestamp(1576253078, 1),
“t” : NumberLong(24)
},
“optimeDurable” : {
“ts” : Timestamp(1576253078, 1),
“t” : NumberLong(24)
},
“optimeDate” : ISODate(“2019-12-13T16:04:38Z”),
“optimeDurableDate” : ISODate(“2019-12-13T16:04:38Z”),
“lastHeartbeat” : ISODate(“2019-12-13T16:04:44.549Z”),
“lastHeartbeatRecv” : ISODate(“2019-12-13T16:04:43.161Z”),
“pingMs” : NumberLong(0),
“lastHeartbeatMessage” : “”,
“syncingTo” : “192.168.103.100:26003”,
“syncSourceHost” : “192.168.103.100:26003”,
“syncSourceId” : 2,
“infoMessage” : “”,
“configVersion” : 11
}
],
“ok” : 1,
“operationTime” : Timestamp(1576253078, 1),
“$gleStats” : {
“lastOpTime” : Timestamp(0, 0),
“electionId” : ObjectId(“7fffffff0000000000000018”)
},
“$clusterTime” : {
“clusterTime” : Timestamp(1576253078, 1),
“signature” : {
“hash” : BinData(0,“EaGw5FQGz5LrBI25TnHYqenyd6s=”),
“keyId” : NumberLong(“6766552105323855898”)
}
}
}

@Sanskar_83502 what command did you run when you encountered this error? If it’s sh.addShard() make sure you’re using the host name of the primary shard or include all three host names.

Otherwise, let’s see the output of the mongos log file like @Shubham_Ranjan requested.

yes its sh.addShrad()
@007_jb

here my mongos log file:

I have tried all the possible solutions stated above.
Please help me to solve this issue.

And this?

image
This is the mongos log file.

Lastly, show a screenshot of your sh.addShard() command including the error message.