---
title: "Connect to a Redis cluster"
sidebar_label: "Connect to a cluster"
sidebar_position: 6
description: "How to connect to a Redis cluster with or without an SSL certificate in the console and from different programming languages"
---

import Formbricks from '@theme/MDXComponents/Formbricks'
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import {TabItemLabel} from '@selectel/docux/components'
import ConnectionAddresses from '@site/i18n/en/docusaurus-plugin-content-docs/current/_partials/managed-databases/common/connection-addresses.mdx'

# Connect to a Redis cluster

You can connect to a Redis cluster:

* [via Docker](#connect-via-docker);
* program code [with SSL](#connect-with-ssl) and [without SSL](#connect-without-SSL).

Connecting with an SSL certificate is available for all methods.

When connecting, specify the [port](#connection-ports) and [address](#connection-addresses).

## Connection ports \{#connection-ports}

Use the following ports to connect to Redis:

* 6380 — port for connecting with an SSL certificate;
* 6379 — port for connecting without an SSL certificate (only available for clusters in a private subnet).

## Connection addresses \{#connection-addresses}

<ConnectionAddresses DatabaseName="Redis" slug="redis" />

## Connect via SSL \{#connect-with-ssl}

Connecting using TLS(SSL) encryption ensures a secure connection between your server and the database cluster.

<Tabs queryString="connect-with-ssl">
  <TabItem value="bash" default>
    <TabItemLabel>
      Bash
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.redis/:` folder:

       ```bash
       mkdir -p ~/.redis/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
       chmod 600 ~/.redis/root.crt
       ```

    2. Connect to the cluster:

       ```bash
       redis-cli -h <host> \
           -a <password> \
           -p <port> \
           --tls \
           --cacert ~/.redis/root.crt
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<password>` — the password;
       * `<port>` — [the connection port](#connection-ports).
  </TabItem>

  <TabItem value="powershell">
    <TabItemLabel>
      PowerShell
    </TabItemLabel>

    1. In the [Control panel](https://my.selectel.ru/vpc/default/dbaas/), click **Download certificate** to download the root certificate and place it in the `%APPDATA%\redis\`.
    2. Connect to the cluster:

       ```bash
       redis-cli -h <host> `
       -a <password> `
       -p <port> `
       --tls `
       --cacert %APPDATA%\redis\CA.pem
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<password>` — the password;
       * `<port>` — [the connection port](#connection-ports).
  </TabItem>

  <TabItem value="python">
    <TabItemLabel>
      Python
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.redis/:` folder:

       ```bash
       mkdir -p ~/.redis/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
       chmod 600 ~/.redis/root.crt
       ```

    2. Install the redis library:

       ```bash
       pip install redis
       ```

    3. Use the connection example:

       ```python
       import redis

       r = redis.Redis(
           host="<host>",
           password="<password>",
           port=<port>,
           db=0,
           ssl=True,
           ssl_ca_certs="<path>",
       )

       print(r.set("KEY", "VALUE"))
       print(r.get("KEY"))
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<password>` — the user password;
       * `<port>` — [the connection port](#connection-ports);
       * `<path>` — the full path to the root certificate.
  </TabItem>

  <TabItem value="php">
    <TabItemLabel>
      PHP
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.redis/:` folder:

       ```bash
       mkdir -p ~/.redis/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
       chmod 600 ~/.redis/root.crt
       ```

    2. Install the `predis` library via `Composer`:

       ```bash
       composer require predis/predis
       ```

    3. Use the connection example:

       ```php
       <?php
       require __DIR__ . '/vendor/autoload.php';
       Predis\Autoloader::register();

       $host = ['<host>:<port>'];
       $options = [
           'parameters' => [
               'scheme' => 'tls',
               'ssl' => ['cafile' => '<path>', 'verify_peer' => true],
               'password' => "<password>"
           ],
           'cluster' => 'predis'
       ];

       $conn = new Predis\Client($host, $options);

       $conn->set('KEY', 'VALUE');
       var_dump($conn->get('KEY'));

       $conn->disconnect();
       ?>
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<path>` — the full path to the root certificate;
       * `<password>` — the user password.
  </TabItem>

  <TabItem value="go">
    <TabItemLabel>
      Go
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.redis/:` folder:

       ```bash
       mkdir -p ~/.redis/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
       chmod 600 ~/.redis/root.crt
       ```

    2. Use the connection example:

       ```go
       package main

       import (
           "context"
           "crypto/tls"
           "crypto/x509"
           "fmt"
           "io/ioutil"

           "github.com/go-redis/redis/v8"
       )

       var ctx = context.Background()
       var certPath = "<path>"

       func main() {
           caCert, err := ioutil.ReadFile(certPath)
           if err != nil {
               panic(err)
           }
           caCertPool := x509.NewCertPool()
           caCertPool.AppendCertsFromPEM(caCert)
           rdb := redis.NewClient(&redis.Options{
               Addr:     "<host>:<port>",
               Password: "<password>",
               DB:       0,
               TLSConfig: &tls.Config{
                   RootCAs:            caCertPool,
                   InsecureSkipVerify: true,
               },
           })

           err = rdb.Set(ctx, "key", "value", 0).Err()
           if err != nil {
               panic(err)
           }

           val, err := rdb.Get(ctx, "key").Result()
           if err != nil {
               panic(err)
           }
           fmt.Println("key", val)

       }
       ```

       Specify:

       * `<path>` — the full path to the root certificate;
       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<password>` — the user password.
  </TabItem>

  <TabItem value="nodejs">
    <TabItemLabel>
      Node.js
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.redis/:` folder:

       ```bash
       mkdir -p ~/.redis/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
       chmod 600 ~/.redis/root.crt
       ```

    2. Install the ioredis client:

       ```bash
       npm install ioredis
       ```

    3. Use the connection example:

       ```js
       const fs = require('fs');
       const Redis = require('ioredis');

       const config = {
         host: '<host>',
         port: <port>,
         password: '<password>',
         tls: {
           rejectUnauthorized: true,
           ca: fs.readFileSync('<path>').toString(),
         }
       };

       const connection = new Redis(config);

       connection.set('key', 'value', (error) => {
         if (error) throw error;
       });

       connection.get('key', (error, res) => {
         if (error) throw error;
         console.log(res);
         connection.disconnect();
       });
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<password>` — the user password;
       * `<path>` — the full path to the root certificate.
  </TabItem>
</Tabs>

## Connect without SSL \{#connect-without-SSL}

Connection without SSL is only available for clusters in a private subnet.

<Tabs queryString="connect-without-ssl">
  <TabItem value="bash" default>
    <TabItemLabel>
      Bash
    </TabItemLabel>

    1. Open the CLI.
    2. Connect to the cluster:

       ```bash
       redis-cli -h <host> \
         -a <password> \
         -p <port>
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<password>` — the password;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="powershell">
    <TabItemLabel>
      PowerShell
    </TabItemLabel>

    1. Open the CLI.
    2. Connect to the cluster:

       ```bash
       redis-cli -h <host> `
       -a <password> `
       -p <port>
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<password>` — the password;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="python">
    <TabItemLabel>
      Python
    </TabItemLabel>

    1. Install the redis library:

       ```bash
       pip install redis
       ```

    2. Use the connection example:

       ```python
       import redis

       r = redis.Redis(
           host="<host>",
           password=r"<password>",
           port=<port>,
           db=0,

       )

       print(r.set("KEY", "VALUE"))
       print(r.get("KEY"))
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<password>` — the user password;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="php">
    <TabItemLabel>
      PHP
    </TabItemLabel>

    1. Install the `predis` library via `Composer`:

       ```bash
       composer require predis/predis
       ```

    2. Use the connection example:

       ```php
       <?php
           require __DIR__ . '/vendor/autoload.php';
           Predis\Autoloader::register();

           $host = ['<host>:<port>'];
           $options = [
             'parameters' => [
               'password' => "<password>"
             ],
             'cluster' => 'predis'
           ];

           $conn = new Predis\Client($host, $options);

           $conn->set('KEY', 'VALUE');
           var_dump($conn->get('KEY'));

           $conn->disconnect();
       ?>
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<password>` — the user password.
  </TabItem>

  <TabItem value="go">
    <TabItemLabel>
      Go
    </TabItemLabel>

    Use the connection example:

    ```go
    package main

    import (
        "context"
        "fmt"

        "github.com/go-redis/redis/v8"
    )

    func main() {
        var ctx = context.Background()

        rdb := redis.NewClient(&redis.Options{
            Addr:     "<host>:<port>",
            Password: "<password>",
            DB:       0,
        })

        err := rdb.Set(ctx, "key", "value", 0).Err()
        if err != nil {
            panic(err)
        }

        val, err := rdb.Get(ctx, "key").Result()
        if err != nil {
            panic(err)
        }
        fmt.Println("key", val)

    }
    ```

    Specify:

    * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
    * `<port>` — [connection port](#connection-ports);
    * `<password>` — the user password.
  </TabItem>

  <TabItem value="nodejs">
    <TabItemLabel>
      Node.js
    </TabItemLabel>

    1. Install the ioredis client:

       ```bash
       npm install ioredis
       ```

    2. Use the connection example:

       ```js
       const Redis = require('ioredis');

       const config = {
         host: '<host>',
         port: <port>,
         password: '<password>',
       };

       const connection = new Redis(config);

       connection.set('key', 'value', (error) => {
         if (error) throw error;
       });

       connection.get('key', (error, res) => {
         if (error) throw error;
         console.log(res);
         connection.disconnect();
       });
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<password>` — the user password.
  </TabItem>
</Tabs>

## Connect via Docker \{#connect-via-docker}

1. Download the root certificate and place it in the `~/.redis/:` folder:

   ```bash
   mkdir -p ~/.redis/
   wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
   chmod 600 ~/.redis/root.crt
   ```

2. Connect to the cluster:

   ```bash
   docker run --rm -it \
     -v $(pwd)/.redis/root.crt:/root.crt \
     redis \
     redis-cli \
     -h <host> \
     -a <password> \
     -p <port> --tls \
     --cacert /root.crt
   ```

   Specify:

   * `<host>` — the DNS address or [public IP address](/managed-databases/redis/public-ip.mdx) (Floating IP) of the node;
   * `<password>` — the user password;
   * `<port>` — [connection port](#connection-ports).

<Formbricks />
