Technology Encyclopedia Home >What is the URI for connecting to MongoDB in the business program?

What is the URI for connecting to MongoDB in the business program?

The URI (Uniform Resource Identifier) for connecting to MongoDB in a business program follows a standard format, typically structured as:

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]

Explanation:

  • mongodb://: The protocol prefix for MongoDB connections.
  • [username:password@]: Optional authentication credentials. If omitted, the connection is unauthenticated (not recommended for production).
  • host1[:port1]: The hostname or IP address of the MongoDB server, with an optional port (default is 27017).
  • [,...hostN[:portN]]: For replica sets, multiple hosts can be specified.
  • /[defaultauthdb]: The default authentication database (e.g., admin).
  • [?options]: Optional query parameters for configuration (e.g., ssl=true, replicaSet=myReplicaSet).

Example:

A connection URI for a standalone MongoDB instance with authentication:

mongodb://admin:securepassword@127.0.0.1:27017/mydatabase?ssl=false

For a production environment using Tencent Cloud Database for MongoDB, the URI is provided in the console after creating the instance. It typically includes a private or public endpoint, such as:

mongodb://<username>:<password>@<mongodb-host>:<port>/<database>?authSource=admin&replicaSet=<replicaSetName>

To use Tencent Cloud Database for MongoDB, you can:

  1. Log in to the Tencent Cloud console.
  2. Navigate to Database for MongoDB.
  3. Create or select an instance.
  4. Retrieve the connection string from the instance details page.

For secure connections, enable SSL and use the provided certificate (if required). Tencent Cloud also supports VPC peering for private network access.