Connect an application to a MongoDB cluster

The Bitnami Multi-Tier Solution for MongoDB creates a replica set consisting of one primary and a variable number of secondary nodes (which could include an optional arbiter node). In the event of primary node failure, a secondary node will be elected as the new primary and take the place of the original primary. This ensures data availability and redundancy for connected applications.

When connecting an application to the Bitnami Multi-Tier Solution for MongoDB, keep the following points in mind:

  • Construct the MongoDB connection URI so that it contains the IP addresses of all the nodes in the cluster. This ensures that even if the primary node fails and is replaced by a secondary node, the application will still be able to read data from, and write data to, the MongoDB cluster.

    For details on how to pass the MongoDB connection string to your application, refer to the documentation for your application’s MongoDB driver. As an example, here is the connection string passed to the PHP MongoDB driver:

    $client = new \MongoDB\Client("mongodb://username:password@,,,");

    Read more about the MongoDB standard connection string format in the MongoDB documentation.

  • Ensure that the application is able to connect to each cluster node using its public or private IP address. To ensure connectivity, you have the following use cases and solutions (these are shown in order of preference, from the most secure to the least secure):

    • Option 1: Host the application in the same network as the MongoDB cluster and connect both instances internally. To do so, follow the steps shown in the Share a virtual network to join instances from the same resource group how-to guide. This is the recommended configuration for production environments.
    • Option 2: Host the application in a different network and connect to the database server by enabling a Virtual Network peering. Refer to this guide to learn how to enable Vnet peering to join instances deployed in different resource groups.
    • Option 3: Host the application in a different server for development purposes; we recommend connecting to the database cluster through an SSH tunnel. Refer to the FAQ for more information on this.
    • Option 4: Open the MongoDB port in each cluster node’s firewall and create and assign a public IP address to each cluster node so that the application can address each node using its public IP address. Then, connect to the MongoDB server using the CLI and update the cluster configuration to use the new public IP addresses by executing the commands shown below. Replace the IP-ADDRESS-NODE-X and PORT placeholders with the corresponding public IP address and port for each node:

      cfg = rs.conf()
      cfg.members[0].host = "IP-ADDRESS-NODE-0:PORT"
      cfg.members[1].host = "IP-ADDRESS-NODE-1:PORT
      cfg.members[2].host = "IP-ADDRESS-NODE-2:PORT

    When opening the MongoDB port in the server firewall under this option, it is strongly recommended to specify a trusted IP address range for incoming connections. Check how to configure the appropriate firewall rules section to know how to allow traffic to your server only from trusted sources.

Last modification September 6, 2018