0
votes

I have 4 servers each hosting a website and a mongo replica set.

  • MongoDB version: v3.4.13
  • Driver: PHP

Server 1 is PRIMARY.

  • Located on the west coast

Server 2 is SECONDARY with tag: { location: 'east' }

  • Located on the east coast

  • I'm connecting to the db with connection string: mongodb://localhost:27017/?replicaSet=rs&readPreference=nearest&readPreferenceTags=location:east

Server 3 and 4 are SECONDARY with no tags.

I want server 2 to read from it's local database, but instead it is reading from primary (or another secondary, I can't tell, but it's definitely not reading from it's local database)

  • I suspect it's not reading from it's own SECONDARY db because there is about a 3 second lag for any query I run.

How can I tell server 2 to read from it's own local SECONDARY db?

2
Can you clarify what you mean by local? This could either be the local database (where the replication oplog and other non-replicated collections live) or local data (i.e. reading from any other database). I suspect you mean the latter, but it's not clear from the current description. It would also be helpful to edit your description to include information on the driver & version you are connecting with as well as how you are verifying data is being returned from the expected server.Stennie
@Stennie You are correct, I want it to read from it's own local data so that reads are super fast. The db is on the east coast and I don't want it reading from the west coast db. I've updated my question.Andrew

2 Answers

0
votes

Include readPreferenceTags in your connection string. Refer to the online doc.

By the way, you shouldn't have a even number of nodes in a replica set unless one of them has no voting right.

0
votes

SOLUTION: All servers within the cluster need to be able to connect to all other servers within the cluster.

Turns out, some of my secondary's weren't able to connect to the other secondary's because of some rules placed in my iptables.

Once all servers within the cluster were able to connect to each other the speed increased dramatically.