Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use redis-cli with a connection URL?

I am used to psql which I can use by feeding it the connection string without having to break it in different arguments, that is,

psql postgres://<username>:<password>@<host>:<port> 

This is useful when I have such string from Heroku, for example. Can I do something similar with redis-cli? I want to feed it directly a connection string, such as the one that is stored on Heroku as environment variable when I install a Redis add-on. Is that possible? Example of the syntax I would like to use:

redis-cli redis://<username>:<password>@<host>:<port> 
like image 352
Alessandro Cosentino Avatar asked Jul 08 '16 16:07

Alessandro Cosentino


People also ask

How do I connect to Redis terminal?

To start Redis client, open the terminal and type the command redis-cli. This will connect to your local server and now you can run any command.

What is Redis URL?

DESCRIPTION. The URI::redis class supports URI objects belonging to the redis and redis+unix URI scheme. Such URLs are used to encode connection info ( redis : host, port, password, database, redis+unix : socket path, password, database) to Redis servers.

How does Redis-cli work?

It has two main modes: an interactive Read Eval Print Loop (REPL) mode where the user types Redis commands and receives replies, and a command mode where redis-cli is executed with additional arguments and the reply is printed to the standard output.


2 Answers

No, at the moment (v3.2.1) redis-cli does not support the URI connection schema. If you want, you can make a feature or pull request for that in the Redis repository.

UPDATE: The -u option was released with Redis 4.0, see Release notes. For example:

redis-cli -u redis://user:pass@host:6379/0 
like image 159
Itamar Haber Avatar answered Oct 11 '22 18:10

Itamar Haber


For those who cannot wait for the next Redis release won't be able to update their redis-cli, I made a simple bash function that seems to work for my case (Heroku). This is to be added to ~/.bash_profile or ~/.bashrc:

function redis-url() {   # Get the first argument as the URL variable   url=$1   # Parse and generate the command: redis-cli -h [hostname] -p [port] -a [password]   cmd=`echo $url | sed 's_redis://\(.*\):\(.*\)@\(.*\):\(.*\)_redis-cli -h \3 -p \4 -a \2_'`   # Run the command   $cmd } 

I can then type the following in my terminal:

redis-url redis://rediscloud:[email protected]:1234 
like image 38
Bruno A. Avatar answered Oct 11 '22 20:10

Bruno A.