Connect to a Redis cluster
You can connect to a Redis cloud database cluster by DNS address.
If the cluster is connected to a private subnet and you want to work with it via DNS, connect cloud router-to-external-network to the cluster subnet.
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 to connect
- via Docker;
- from the with SSL and without SSL program code.
Connection with SSL certificate is available for all methods.
View the address to connect
- In Control Panel, go to Cloud Platform → Databases.
- Open the Database Cluster page → Connect tab.
- In the DNS addresses to connect block, look up the DNS address.
Connect with SSL
SSL connectivity provides data encryption between your server and the database cluster.
- Bash
- Python
- PHP
- Go
- Node.js
Download the root certificate and place it in the
~/.redis/
folder:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crtConnect to the cluster:
redis-cli -h <host> \
-a <password> \
-p 6380 \
--tls \
--cacert ~/.redis/root.crtSpecify:
<host>
— DNS address of the node;<password>
is the password.
Download the root certificate and place it in the
~/.redis/
folder:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crtInstall 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>
is the user's password;<full_path_to_root_certificate>
is the full path to the root certificate.
Download the root certificate and place it in the
~/.redis/
folder:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crtInstall 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>
is the full path to the root certificate;<password>
is the user's password.
Download the root certificate and place it in the
~/.redis/
folder:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crtUse 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>
is the full path to the root certificate;<host>
— DNS address of the node;<password>
is the user's password.
Download the root certificate and place it in the
~/.redis/
folder:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crtInstall 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>
is the user's password;<full_path_to_root_certificate>
is 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>
is the 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>
is the user's 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>
as the user's 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>` as the user's 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>
is the user's password.
Connect from Docker
Download the root certificate and place it in the
~/.redis/
folder:mkdir -p ~/.redis/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.redis/root.crt
chmod 600 ~/.redis/root.crtConnect 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>
is the user's password.