Connect to a Redis cluster
You can connect to a Redis cloud database cluster by DNS address, private IP address, and public IP address.
We recommend connecting by DNS address because DNS addresses correspond to node roles and lead to the actual IP addresses of the master and replicas. IP addresses correspond to specific nodes. If the master is unavailable, one of the replicas will assume its role, the master's IP address will change, and the IP connection will stop working.
If the cluster is connected to a private subnet and you want to work with it via DNS, connect the cluster subnet to a cloud router with access to the external network. Use the following instructions Set up internet access via cloud router.
If the cluster is connected to a private network and you need to configure access to the node from the Internet, connect a public IP address.
Ports
Use ports to connect to Redis:
- 6380 — port for connection with SSL certificate;
- 6379 — port for connection without SSL certificate (available only for clusters in private subnet).
Ways of connection
- through Docker;
- from program code with SSL and without SSL.
Connection with SSL certificate is available for all methods.
View the address for connection
- In control panel go to Cloud platform → Databases.
- Open the Database Cluster page → tab Connection.
- In the block Addresses for connection check out the address.
Connect with SSL
Connecting using TLS/SSL encryption provides a secure connection between your server and the database cluster.
Bash
Python
PHP
Go
Node.js
-
Download the root certificate and place it in the folder
~/.redis/
:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crt -
Connect to the cluster:
redis-cli -h <host> \
-a <password> \
-p 6380 \
--tls \
--cacert ~/.redis/root.crtSpecify:
<host>
— DNS address of the node;<password>
— password.
-
Download the root certificate and place it in the folder
~/.redis/
:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crt -
Install the redis library:
pip install redis
-
Use the connection example:
import redis
r = redis.Redis(
host="<host>",
password="<password>",
port=6380,
db=0,
ssl=True,
ssl_ca_certs="<full_path_to_root_certificate>",
)
print(r.set("KEY", "VALUE"))
print(r.get("KEY"))Specify:
<host>
— DNS address of the node;<password>
— user password;<full_path_to_root_certificate>
— the full path to the root certificate.
-
Download the root certificate and place it in the folder
~/.redis/
:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crt -
Install the predis library via composer:
composer require predis/predis
-
Use the connection example:
<?php
require __DIR__ . '/vendor/autoload.php';
Predis\Autoloader::register();
$host = ['<host>:6380'];
$options = [
'parameters' => [
'scheme' => 'tls',
'ssl' => ['cafile' => '<full_path_to_root_certificate>', '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>
— DNS address of the node;<full_path_to_root_certificate>
— the full path to the root certificate;<password>
— user password.
-
Download the root certificate and place it in the folder
~/.redis/
:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crt -
Use the connection example:
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
var certPath = "<full_path_to_root_certificate>"
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>:6380",
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:
<full_path_to_root_certificate>
— the full path to the root certificate;<host>
— DNS address of the node;<password>
— user password.
-
Download the root certificate and place it in the folder
~/.redis/
:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crt -
Install the ioredis client:
npm install ioredis
-
Use the connection example:
const fs = require('fs');
const Redis = require('ioredis');
const config = {
host: '<host>',
port: 6380,
password: '<password>',
tls: {
rejectUnauthorized: true,
ca: fs.readFileSync('<full_path_to_root_certificate>').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>
— DNS address of the node;<password>
— user password;<full_path_to_root_certificate>
— the full path to the root certificate.
Connect without SSL
Connection without SSL is only available for clusters on a private subnet.
Bash
Python
PHP
Go
Node.js
-
Open the CLI.
-
Connect to the cluster:
redis-cli -h <host> \
-a <password> \
-p 6379Specify:
<host>
— DNS address of the node;<password>
— password.
-
Install the redis library:
pip install redis
-
Use the connection example:
import redis
r = redis.Redis(
host="<host>",
password=r"<password>",
port=6379,
db=0,
)
print(r.set("KEY", "VALUE"))
print(r.get("KEY"))Specify:
<host>
— DNS address of the node;<password>
— user password.
-
Install the predis library via composer:
composer require predis/predis
-
Use the connection example:
<?php
require __DIR__ . '/vendor/autoload.php';
Predis\Autoloader::register();
$host = ['<host>:6379'];
$options = [
'parameters' => [
'password' => "<password>"
],
'cluster' => 'predis'
];
$conn = new Predis\Client($host, $options);
$conn->set('KEY', 'VALUE');
var_dump($conn->get('KEY'));
$conn->disconnect();
?>Specify
<password>
— user password.
Use the connection example:
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
)
func main() {
var ctx = context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "<host>:6379",
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 <password>
— user password.
-
Install the ioredis client:
npm install ioredis
-
Use the connection example:
const Redis = require('ioredis');
const config = {
host: '<host>',
port: 6379,
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>
— DNS address of the node;<password>
— user password.
Connect from Docker
-
Download the root certificate and place it in the folder
~/.redis/
:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crt -
Connect to the cluster:
docker run --rm -it \
-v $(pwd)/.redis/root.crt:/root.crt \
redis \
redis-cli \
-h <host> \
-a <password> \
-p 6380 --tls \
--cacert /root.crtSpecify:
<host>
— DNS address of the node;<password>
— user password.