Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Managed Service for MongoDB
  • Getting started
    • All guides
      • Pre-configuration
      • Connecting from applications
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes

In this article:

  • Command line tools
  • Linux (Bash)
  • Windows (PowerShell)
  • Connecting from graphical IDEs
  • DataGrip
  • DBeaver
  • Before you connect from a Docker container
  1. Step-by-step guides
  2. Connection
  3. Connecting from applications

Connecting to a MongoDB cluster from applications

Written by
Yandex Cloud
Updated at May 13, 2025
  • Command line tools
    • Linux (Bash)
    • Windows (PowerShell)
  • Connecting from graphical IDEs
    • DataGrip
    • DBeaver
  • Before you connect from a Docker container

You can connect to a MongoDB cluster using command line tools, graphical IDEs, and Docker containers. To learn how to connect from your application code, see Code examples.

In the examples below, the root.crt SSL certificate is located in the following directory:

  • ~/.mongodb/ for Ubuntu.
  • $HOME\.mongodb for Windows.

If the connection to the cluster is successful and the test query is executed, the name of the database to which the connection was made will be displayed.

Command line toolsCommand line tools

To see code examples with the host FQDN filled in, open the cluster page in the management console and click Connect.

The setup method depends on whether cluster sharding is enabled:

Linux (Bash)Linux (Bash)

Before connecting, install the MongoDB Shell utility.

Connecting via SSL for MongoDB 4.2 and higher
Connecting with SSL for MongoDB 4.0
Connecting without SSL

For a non-sharded cluster:

mongosh --norc \
        --tls \
        --tlsCAFile ~/.mongodb/root.crt \
        --host '<MongoDB_host_1_FQDN>:27018,...,<MongoDB_host_N_FQDN>:27018' \
        --username <DB_username> \
        --password <DB_user_password> \
        <DB_name>

For a sharded cluster:

mongosh --norc \
        --tls \
        --tlsCAFile ~/.mongodb/root.crt \
        --host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' \
        --username <DB_username> \
        --password <DB_user_password> \
        <DB_name>

For a non-sharded cluster:

mongosh --norc \
        --ssl \
        --sslCAFile ~/.mongodb/root.crt \
        --host '<MongoDB_host_1_FQDN>:27018,...,<MongoDB_host_N_FQDN>:27018' \
        --username <DB_username> \
        --password <DB_user_password> \
        <DB_name>

For a sharded cluster:

mongosh --norc \
        --ssl \
        --sslCAFile ~/.mongodb/root.crt \
        --host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' \
        --username <DB_username> \
        --password <DB_user_password> \
        <DB_name>

For a non-sharded cluster:

mongosh --norc \
        --host '<MongoDB_host_1_FQDN>:27018,...,<MongoDB_host_N_FQDN>:27018' \
        --username <DB_username> \
        --password <DB_user_password> \
        <DB_name>

For a sharded cluster:

mongosh --norc \
        --host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' \
        --username <DB_username> \
        --password <DB_user_password> \
        <DB_name>

To learn how to get a host FQDN, see this guide.

Once connected, run the db command.

Windows (PowerShell)Windows (PowerShell)

Before connecting, install the MongoDB Shell utility.

Connecting via SSL for MongoDB 4.2 and higher
Connecting without SSL

For a non-sharded cluster:

mongosh.exe --norc `
            --host '<MongoDB_host_1_FQDN>:27018,...,<MongoDB_host_N_FQDN>:27018' `
            --tls `
            --tlsCAFile $HOME\.mongodb\root.crt `
            --username <DB_username> `
            --password <DB_user_password> `
            <DB_name>

For a sharded cluster:

mongosh.exe --norc `
            --host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' `
            --tls `
            --tlsCAFile $HOME\.mongodb\root.crt `
            --username <DB_username> `
            --password <DB_user_password> `
            <DB_name>

For a non-sharded cluster:

mongosh.exe --norc `
           --host '<FQDN_of_MongoDB_host_1>:27018,...,<FQDN_of_MongoDB_host_N>:27018' `
            --username <DB_username> `
            --password <DB_user_password> `
            <DB_name>

For a sharded cluster:

mongosh.exe --norc `
           --host '<FQDN_of_MONGOINFRA_or_MONGOS_host_1>:27017,...,<FQDN_of_MONGOINFRA_or_MONGOS_host_N>:27017' `
            --username <DB_username> `
            --password <DB_user_password> `
            <DB_name>

To learn how to get a host FQDN, see this guide.

Once connected, run the db command.

Connecting from graphical IDEsConnecting from graphical IDEs

Connections were tested in the following environment:

  • MacOS Big Sur 11.3
  • JetBrains DataGrip: 2021.1
  • DBeaver Enterprise: 21.0

You can only use graphical IDEs to connect to public cluster hosts with an SSL certificate.

To avoid connection errors, save the certificate to a local folder that does not require administrator rights to access.

DataGripDataGrip

  1. Create a data source:
    1. Select File → New → Data Source → MongoDB.
    2. On the General tab:
      1. Specify the connection settings:

        • User, Password: DB user's name and password.

        • URL: Connection string.

          For a non-sharded cluster:

          mongodb://<MongoDB_host_1_FQDN>:27018,..,<MongoDB_host_N_FQDN>:27018/<DB_name>
          

          For a sharded cluster:

          mongodb://<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017/<DB_name>
          

          To learn how to get a host FQDN, see this guide.

      2. Click Download to download the connection driver.

    3. On the SSH/SSL tab:
      1. Enable the Use SSL setting.
      2. In the CA file field, specify the path to the file with an SSL certificate for the connection.
  2. Click Test Connection to test the connection. If the connection is successful, you will see the connection status and information about the DBMS and driver.
  3. Click OK to save the data source.

DBeaverDBeaver

Connections to MongoDB clusters are only available in DBeaver business editions.

To connect to a cluster:

  1. Create a new DB connection:
    1. In the Database menu, select New connection.
    2. Select MongoDB from the DB list.
    3. Click Next.
    4. Specify the connection parameters on the Main tab:
      1. Under Address, change Type to URL and specify the connection string.

        For a non-sharded cluster:

        mongodb://<MongoDB_host_1_FQDN>:27018,..,<MongoDB_host_N_FQDN>:27018/<DB_name>
        

        For a sharded cluster:

        mongodb://<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017/<DB_name>
        

        To learn how to get a host FQDN, see this guide.

      2. In the Device list, select SCRAM-SHA-256 (type of password encryption when connecting to the DB).

      3. Under Authentication, specify the DB username and password.

    5. On the SSL tab:
      1. Enable Use SSL.
      2. In the Root certificate field, specify the path to the saved SSL certificate file.
      3. Under Settings, select Skip hostname validation.
  2. Click Test connection ... to test the connection. If the connection is successful, you will see the connection status and information about the DBMS and driver.
  3. Click Ready to save the database connection settings.

Before you connect from a Docker containerBefore you connect from a Docker container

To connect to a Managed Service for MongoDB cluster from a Docker container using SSL, add the following lines to the Dockerfile:

RUN apt-get update && \
    apt-get install wget --yes && \
    mkdir --parents ~/.mongodb && \
    wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \
         --output-document ~/.mongodb/root.crt && \
    chmod 0644 ~/.mongodb/root.crt

To connect without SSL, no additional Dockerfile settings are required.

After running the Docker container, switch to it and install mongosh. You will need it to connect to the cluster.

Was the article helpful?

Previous
Pre-configuration
Next
Overview
© 2025 Direct Cursus Technology L.L.C.