Pixel1002
Pixel1002

Reputation: 196

How to configure Doctrine for MongoDB master/slave replica to automatically failover

I have two MongoDB instances - one slave and one master. There is an Arbiter too.

Basically the setup is the following: enter image description here (Image Source: https://docs.mongodb.com/manual/replication/)

In my Symfony application, in config.yml I have this for MongoDB:

# Mongo DB
doctrine_mongodb:
connections:
    default:
        server: %database_mongodb_access%
        options: {}
default_database: %database_mongodb_name%
document_managers:
    default:
        auto_mapping: true

where %database_mongodb_access% is 'mongodb://mongo:27017'.

How to configure my Symfony/Doctrine application to automatically failover to the slave MongoDB instance if the primary fails, so the application continues working?

It is not clear to me from the docs: http://symfony.com/doc/master/bundles/DoctrineMongoDBBundle/config.html

Upvotes: 1

Views: 2249

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

It is possible to connect to several mongodb servers on one connection if you are using a replica set by listing all of the servers within the connection string as a comma separated list.

doctrine_mongodb:
    # ...
    connections:
        default:
            server: "mongodb://mongodb-01:27017,mongodb-02:27017,mongodb-03:27017"

http://symfony.com/doc/master/bundles/DoctrineMongoDBBundle/config.html#connecting-to-a-pool-of-mongodb-servers-on-1-connection

And check the retry mechanism for failover :

http://symfony.com/doc/master/bundles/DoctrineMongoDBBundle/config.html#retrying-connections-and-queries

Upvotes: 1

Related Questions