Remote Access to MongoDB

Remote Access to MongoDB

MongoDB can be accessed remotely from a local computer or another server without logging in to the platform dashboard. First make the MongoDB container externally accessible through a Public IP or an endpoint, and then connect with mongosh or another MongoDB client.

Set Up MongoDB Environment

For a remote connection, the MongoDB container must be externally accessible. The source documentation provides two methods: attach a Public IP or configure an endpoint.

Option 1: Public IP

Attach a Public IP to the MongoDB container through the topology wizard. The source recommends this option for production environments. A Public IP can be added during environment creation or later through Change Environment Topology.

After the Public IP is attached, it is displayed in the dashboard and can be copied for use as the MongoDB host address.

Option 2: Endpoint

An endpoint can be created from Settings → Endpoints. The source recommends this approach for development and testing environments.

Select the predefined MongoDB endpoint template to expose private port 27017, which is the default MongoDB connection port. After the endpoint is created, use its Access URL and Public Port for the remote connection.

Result: Either a Public IP or an endpoint makes the MongoDB container remotely accessible.

Remote Connection to MongoDB

The source demonstrates remote access with the mongosh command-line tool from an SSH terminal on a local computer or another server.

mongosh --host {host} --port {port} -u {username} -p {password} --authenticationDatabase {authDatabase} {database}
Alternative clients: You can use another MongoDB client instead of mongosh. The client-specific steps may differ, but the same host, port, username, and password are used.

Connection Parameters

{host}MongoDB address. Use the container’s Public IP or the endpoint Access URL.
{port}Target port. Use default port 27017 with a Public IP or the endpoint’s Public Port when connecting through an endpoint.
{username}MongoDB database username. Default access credentials are sent by email after environment creation.
{password}Password for the database user. You can omit the password argument and enter the password interactively.
{authDatabase}Authentication database for the specified user. It is needed when the user is not defined in the database being accessed.
{database}Database to connect to. The documented default is test.

Connect Using a Public IP

For a Public IP connection, the source notes that it is sufficient to provide the Public IP and username when the default port and database are suitable. The password can then be requested interactively.

Connect Using an Endpoint

For an endpoint connection, specify the endpoint Access URL as the host and its Public Port as the port. A password may also be provided directly in the command when required, for example for automation.

Work with the Remote Database

After the connection is established, MongoDB can be managed directly from the terminal. Type help to display available commands.

Expected Result

The MongoDB container is externally accessible through either a Public IP or endpoint, and a remote computer or server can connect to it with the correct host, port, username, password, authentication database, and target database.

Important Notes

  • Public IP is the source-recommended option for production environments.
  • Endpoints are recommended by the source for development and testing.
  • The default MongoDB private connection port is 27017.
  • For a Public IP connection, use port 27017 unless your configuration differs.
  • For an endpoint connection, use the endpoint Access URL and Public Port.
  • Default MongoDB access credentials are sent by email after environment creation.
  • The password can be entered interactively instead of being included directly in the mongosh command.
  • The default target database documented by the source is test.