[Yandex Cloud documentation](../../../index.md) > [Yandex Managed Service for Valkey™](../../index.md) > [Step-by-step guides](../index.md) > [Connection](index.md) > Connecting from applications

# Connecting to a Valkey™ cluster from applications

You can connect to a Valkey™ cluster using [command line tools](#command-line-tools), [graphical IDEs](#connection-ide), or [Docker containers](#connection-docker). To learn how to connect from your application code, see [Code examples](code-examples.md).

## 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.

### redis-cli {#redis-cli}

For Valkey™ clusters, there is support for encrypted connections on port `6380` and unencrypted connections on port `6379`.

{% note warning %}

When using SSL, you can only connect to clusters with **TLS support** enabled.

{% endnote %}

To always connect to the master in a non-sharded cluster, use a [special FQDN](fqdn.md#special-fqdn) that always points to the master, or track the roles of all cluster hosts yourself.

{% list tabs group=connection %}

- Connecting without SSL {#without-ssl}

    1. **Before connecting, install the required dependencies:**
       
       ```bash
       sudo apt update && sudo apt install -y redis-tools
       ```

    1. **Connect directly to the master host:**

       * In a sharded cluster:

          ```bash
          redis-cli \
              -h c-<cluster_ID>.rw.mdb.yandexcloud.net \
              -a <password>
          ```
       * In a non-sharded cluster:

          ```bash
           redis-cli \
               -c \
               -h <FQDN_of_master_host_in_target_shard> \
               -a <password>
           ```

- Connecting with SSL {#with-ssl}

    1. **Before connecting, install the dependencies:**
       
       Build `redis-tools` with TLS support using one of the two methods:
       
       * **From a repository**
       
           1. Add a repository:
       
               ```bash
               sudo apt-add-repository ppa:redislabs/redis
               ```
       
               The repository’s packages come pre-built with `BUILD_TLS=yes`.
       
           1. Install the tool:
       
               ```bash
               sudo apt update && sudo apt install -y redis-tools
               ```
       
       * **Manually**
       
           Go to the directory you want to download the distribution to. Download the stable tool version, then build and install it:
       
           ```bash
           wget https://download.redis.io/redis-stable.tar.gz && \
           tar -xzvf redis-stable.tar.gz && \
           cd redis-stable && \
           make BUILD_TLS=yes && \
           sudo make install && \
           sudo cp ./src/redis-cli /usr/bin/
           ```

    1. **Connect directly to the master host:**

       * In a sharded cluster:

         ```bash
         redis-cli \
             -h c-<cluster_ID>.rw.mdb.yandexcloud.net \
             -a <password> \
             -p 6380 \
             --tls \
             --cacert ~/.redis/YandexInternalRootCA.crt
         ```

       * In a non-sharded cluster:

          ```bash
          redis-cli \
              -c \
              -h <FQDN_of_master_host_in_target_shard> \
              -a <password> \
              -p 6380 \
              --tls \
              --cacert ~/.redis/YandexInternalRootCA.crt \
          ```

{% endlist %}

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

Once connected to the cluster, run these commands:

```text
SET foo bar
GET foo
```

If your cluster connection and test command are successful, you will see the `bar` string in the output.

### Valkey™ Sentinel {#redis-sentinel}

Valkey™ Sentinel is a Valkey™ host management system that enables monitoring, notification, [automatic failover](../failover.md), and providing clients with the current host addresses.

For non-sharded clusters, there is support for unencrypted connections on port `26379` with any Valkey™ version.

If your client application does not support connecting via Sentinel, connect directly to the master. Otherwise, use Sentinel for more reliable cluster host management:

{% list tabs group=connection %}

- Connecting without SSL {#without-ssl}

    1. **Before connecting, install the required dependencies:**
       
       ```bash
       sudo apt update && sudo apt install -y redis-tools
       ```

    1. **Get the master host address by using Sentinel and any Valkey™ host:**

        ```bash
        redis-cli \
            -h <FQDN_of_any_Valkey™_host> \
            -p 26379 \
            sentinel \
            get-master-addr-by-name <Valkey™_cluster_name> | head -n 1
        ```

    1. **Connect to the host with this address:**

        ```bash
        redis-cli \
            -h <Valkey™_master_host_address> \
            -a <Valkey™_password>
        ``` 

{% endlist %}

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

Once connected to the cluster, run these commands:

```text
SET foo bar
GET foo
```

If your cluster connection and test command are successful, you will see the `bar` string in the output.

For more on Sentinel, see [this article](../../concepts/replication.md) and [this Valkey™ guide](https://valkey.io/topics/sentinel/).

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

**The connection was tested in the following environment:**
* MacOS Big Sur 11.3.
* DBeaver Enterprise: `21.0`.

You can only use graphical IDEs to connect to cluster hosts through an SSL tunnel using the [VM you created](index.md). Before connecting, [prepare a certificate](index.md#get-ssl-cert).

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.

{% list tabs group=ide %}

- DBeaver {#dbeaver}

    Connections to Valkey™ 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 **Valkey™** from the DB list.
        1. Click **Next**.
        1. Specify the connection settings on the **Main** tab:
            * **Host**: [FQDN of the master host](fqdn.md) or a [special FQDN](fqdn.md#special-fqdns) always pointing to the current master host.
              When connecting to a sharded cluster, specify comma-separated [FQDNs of master hosts](fqdn.md) in each shard.
            * **Port**: `6379` for a regular cluster, or `6380` for a cluster with SSL encryption enabled.
            * Under **Authentication**, enter the cluster password.
        1. On the **SSH** tab:
            1. Enable **Use SSL tunnel**.
            1. Configure the SSH tunnel:
                * **Host/IP**: Public IP address of the [VM to connect to](index.md).
                * **Username**: Username for connecting to the VM.
                * **Authentication method**: `Public key`.
                * **Secret key**: Path to the private key file for the VM connection.
                * **Passphrase**: Private key password.
        1. On the **SSL** tab:
            1. Enable **Use SSL** and **Skip hostname validation** settings.
            1. Under **Parameters**:
                1. Select **Method**: **Set of certificates**.
                1. In the **Root certificate** field, specify the path to the saved [SSL certificate](index.md#get-ssl-cert) file.
    1. Click **Test Connection ...**. If the connection is successful, you will see the connection status, DBMS information, and driver details.
    1. Click **Done** to save the database connection settings.

{% endlist %}

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

To connect to a Yandex Managed Service for Valkey™ cluster from a Docker container, add the following lines to the Dockerfile:

{% list tabs group=connection %}


- Connecting without SSL {#without-ssl}

    ```bash
    # Build redis-tools with TLS support manually.
    RUN apt-get update && \
        apt-get install make gcc libssl-dev --yes && \
        wget https://download.redis.io/redis-stable.tar.gz && \
        tar -xzvf redis-stable.tar.gz && \
        cd redis-stable && \
        make BUILD_TLS=yes MALLOC=libc && \
        make install && \
        cp ./src/redis-cli /usr/bin/
    ```


- Connecting with SSL {#with-ssl}

    ```bash
    # Build redis-tools with TLS support manually.
    RUN apt-get update && \
        apt-get install wget make gcc libssl-dev --yes && \
        wget https://download.redis.io/redis-stable.tar.gz && \
        tar -xzvf redis-stable.tar.gz && \
        cd redis-stable && \
        make BUILD_TLS=yes MALLOC=libc && \
        make install && \
        cp ./src/redis-cli /usr/bin/ && \
        # Get an SSL certificate.
        mkdir --parents ~/.redis && \
        wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \
             --output-document ~/.redis/YandexInternalRootCA.crt && \
        chmod 0655 ~/.redis/YandexInternalRootCA.crt
    ```

{% endlist %}