DoctrineMongoDBBundle icon indicating copy to clipboard operation
DoctrineMongoDBBundle copied to clipboard

Replica set connection issue

Open ppounder opened this issue 9 years ago • 6 comments

Hi there,

I'm trying to connect to my replica set within Symfony. I have my config settings as follows as per http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/config.html#connecting-to-a-pool-of-mongodb-servers-on-1-connection

connections:
    default:
        server: 'mongodb://*.*.*.1:27017,*.*.*.2:27017,*.*.*.3:27017'

However I get the error: Uncaught PHP Exception MongoException: "Cannot run command count(): not master and slaveOk=false"

Let me add a bit of context. Primary is *.*.*.1 and I want to be able to connect via *.*.*.2 or *.*.*.3 if primary goes down and one of the secondaries then become primary. If I go with just the primary mongo server IP address, instead of a comma'd list of my servers, I can connect fine, but that fails if *.*.*.1 goes down and *.*.*.2 becomes primary for example.

Has anyone any suggestions?

Regards Paul Pounder

ppounder avatar Jul 11 '16 09:07 ppounder

To add further, I've added readPreference: primary but still getting the same issue. Seems to round robin all my requests to each server in the list, not knowing which is primary.

Does the replicaSet: repl-set-name have to be configured? Will this tell my config which is primary?

ppounder avatar Aug 25 '16 15:08 ppounder

FWIW I'm using following DSN: u:p@somewhere-s1:29629,somewhere-s2:29629/?replicaSet=rs-name&readPreference=primaryPreferred and it's working as expected so yes, I'd try adding replicaSet

malarzm avatar Aug 26 '16 06:08 malarzm

Good news is that adding replicaSet has resolved our issue. However if you don't add replicaSet config setting it seems to ignore readPreferences and round robins to the mongo server names listed in the server config setting. Wonder whether this needs a documentation update or is my reading of this incorrect?

ppounder avatar Aug 26 '16 10:08 ppounder

I think this is just how mongo driver behaves but I don't mind adding a tip in our docs if we have something more on replica sets :)

malarzm avatar Aug 26 '16 12:08 malarzm

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 21 '19 07:01 stale[bot]

I believe adding a link to the connection string reference in the MongoDB docs should help in the future. Adding to the roadmap.

alcaeus avatar Jan 21 '19 15:01 alcaeus