Skip to main content
Skip table of contents

Create MongoDB Multi-Host Connections

Overview

MongoDB commonly employs a multi-host structure, such as a Replica Set or Sharded Cluster (Sharding), instead of a single-host configuration. When dealing with these setups, it is essential to create connections in a multi-host fashion.

Create a MongoDB Connection

1. Understand the Basic Connection String Structure of MongoDB

The basic MongoDB Connection String structure is outlined below:

CODE
mongodb://[Hostname or IP]:[port],[Hostname or IP]:[port],[Hostname or IP]:[port]..../?options
  • mongodb:// : Standard connection string scheme.

  • Hostname or IP : The full domain name or IP address of the MongoDB cluster member host.

  • Options : /? Options.

If you used a replica set, you must enter /? followed by replicaSet=[replica set name]. For example, /?replicaSet=rs0.

2. Create a MongoDB Connection

image-20240127-140739.png

Settings > Database Settings > DB Connections > Create Connection > Select a data source

  1. From the Database Settings menu, navigate to the DB Connections menu.

  2. Click the Create Connection button located in the top right corner.

  3. In the Select data source step, select MongoDB.

3. Set Up as a Replica Set

스크린샷 2024-01-27 오후 11.18.40.png

Settings > Database Settings > DB Connections > Create Connection > Fill out for configuration

  1. Turn the Cluster switch on to enable cluster input

  2. Enter the following information:

    1. Type : The area exposed to connection information, typically retaining the value "Primary" because the primary role can change dynamically within a MongoDB replica set or cluster.

    2. Connection String : Enter a standard connection string.

      1. For replica set, append /?replicaSet=[replica set name] at the end, as the replica set name is required.

    3. Click the Add Instance button to include each member host as an instance.

      1. If your cluster comprises three hosts, input each host in the format mongodb://hostname (domain address or IP):[port], followed by the appropriate Instance Name.

      2. Expose is a value that determines whether to display the connection information.

    4. Authentication DB : Specify the path to the Authentication DB.

    5. Username & Password : Input the credentials (username and password) for the corresponding connection database.

  3. Click the Test Connection button to ensure that the access information is valid.

  4. Finalize and save by clicking the Next button.

For further information on cluster mode, please refer to Registering a Connection Manually.

4. Set Up as a Sharded Cluster (Sharding)

Sharded Cluster differs only in the options for Replica Set and Connection String, and the setup is the same: Replica Set requires the option replicaSet=[replica set name], while Sharded Cluster does not require this option.

image-20240128-115556.png

Settings > Database Settings > DB Connections > Create Connection > Fill out for configuration

  1. Turn the Cluster switch on to enable cluster input, and then enter the following information:

    1. Type : This indicates the area exposed to connection information. Keep the value as “Primary” because the Primary role can change dynamically.

    2. Connection String : Enter a standard connection string.

    3. Click the Add Instance button to add each mongos as an instance.

      1. Enter the following format : mongodb://hostname (domain address or IP):[port], followed by the appropriate Instance Name.

      2. Expose is a value that determines whether to display the connection information.

    4. Username & Password : Input the credentials (username and password) for the corresponding connection database.

  2. Click the Test Connection button to ensure that the access information is valid.

  3. Finalize and save by clicking the Next button.

Related Topics

Back to DB Management

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.