One place for hosting & domains

      Connect

      How To Connect to a Redis Database


      Introduction

      Redis is an open-source, in-memory key-value data store. Whether you’ve installed Redis locally or you’re working with a remote instance, you need to connect to it in order to perform most operations. In this tutorial we will go over how to connect to Redis from the command line, how to authenticate and test your connection, as well as how to close a Redis connection.

      How To Use This Guide
      This guide is written as a cheat sheet with self-contained examples. We encourage you to jump to any section that is relevant to the task you’re trying to complete.

      The commands and outputs shown in this guide were tested on an Ubuntu 18.04 server running Redis version 4.0.9. To obtain a similar setup, you can follow Step 1 of our guide on How To Install and Secure Redis on Ubuntu 18.04. We will demonstrate how these commands behave by running them with redis-cli, the Redis command line interface. Note that if you’re using a different Redis interface — Redli, for example — the exact outputs of certain commands may differ.

      Alternatively, you could provision a managed Redis database instance to test these commands, but note that depending on the level of control allowed by your database provider, some commands in this guide may not work as described. To provision a DigitalOcean Managed Database, follow our Managed Databases product documentation. Then, you must either install Redli or set up a TLS tunnel in order to connect to the Managed Database over TLS.

      Connecting to Redis

      If you have redis-server installed locally, you can connect to the Redis instance with the redis-cli command:

      This will take you into redis-cli’s interactive mode which presents you with a read-eval-print loop (REPL) where you can run Redis’s built-in commands and receive replies.

      In interactive mode, your command line prompt will change to reflect your connection. In this example and others throughout this guide, the prompt indicates a connection to a Redis instance hosted locally (127.0.0.1) and accessed over Redis’s default port (6379):

      The alternative to running Redis commands in interactive mode is to run them as arguments to the redis-cli command, like so:

      If you want to connect to a remote Redis datastore, you can specify its host and port numbers with the -h and -p flags, respectively. Also, if you’ve configured your Redis database to require a password, you can include the -a flag followed by your password in order to authenticate:

      • redis-cli -h host -p port_number -a password

      If you’ve set a Redis password, clients will be able to connect to Redis even if they don’t include the -a flag in their redis-cli command. However, they won’t be able to add, change, or query data until they authenticate. To authenticate after connecting, use the auth command followed by the password:

      If the password passed to auth is valid, the command will return OK. Otherwise, it will return an error.

      If you’re working with a managed Redis database, your cloud provider may give you a URI that begins with redis:// or rediss:// which you can use to access your datastore. If the connection string begins with redis://, you can include it as an argument to redis-cli to connect.

      However, if you have a connection string that begins with rediss://, that means your managed database requires connections over TLS/SSL. redis-cli does not support TLS connections, so you’ll need to use a different tool that supports the rediss protocol in order to connect with the URI. For DigitalOcean Managed Databases, which require connections to be made over TLS, we recommend using Redli to access the Redis instance.

      Use the following syntax to connect to a database with Redli. Note that this example includes the --tls option, which specifies that the connection should be made over TLS, and the -u flag, which declares that the following argument will be a connection URI:

      • redli --tls -u rediss://connection_URI

      I you’ve attempted to connect to an unavailable instance, redis-cli will go into disconnected mode. The prompt will reflect this:

      Redis will attempt to reestablish the connection every time you run a command when it’s in a disconnected state.

      Testing Connections

      The ping command is useful for testing whether the connection to a database is alive. Note that this is a Redis-specific command and is different from the ping networking utility. However, the two share a similar function in that they’re both used to check a connection between two machines.

      If the connection is up and no arguments are included, the ping command will return PONG:

      Output

      PONG

      If you provide an argument to the ping command, it will return that argument instead of PONG if the connection is successful:

      Output

      "hello Redis!"

      If you run ping or any other command in disconnected mode, you will see an output like this:

      Output

      Could not connect to Redis at host:port: Connection refused

      Note that ping is also used by Redis internally to measure latency.

      Disconnecting from Redis

      To disconnect from a Redis instance, use the quit command:

      Running exit will also exit the connection:

      Both quit and exit will close the connection, but only as soon as all pending replies have been written to clients.

      Conclusion

      This guide details a number of commands used to establish, test, and close connections to a Redis server. If there are other related commands, arguments, or procedures you’d like to see in this guide, please ask or make suggestions in the comments below.

      For more information on Redis commands, see our tutorial series on How to Manage a Redis Database.



      Source link

      How To Connect to a Managed Redis Instance over TLS with Stunnel and redis-cli


      Introduction

      A managed Redis instance can provide benefits like high availability and automated updates. However, any time you make a connection to a remote database server, you run the risk of malicious actors sniffing the sensitive information you send to it.

      redis-cli, the Redis command line interface, doesn’t natively support connections over TLS, a cryptographic protocol that allows for secure communications over a network. This means that without further configuration, redis-cli is not a secure way to connect to a remote Redis server. One way to establish a secure connection to a managed Redis instance is to create a tunnel that uses the TLS protocol.

      Stunnel is an open-source proxy used to create secure tunnels, allowing you to communicate with other machines over TLS. In this guide, we will walk through installing and configuring stunnel so you can connect to a managed Redis instance over TLS with redis-cli.

      Prerequisites

      To complete this guide, you will need:

      • Access to an Ubuntu 18.04 server. This server should have a non-root user with administrative privileges and a firewall configured with ufw. To set this up, follow our initial server setup guide for Ubuntu 18.04.
      • A managed Redis database instance. The steps outlined in this tutorial were tested on a DigitalOcean Managed Redis Database, though they should generally work for managed databases from any cloud provider. To provision a DigitalOcean Managed Redis Database, follow our Managed Redis product documentation.

      Step 1 — Installing Stunnel and redis-cli

      When you install a Redis server, it usually comes packaged with redis-cli. However, you can install redis-cli without the Redis server by installing the redis-tools package from the default Ubuntu repositories. You can also install stunnel from the default Ubuntu repositories by downloading the stunnel4 package.

      First, update your server’s package index if you’ve not done so recently:

      Then install the redis-tools and stunnel4 packages with APT:

      • sudo apt install redis-tools stunnel4

      When prompted, press ENTER to confirm that you want to install the packages.

      You can check whether stunnel was installed correctly and its systemd service is working by running the following command:

      • sudo systemctl status stunnel4

      Output

      ● stunnel4.service - LSB: Start or stop stunnel 4.x (TLS tunnel for network daemons) Loaded: loaded (/etc/init.d/stunnel4; generated) Active: active (exited) since Thu 2019-09-12 14:34:05 UTC; 8s ago Docs: man:systemd-sysv-generator(8) Tasks: 0 (limit: 2362) CGroup: /system.slice/stunnel4.service Sep 12 14:34:05 stunnel systemd[1]: Starting LSB: Start or stop stunnel 4.x (TLS tunnel for network daemons)... Sep 12 14:34:05 stunnel stunnel4[2034]: TLS tunnels disabled, see /etc/default/stunnel4 Sep 12 14:34:05 stunnel systemd[1]: Started LSB: Start or stop stunnel 4.x (TLS tunnel for network daemons).

      Here, you can see that the stunnel service is active, though the process immediately exited. This tells us that stunnel is running, but it isn’t able to actually do anything since we haven’t yet configured it.

      Step 2 — Configuring Stunnel

      Modern Linux systems rely on systemd for initializing and managing services and daemons. However, stunnel uses a SysV-style init script, which is based on the older UNIX System V init system, for startup. You’ll need to modify the /etc/default/stunnel4 file to enable this init script.

      Open this file with your preferred text editor. Here, we’ll use nano:

      • sudo nano /etc/default/stunnel4

      Find the ENABLED option near the top of the file. It will be set to 0 by default, but change this to 1 to enable stunnel to start at boot:

      /etc/default/stunnel4

      # /etc/default/stunnel
      # Julien LEMOINE <speedblue@debian.org>
      # September 2003
      
      # Change to one to enable stunnel automatic startup
      ENABLED=1
      . . .
      

      Save and close the file. If you used nano to edit the file, do so by pressing CTRL+X, Y, then ENTER.

      Next, you will need to create a configuration file for stunnel which will tell the program where it needs to route traffic.

      Open a new file called stunnel.conf under the /etc/stunnel directory:

      • sudo nano /etc/stunnel/stunnel.conf

      Add the following content to this file:

      /etc/stunnel/stunnel.conf

      fips = no
      setuid = nobody
      setgid = nogroup
      pid =
      debug = 7
      [redis-cli]
        client = yes
        accept = 127.0.0.1:8000
        connect = managed_redis_hostname_or_ip:managed_redis_port
      

      The first five lines in the file are global options, meaning they will apply to every service you include in this file:

      • fips: Enables or disables stunnel’s FIPS 140-2 mode. In this mode, stunnel will validate that the connection meets the Federal Information Processing Standard. Setting this to no disables this feature. Note that disabling this is not any less secure, but keeping it enabled (as it is by default) would require some extra configuration.
      • setuid: Defines the Unix user ID under which stunnel will run. By default, the stunnel process is owned by the root user. However, the stunnel documentation recommends that you drop administrative privileges once the tunnel starts, as failing to do so poses a security risk. Setting the setuid parameter to nobody will cause nobody, an unprivileged user, to take ownership over the stunnel process once the tunnel has been established.
      • setgid: Defines the Unix group ID under which stunnel will run. As with setuid, this configuration specifies a group without any special privileges — nogroup — to avoid any potential security issues.
      • pid: Defines a file location where stunnel will create a .pid file, a type of file that contains a process’s PID. .pid files are typically used by other programs to find the PID of a running process. By default, stunnel creates a .pid file in the /var/run/stunnel4/ directory. Because the nobody user doesn’t have permission to access that directory, it will prevent the tunnel from starting correctly. By not providing any argument to the pid parameter in this configuration we’re disabling this behavior, as a .pid file isn’t necessary for the purposes of this tutorial. If you do need a .pid file for your use case, though, be sure to set this to a file that nobody has permission to write to.
      • debug: Sets stunnel’s debugging level, which can range from 0 to 7. In this example we’ll set it to 7, the highest level available, as that will provide the most detailed information if stunnel runs into any issues. You can set it to any level you like, but be aware that the default setting is 5.

      The remaining lines are service-level options, and only apply to the tunnel we’ll create for redis-cli:

      • [redis-cli]: This is a service name and specifies that the following lines represent an individual service configuration for a client program. You can have more than one service in a stunnel configuration file, though each must be associated with an existing client application and you can’t have two services for the same application.
      • client: Setting this to yes tells stunnel to run in client mode, meaning that stunnel will connect to a TLS server (the managed Redis instance) rather than act as a TLS server.
      • accept: Defines the host and port on which stunnel will accept connections from the client. Here, we specify the IP address 127.0.0.1, which is an IPv4 loopback address used to represent localhost, and port 8000. This means stunnel will listen for connections originating from the Ubuntu server on port 8000 and encrypt them. Note that you can set the port to any port number you like as long as it’s not already in use.
      • connect: Defines the remote address and port to which stunnel will make the connection. Be sure to change this parameter to align with your managed database’s port and hostname or IP address.

      Note: The hostname or IP address and port you should specify in the connect directive will be specific to your own managed Redis database. These can usually be found in your cloud provider’s database management user interface where you provisioned your Redis instance.

      If you’re using a DigitalOcean Managed Redis Database, you can find this information by going to your Control Panel and clicking on Databases in the left-hand sidebar menu. Then, click on the name of the Redis instance you want to connect to and scroll down to the Connection Details section. There, you will find fields describing your database’s host and port.

      This is a fairly minimal configuration that leaves many of stunnel’s default settings in place. The program has many options available for you to create tunnels that suit your particular needs. See the official documentation for more details.

      After adding this content, save and close the file.

      Then, restart the stunnel4 service so stunnel will read the new configuration file:

      • sudo systemctl restart stunnel4

      Following that, you can test whether stunnel has created with netstat, a command line utility used to display network connections. Run the following command, which pipelines the netstat output into a grep command, which then searches it for every instance of stunnel:

      • sudo netstat -plunt | grep stunnel

      Output

      tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 17868/stunnel

      This output shows that stunnel is listening for connections on local port 8000.

      You can also confirm that the nobody user has taken ownership over the stunnel process with ps, a program that displays all currently-running processes:

      Output

      nobody 15674 0.0 0.1 121912 3180 ? Ssl 19:28 0:00 /usr/bin/stunnel4 /etc/stunnel/stunnel.conf . . .

      Here, you can see that nobody has indeed taken over the stunnel process.

      Stunnel is now fully configured and running on your system. You’re ready to connect to your managed Redis instance and test that the tunnel is working as expected.

      Step 3 — Connecting To Your Managed Database over TLS

      Now that you’ve installed redis-cli and configured stunnel on your server, you’re ready to connect to your managed database over TLS.

      Based on the settings defined in the configuration file created in Step 2, you would connect to your managed database with the following command:

      • redis-cli -h localhost -p 8000

      This command includes the -h flag, which tells redis-cli that the next argument will be the host to connect to. In this case, it’s localhost since we’re connecting to a tunnel created locally on the server. After that is the -p flag, which precedes the port of the local tunnel we’re connecting to, which in this case is port 8000.

      After running that command, you will be connected to your managed Redis server. Your prompt will change to reflect that you’ve connected and are in redis-cli’s interactive mode:

      Note: Oftentimes, managed databases are configured to require users to authenticate with a password when they connect. If your managed Redis instance requires a password, you can include the -a flag in your redis-cli command, followed by your password:

      • redis-cli -h localhost -p 8000 -a password

      Alternatively, you can authenticate by running the auth command followed by your password after establishing the connection:

      If you’re using a DigitalOcean Managed Database, you can find your Redis instance’s password in the same place you found its hostname and port. In your Control Panel, click on Databases in the left-hand sidebar menu. Then, click on the name of the Redis instance you’ve connected to. Scroll down to the Connection Details section, and there you’ll find a field labeled password. Click on the show button to reveal the password, then copy and paste it into either of these commands — replacing password — in order to authenticate.

      You can test whether the tunnel is working as expected by running the ping command from Redis’s interactive mode:

      If the connection is alive it will return PONG:

      Output

      PONG

      If, however, stunnel is not tunnelling traffic from your server to your Redis instance correctly, you may see an error message like this before being disconnected from Redis:

      Output

      Error: Server closed the connection

      If you receive this or a similar error, double check that you’ve entered your Redis instance’s hostname and port correctly in your stunnel.conf file. Likewise, be sure that you entered the correct port number in your redis-cli command.

      Once you’ve confirmed that the tunnel is working, go ahead and disconnect from your Redis instance:

      If you ever change stunnel’s configuration, you’ll need to reload or restart the stunnel4 service so stunnel will notice the changes:

      • sudo systemctl reload stunnel4

      However, if at any point in the future you want to close the TLS tunnel, you won’t be able to close it with systemctl. Instead, you’ll need to kill the process with the pkill command:

      After the tunnel has ben closed, you can reopen the tunnel by restarting the service:

      • sudo systemctl restart stunnel4

      Now that you’ve successfully configured stunnel, you’re all set to begin adding data to your managed Redis instance with redis-cli.

      Conclusion

      Stunnel is a handy tool for creating TLS tunnels and establishing secure connections to remote servers. This is especially useful in cases where the secure transport of information between machines is critical, as with a remote database.

      From here, you can begin exploring Redis and integrating it with your next application. If you’re new to working with Redis, you may find our series on How To Manage a Redis Database useful.



      Source link

      How To Connect to a Managed Database on Ubuntu 18.04


      Introduction

      Managed databases have a number of benefits over self-managed databases, including automated updates, simplified scaling, and high availability. If you’re new to working with managed databases, though, the best way to perform certain tasks — like connecting to the database — may not be self-evident.

      In this guide, we will go over how to install client programs for a variety of database management systems (DBMSs), including PostgreSQL, MySQL, and Redis, on an Ubuntu 18.04 server. We’ll also explain how to use these programs to connect to a managed database instance.

      Note: The instructions outlined in this guide were tested with DigitalOcean Managed Databases, but they should generally work for managed databases from any cloud provider. If, however, you run into issues connecting to a database provisioned from another provider, you should consult their documentation for help.

      Prerequisites

      To follow the instructions detailed in this guide, you will need:

      • Access to a server running Ubuntu 18.04. This server should have a non-root user with administrative privileges and a firewall configured with ufw. To set this up, follow our Initial Server Setup Guide for Ubuntu 18.04.
      • A managed database instance. This tutorial provides instructions on how to connect to a variety of database management systems, specifically PostgreSQL, MySQL, and Redis. To provision a DigitalOcean Managed Database, review our documentation for the DBMS of your choice:

      Once you have these in place, jump to whichever section aligns with your DBMS.

      Connecting to a Managed PostgreSQL Database

      To connect to a managed PostgreSQL database, you can use psql, the standard command line client for Postgres. It’s open-source, maintained by the PostgreSQL Development Group, and is usually included when you download the PostgreSQL server. However, you can install psql by itself by installing the postgresql-client package with APT.

      If you’ve not done so recently, update your server’s package index:

      Then run the following command to install psql:

      • sudo apt install postgresql-client

      APT will ask you to confirm that you want to install the package. Do so by pressing ENTER.

      Following that, you can connect to your managed Postgres database without any need for further configuration. For example, you might invoke psql with the following flags:

      • -U, the PostgreSQL user you want to connect as
      • -h, the managed database’s hostname or IP address
      • -p, the TCP port on which the managed database is listening for connections
      • -d, the specific database you want to connect to
      • -v, short for “variable,” precedes other connection variables, followed by an equal sign (=) and the variables’ values. For example, if you want to validate the database’s CA certificate when you connect, you would include -v sslmode=require in your command
      • -W, which tells psql to prompt you for the PostgreSQL user’s password. Note that you could precede the psql command with PGPASSWORD=password, but it’s generally considered more secure to not include passwords on the command line

      With these flags included, the psql command’s syntax would look like this:

      • psql -U user -h host -p port -d database -v variable=value -W

      Alternatively, if your managed database provider offers a uniform resource identifer (URI) for connecting, you might use the following syntax:

      • psql postgresql://username:password@host:port/database?option_1=value&option_n=value

      Note: If you’re connecting to a DigitalOcean Managed Database, you can find all of this connection information in your Cloud Control Panel. Click on Databases in the left-hand sidebar menu, then click on the database you want to connect to and scroll down to find its Connection Details section. From there, you do one of the following:

      • Select the Connection parameters option and copy the relevant fields individually into the psql syntax detailed previously
      • Select the Connection String option and copy a ready-made connection URI you can paste into the connection URI syntax outlined above
      • Select the Flags option and copy a ready-to-use psql command that you can paste into your terminal to make the connection

      With that, you’re ready to begin using with your managed PostgreSQL instance. For more information on how to interact with PostgreSQL, see our guide on How to Manage an SQL Database. You may also find our Introduction to Queries in PostgreSQL useful.

      Connecting to a Managed MySQL Database

      To connect to a managed MySQL database, you can use the official MySQL database client. On Ubuntu, this client is typically installed by downloading the mysql-client package through APT. If you’re using the default Ubuntu repositories, though, this will install version 5.7 of the program.

      In order to access a DigitalOcean Managed MySQL database, you will need to install version 8.0 or above. To do so, you must first add the MySQL software repository before installing the package.

      Note: If you don’t need to install the latest version of mysql-client, you can just update your server’s package index and install mysql-client without adding the MySQL software repository:

      • sudo apt update
      • sudo apt install mysql-client

      If you aren’t sure whether you need the latest version of mysql-client, you should consult your cloud provider’s managed databases documentation.

      Begin by navigating to the MySQL APT Repository page in your web browser. Find the Download button in the lower-right corner and click through to the next page. This page will prompt you to log in or sign up for an Oracle web account. You can skip that and instead look for the link that says No thanks, just start my download. Right-click the link and select Copy Link Address (this option may be worded differently, depending on your browser).

      Now you’re ready to download the file. On your server, move to a directory you can write to:

      Download the file using curl, remembering to paste the address you just copied in place of the highlighted portion of the following command. You also need to pass two command line flags to curl. -O instructs curl to output to a file instead of standard output. The L flag makes curl follow HTTP redirects, which is necessary in this case because the address you copied actually redirects to another location before the file downloads:

      • curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb

      The file should now be downloaded in your current directory. List the files to make sure:

      You will see the filename listed in the output:

      Output

      mysql-apt-config_0.8.13-1_all.deb . . .

      Now you can add the MySQL APT repository to your system’s repository list. The dpkg command is used to install, remove, and inspect .deb software packages. The following command includes the -i flag, indicating that you’d like to install from the specified file:

      • sudo dpkg -i mysql-apt-config*

      During the installation, you’ll be presented with a configuration screen where you can specify which version of MySQL you’d prefer, along with an option to install repositories for other MySQL-related tools. The defaults will add the repository information for the latest stable version of MySQL and nothing else. This is what we want, so use the down arrow to navigate to the Ok menu option and hit ENTER.

      Selecting mysql-apt-config configuration options

      Following that, the package will finish adding the repository. Refresh your apt package cache to make the new software packages available:

      Next, you can clean up your system a bit and delete the file you downloaded, as you won’t need it in the future:

      Note: If you ever need to update the configuration of these repositories, run the following command to select your new options:

      • sudo dpkg-reconfigure mysql-apt-config

      After selecting your new options, run the following command to refresh your package cache:

      Now that you’ve added the MySQL repositories, you’re ready to install the actual MySQL client software. Do so with the following apt command:

      • sudo apt install mysql-client

      Once that command finishes, check the software version number to ensure that you have the latest release:

      Output

      mysql Ver 8.0.17-cluster for Linux on x86_64 (MySQL Community Server - GPL)

      After you’ve installed the mysql-client package, you can access your managed database by running the mysql command with the following flags as arguments:

      • -u, the MySQL user you want to connect as
      • -p, tells mysql to prompt for the user’s password. You could include your password directly in the connection command following the -p flag (without a space, as in -ppassword) but, for security reasons, this is generally not recommended
      • -h, the database’s hostname or IP address
      • -P, the TCP port on which MySQL is listening for connections
      • -D, the specific database you want to connect to

      Using these flags, the mysql syntax will look like this:

      • mysql -u user -p -h host -P port -D database

      Alternatively, if you have a connection URI you can use to connect, you would use a syntax like this:

      • mysql mysql://user:password@host:port/database?option_1=value&option_n=value

      Note: If you’re connecting to a DigitalOcean Managed Database, you can find all of this connection information in your Cloud Control Panel. Click on Databases in the left-hand sidebar menu, then click on the database you want to connect to and scroll down to find its Connection Details section. From there, you do one of the following:

      • Select the Connection parameters option and copy the relevant fields individually into the mysql syntax outlined previously
      • Select the Connection String option and copy a ready-made connection URI you can paste into the connection string detailed above
      • Select the Flags option and copy a ready-to-use mysql command that you can paste into your terminal to make the connection

      With that, you’re ready to begin using with your managed MySQL instance. For more information on how to interact with MySQL, see our guide on How to Manage an SQL Database. You may also find our Introduction to Queries in MySQL useful.

      A Note Regarding Password Authentication in MySQL 8

      In MySQL 8.0 and newer, the default authentication plugin is caching_sha2_password. As of this writing, though, PHP does not support caching_sha2_password. If you plan on using your managed MySQL database with an application that uses PHP, such as WordPress or phpMyAdmin, this may lead to issues when the application attempts to connect to the database.

      If you have access to the database’s configuration file, you could add a setting to force it to use a PHP-supported authentication plugin — for example, mysql_native_password — by default:

      Example MySQL Configuration File

      [mysqld]
      default-authentication-plugin=mysql_native_password
      

      However, some managed database providers — including DigitalOcean — do not make the database configuration file available to end users. In this case, you could connect to the database and run an ALTER USER command for any existing MySQL users which need to connect to the database, but can’t do so with the caching_sha2_password plugin:

      • ALTER USER user IDENTIFIED WITH mysql_native_password BY 'password';

      Of course, you can set new users to authenticate with mysql_native_password by specifying the plugin in their respective CREATE USER statements:

      • CREATE USER user IDENTIFIED WITH mysql_native_password BY 'password';

      If you’re using a DigitalOcean Managed Database, be aware that if you configure a user to authenticate with a plugin other than caching_sha2_password then you won’t be able to see that user’s password in your Cloud Control Panel. For this reason, you should make sure you note down the passwords of any users that authenticate with mysql_native_password or other plugins in a secure location.

      Connecting to a Managed Redis Database

      When you install Redis locally, it comes with redis-cli, the Redis command line interface. You can use redis-cli to connect to a remote, managed Redis instance, but it doesn’t natively support TLS/SSL connections. For that reason, it’s recommended that you use an alternative Redis client to enable secure connections to Redis.

      For DigitalOcean Managed Redis Databases, we recommend that you install Redli, an open-source, interactive Redis terminal. To do so, navigate to the Releases Page on the Redli GitHub project and locate the Assets table for the latest release. As of this writing, this will be version 0.4.4.

      There, find the link for the file ending in linux_amd64.tar.gz. This link points to an archive file known as a tarball that, when extracted, will create a few files on your system. Right-click this link and select Copy link address (this option may differ depending on your web browser).

      On your server, move to a directory you can write to:

      Then, paste the link into the following wget command, replacing the highlighted URL. This command will download the file to your server:

      • wget https://github.com/IBM-Cloud/redli/releases/download/v0.4.4/redli_0.4.4_linux_amd64.tar.gz

      Once the file has been downloaded to your server, extract the tarball:

      • tar xvf redli_0.4.4_linux_amd64.tar.gz

      This will create the following files on your server:

      Output

      LICENSE.txt README.md redli

      The redli file is the Redli binary file. Move it to the /usr/local/bin directory, the location where Ubuntu looks for executable files:

      sudo mv redli /usr/local/bin/
      

      At this point, you can clean up your system a bit and remove the tarball:

      • rm redli 0.4.4_linux_amd64.tar.gz

      Now you can use Redli to connect to your managed Redis instance. You could do so by running the redli command followed by these flags:

      • -h, the host to connect to. This can either be a hostname or an IP address
      • -a, the password used to authenticate to the Redis instance
      • -p, the port to connect to

      With these flags included, the redli syntax would be as follows. Note that this example also includes the --tls option, which allows you to connect to a managed Redis database over TLS/SSL without the need for a tunnel:

      • redli --tls -h host -a password -p port

      One benefit that Redli has over redis-cli is that it understands the rediss protocol, which is used to designate a URI pointing to a Redis database. This allows you to use a connection string to access your database:

      • redli --tls -u rediss://user:password@host:port

      Note that this example includes the -u flag, which specifies that the following argument will be a connection URI.

      Note: If you’re connecting to a DigitalOcean Managed Database, you can find all of this connection information in your Cloud Control Panel. Click on Databases in the left-hand sidebar menu, then click on the database you want to connect to and scroll down to find the Connection Details section. From there, you do one of the following:

      • Select the Connection parameters option and copy the relevant fields individually into the redli syntax detailed previously
      • Select the Connection String option and copy a ready-made connection URI that you can use with the connection string syntax outlined above
      • Select the Flags option and copy a ready-to-use redli command that you can paste into your terminal to make the connection

      Following that, you can begin interacting with your managed Redis instance.

      Conclusion

      As a relatively new development in cloud services, many practices that are well known for self-managed databases aren’t widely or comprehensively documented for databases managed by cloud providers. One of the most fundamental of these practices, accessing the database, may not be immediately clear to those new to working with managed databases. Our goal for this tutorial is that it helps get you started as you begin using a managed database for storing data.

      For more information on working with databases, we encourage you to check out our variety of database-related content, including tutorials focused directly on PostgreSQL, MySQL, and Redis.

      To learn more about DigitalOcean Managed Databases, please see our Managed Databases product documentation.



      Source link