Knowledgebase

How to Securely Connect to Redis with TLS/SSL in Go, NodeJS, PHP, Python, and redis-cli Print

  • 0

Introduction

RCS's Managed Redis Database requires TLS/SSL connections in your source code or CLI. This article has code samples demonstrating how to connect securely with TLS/SSL with redis-cli and popular programming languages.

How to Find your TLS/SSL Connection URL

To locate your Redis connection URL, navigate to the Overview section of your managed Redis database, then click Copy Redis URL, which places it on your clipboard.

Connection String

  • The URL is assembled from the username, password, host, and port values.

  • All the code in this article uses this example URL, which you should replace with your real one:

    rediss://USERNAME:YOUR_PASSWORD@YOUR_HOST:PORT_NUMBER
    

Important: Note the double ss in the protocol, rediss://, which shows that the connection uses TLS/SSL encryption.

Connect with Go

First, get the go-redis/redis library.

$ go get github.com/go-redis/redis/v8

Next, open a file named main.go and paste the following. Replace the example Redis URL with yours.

package main



import (

    "context"

    "fmt"



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

)



var ctx = context.Background()



func main() {

    redisURL := "rediss://USERNAME:YOUR_PASSWORD@YOUR_HOST:PORT_NUMBER"



    addr, err := redis.ParseURL(redisURL)

    if err != nil {

        panic(err)

    }



    rdb := redis.NewClient(addr)



    err = rdb.Set(ctx, "key", "Hello RCS!", 0).Err()

    if err != nil {

        panic(err)

    }



    val, err := rdb.Get(ctx, "key").Result()

    if err != nil {

        panic(err)

    }

    fmt.Println("Value is:", val)

}

This creates a key named key, with the value Hello RCS!. Then, it retrieves the key and prints its value.

To run this example:

$ go run main.go

Connect with NodeJS

First, install the ioredis library.

$ npm install --save ioredis

Next, open a file named index.php and paste the following. Replace the example Redis URL with yours.

const Redis = require("ioredis");

const redisUrl = "rediss://USERNAME:YOUR_PASSWORD@YOUR_HOST:PORT_NUMBER"

const redis = new Redis(redisUrl);



redis.set("key", "Hello RCS!");



redis.get("key").then(function (result) {

    console.log(`Value is: ${result}`);

    redis.disconnect();

});

This creates a key named key, with the value Hello RCS!. Then, it retrieves the key and prints its value.

To run this example:

$ node index.js

Connect with PHP

First, install the predis library.

$ composer require predis/predis

Next, open a file named index.php and paste the following. Replace the example Redis URL with yours.

<?php



require 'vendor/autoload.php';

Predis\Autoloader::register();



$redis_url = 'rediss://USERNAME:YOUR_PASSWORD@YOUR_HOST:PORT_NUMBER';



$client = new Predis\Client($redis_url);



$client->set('key', 'Hello RCS!');

$value = $client->get('key');



echo "Value is: {$value}";

This creates a key named key, with the value Hello RCS!. Then, it retrieves the key and prints its value.

To run this example:

$ php index.php

Connect with Python

First, install the redis-py library.

$ pip install redis

Next, open a file named main.py and paste the following. Replace the example Redis URL with yours.

import redis



def main():

    redis_url = 'rediss://USERNAME:YOUR_PASSWORD@YOUR_HOST:PORT_NUMBER'

    redis_client = redis.from_url(redis_url)



    redis_client.set('key', 'Hello RCS!')

    key = redis_client.get('key').decode('utf-8')



    print('Value is:', key)



if __name__ == '__main__':

    main()

This creates a key named key, with the value Hello RCS!. Then, it retrieves the key and prints its value.

To run this example:

$ python main.py

Connect with redis-cli

First, you'll need to have redis-cli installed.

Next, execute the following from a terminal window. Replace the example Redis URL with yours.

$ redis-cli -u rediss://USERNAME:YOUR_PASSWORD@YOUR_HOST:PORT_NUMBER

To check the connection, run the INFO command, which returns all the Redis parameters.

INFO

More Information

These are only a few of the launguages and clients available for Redis that support TLS/SSL. You'll find even more in the official documentation.

Want to contribute?

You could earn up to $600 by adding new articles.


Was this answer helpful?
Back

Powered by WHMCompleteSolution