[Yandex Cloud documentation](../../../index.md) > [Yandex StoreDoc](../../index.md) > [Step-by-step guides](../index.md) > [Connection](index.md) > Connecting from applications

# Connecting to a Yandex StoreDoc cluster from applications

You can connect to a Yandex StoreDoc cluster using [command line tools](#command-line-tools), [graphical IDEs](#connection-ide), or [Docker containers](#connection-docker). For details on connecting from your application code, see [Code examples](code-examples.md).

In the following examples, we assume that the `root.crt` [SSL certificate](index.md#get-ssl-cert) is located in one of these directories:

* `~/.mongodb/` for Ubuntu
* `$HOME\.mongodb` for Windows

After you successfully connect to the cluster and run a test query, you will see the name of the database you connected to.

## Command line tools {#command-line-tools}

To see code examples with the host FQDN filled in, open the cluster page in the [management console](https://console.yandex.cloud) and click **Connect**.

The setup method depends on whether [sharding](../../concepts/sharding.md) is enabled in the cluster:


### Linux (Bash) {#bash}

Before connecting, install [MongoDB Shell](index.md#install-mongosh).

{% list tabs group=connection %}

- Connecting with SSL {#with-ssl}

    For a non-sharded cluster:

    ```bash
    mongosh --norc \
            --tls \
            --tlsCAFile ~/.mongodb/root.crt \
            --host '<Yandex_StoreDoc_host_1_FQDN>:27018,...,<Yandex_StoreDoc_host_N_FQDN>:27018' \
            --username <DB_user_name> \
            --password <DB_user_password> \
            <DB_name>
    ```

    For a sharded cluster:

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

    To learn how to get a host FQDN, see [this guide](index.md#get-fqdn).

    Once connected, run the `db` command.

- Connecting without SSL {#without-ssl}

    For a non-sharded cluster:

    ```bash
    mongosh --norc \
            --host '<Yandex_StoreDoc_host_1_FQDN>:27018,...,<Yandex_StoreDoc_host_N_FQDN>:27018' \
            --username <DB_user_name> \
            --password <DB_user_password> \
            <DB_name>
    ```

    For a sharded cluster:

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

    To learn how to get a host FQDN, see [this guide](index.md#get-fqdn).

    Once connected, run the `db` command.

- SRV connection {#srv}

    ```bash
    mongosh "mongodb+srv://<DB_user_name>:<DB_user_password>\
    @<cluster_ID>.mdb.yandexcloud.net/<DB_name>"
    ```

  You can get the cluster ID with the [list of clusters in the folder](../cluster-list.md#list-clusters).

  Once connected, run the `db` command.

  You can add connection parameters to the command, if required, for example:
  * `tls`: Enables TLS encryption for the connection (`true` or `false`). Encryption is enabled by default.
  * `tlsCAFile`: Path to the `.pem` file with root certificates from the CA.
  * `readPreference`: Hosts to read from:
    * `primary`: Only the primary replica (master).
    * `primaryPreferred`: Master or secondary replicas if the master is unavailable.
    * `secondary`: Only secondary replicas.
    * `secondaryPreferred`: Secondary replicas or the master if there are no secondary replicas.
    * `nearest`: Least lagging host.
  * `authSource`: Name of the database storing the user credentials. By default, it is the same as `<DB_name>`.
  * `appName`: App name to display in logs.
  
  For more information about available connection parameters, see [this official guide](https://mongodb.prakticum-team.ru/docs/manual/reference/connection-string-options/).
  
  The parameter string begins with the `?` character, and `&` is the separator.

  Example of a command with connection properties:

    ```bash
    mongosh "mongodb+srv://user********:qwe********@c9qng7jhsgtg********.mdb.yandexcloud.net\
    /db********?authSource=admin&appName=debug"
    ```

{% endlist %}

### Windows (PowerShell) {#powershell}

Before connecting, install [MongoDB Shell](index.md#install-mongosh).

{% list tabs group=connection %}

- Connecting with SSL {#with-ssl}

    For a non-sharded cluster:

    ```powershell
    mongosh.exe --norc `
                --host '<Yandex_StoreDoc_host_1_FQDN>:27018,...,<Yandex_StoreDoc_host_N_FQDN>:27018' `
                --tls `
                --tlsCAFile $HOME\.mongodb\root.crt `
                --username <DB_user_name> `
                --password <DB_user_password> `
                <DB_name>
    ```

    For a sharded cluster:

    ```powershell
    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_user_name> `
                --password <DB_user_password> `
                <DB_name>
    ```

    To learn how to get a host FQDN, see [this guide](index.md#get-fqdn).

    Once connected, run the `db` command.

- Connecting without SSL {#without-ssl}

    For a non-sharded cluster:

    ```powershell
    mongosh.exe --norc `
                --host '<Yandex_StoreDoc_host_1_FQDN>:27018,...,<Yandex_StoreDoc_host_N_FQDN>:27018' `
                --username <DB_user_name> `
                --password <DB_user_password> `
                <DB_name>
    ```

    For a sharded cluster:

    ```powershell
    mongosh.exe --norc `
                --host '<MONGOINFRA_or_MONGOS_host_1_FQDN>:27017,...,<MONGOINFRA_or_MONGOS_host_N_FQDN>:27017' `
                --username <DB_user_name> `
                --password <DB_user_password> `
                <DB_name>
    ```

    To learn how to get a host FQDN, see [this guide](index.md#get-fqdn).

    Once connected, run the `db` command.

- SRV connection {#srv}

    ```powershell
    mongosh.exe "mongodb+srv://<DB_user_name>:<DB_user_password>`
    @<cluster_ID>.mdb.yandexcloud.net/<DB_name>"
    ```

  You can get the cluster ID with the [list of clusters in the folder](../cluster-list.md#list-clusters).

  Once connected, run the `db` command.

  You can add connection parameters to the command, if required, for example:
  * `tls`: Enables TLS encryption for the connection (`true` or `false`). Encryption is enabled by default.
  * `tlsCAFile`: Path to the `.pem` file with root certificates from the CA.
  * `readPreference`: Hosts to read from:
    * `primary`: Only the primary replica (master).
    * `primaryPreferred`: Master or secondary replicas if the master is unavailable.
    * `secondary`: Only secondary replicas.
    * `secondaryPreferred`: Secondary replicas or the master if there are no secondary replicas.
    * `nearest`: Least lagging host.
  * `authSource`: Name of the database storing the user credentials. By default, it is the same as `<DB_name>`.
  * `appName`: App name to display in logs.
  
  For more information about available connection parameters, see [this official guide](https://mongodb.prakticum-team.ru/docs/manual/reference/connection-string-options/).
  
  The parameter string begins with the `?` character, and `&` is the separator.

  Example of a command with connection properties:

    ```powershell
    mongosh.exe "mongodb+srv://user********:qwe********@c9qng7jhsgtg********.mdb.yandexcloud.net`
    /db********?authSource=admin&appName=debug"
    ```

{% endlist %}

## Connecting from graphical IDEs {#connection-ide}

Connections were tested in the following environment:

* MacOS Big Sur 11.3
* JetBrains DataGrip: `2021.1`
* DBeaver Enterprise: `21.0`


From graphical IDEs, you can only connect to public cluster hosts using an SSL certificate.


To avoid connection errors, [save the certificate](https://storage.yandexcloud.net/cloud-certs/RootCA.pem) to a local folder that does not require administrator rights to access.

### DataGrip {#datagrip}

1. Create a data source:
   1. Select **File** → **New** → **Data Source** → **MongoDB**.
   1. On the **General** tab:
      1. Configure the connection as follows:
         * **User**, **Password**: Database user name and password.
         * **URL**: Connection string.

              For a non-sharded cluster:

              ```http
              mongodb://<Yandex_StoreDoc_host_1_FQDN>:27018,..,<Yandex_StoreDoc_host_N_FQDN>:27018/<DB_name>
              ```

              For a [sharded](../../concepts/sharding.md) cluster:

              ```http
              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](index.md#get-fqdn).

      1. Click **Download** to get the database driver.
   1. On the **SSH/SSL** tab:
      1. Enable **Use SSL**.
      1. In the **CA file** field, specify the path to the [SSL certificate for your connection](index.md#get-ssl-cert).
1. Click **Test Connection**. If the connection is successful, you will see the connection status, DBMS information, and driver details.
1. Click **OK** to save the data source.

### DBeaver {#dbeaver}

Connections to Yandex StoreDoc clusters are only available in [DBeaver commercial editions](https://dbeaver.com/buy/).

To connect to your cluster:

1. Create a new database connection:
   1. In the **Database** menu, select **New connection**.
   1. Select **MongoDB** from the database list.
   1. Click **Next**.
   1. On the **Main** tab, specify the connection settings:
      1. Under **Address**, change **Type** to `URL` and specify the connection string.

           For a non-sharded cluster:

           ```http
           mongodb://<Yandex_StoreDoc_host_1_FQDN>:27018,..,<Yandex_StoreDoc_host_N_FQDN>:27018/<DB_name>
           ```

           For a [sharded](../../concepts/sharding.md) cluster:

           ```http
           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](index.md#get-fqdn).

      1. In the **Device** list, select the `SCRAM-SHA-256` password encryption type for the connection.
      1. Under **Authentication**, specify the database user name and password.
   1. On the **SSL** tab:
      1. Enable **Use SSL**.
      1. In the **Root certificate** field, specify the path to your saved [SSL certificate](index.md#get-ssl-cert) file.
      1. Under **Settings**, check **Skip hostname validation**.
1. Click **Test connection ...**. If the connection is successful, you will see the connection status, DBMS information, and driver details.
1. Click **Ready** to save the database connection settings.

## Before you connect from a Docker container {#connection-docker}

To connect to a Yandex StoreDoc cluster from a Docker container over SSL, add the following lines to your Dockerfile:

```bash
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 configuration is required.

After starting the Docker container, access it and install `mongosh`, which you will need to connect to the cluster.