Are you trying to establish connection to a Redis database?
This guide is for you.
With redis-cli, you can easily connect to a Redis database via the command line.
In this context, we shall look into the different methods to connect to a Redis database.
More information about Redis?
Redis is an open-source, in-memory key-value data store. A NoSQL database, Redis doesn't use structured query language, otherwise known as SQL. Redis instead comes with its own set of commands for managing and accessing data.
Basically, with its in-memory key-value data store, it can be used as a database, cache, and message broker.
Most times, users might need to connect to the Redis database if it installed locally or in a remote instance to perform most operations.
Here, you will learn how to connect to Redis from the command line, how to authenticate and test the connection, as well as how to close a Redis connection.
For instance, in Ubuntu 18.04 server running Redis version 4.0.9 using redis-cli can be done by following the process below.
How to connect to Redis Locally?
If you have Redis-server installed locally, we can connect to the Redis instance with the below command:
Basically, after running the above command we are in a Redis-cli’s interactive mode which presents with a read-eval-print loop (REPL) where we can run Redis’s built-in commands and receive replies.
In interactive mode, the command-line prompt will change to reflect the connection. The prompt indicates a connection to a Redis instance hosted locally (127.0.0.1) and accessed over Redis’s default port (6379).
Also, the alternative to running Redis commands in interactive mode is given below;
How to connect Redis Remotely ?
Similarly, the command that uses to connect to the remote Redis database;
redis-cli -h host -p port_number -a password
Also, with a Redis password, clients will be able to connect to Redis even not include the -a flag in their redis-cli command. But, they not allowed to perform any actions like add, change, or query data until they authenticate. In order to authenticate after connecting, we use the below auth command followed by the password:
127.0.0.1:6379> auth password
If the password is correct, it brings an OK message whereas it will return an error.