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:
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
From the Database Settings menu, navigate to the DB Connections menu.
Click the
Create Connection
button located in the top right corner.In the Select data source step, select MongoDB.
3. Set Up as a Replica Set
Turn the
Cluster
switch on to enable cluster inputEnter the following information:
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.
Connection String : Enter a standard connection string.
For replica set, append
/?replicaSet=[replica set name]
at the end, as the replica set name is required.
Click the
Add Instance
button to include each member host as an instance.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.Expose is a value that determines whether to display the connection information.
Authentication DB : Specify the path to the Authentication DB.
Username & Password : Input the credentials (username and password) for the corresponding connection database.
Click the
Test Connection
button to ensure that the access information is valid.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.
Turn the
Cluster
switch on to enable cluster input, and then enter the following information:Type : This indicates the area exposed to connection information. Keep the value as “Primary” because the Primary role can change dynamically.
Connection String : Enter a standard connection string.
Click the
Add Instance
button to add each mongos as an instance.Enter the following format :
mongodb://hostname (domain address or IP):[port]
, followed by the appropriate Instance Name.Expose is a value that determines whether to display the connection information.
Username & Password : Input the credentials (username and password) for the corresponding connection database.
Click the
Test Connection
button to ensure that the access information is valid.Finalize and save by clicking the
Next
button.