我可以使用连接 URL 的 redis-cli 吗?

Can I use redis-cli with a connection URL?

我习惯了psql,我可以通过给它提供连接字符串来使用它,而不必在不同的参数中打破它,也就是说,

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

例如,当我从 Heroku 获得这样的字符串时,这很有用。 我可以用 redis-cli 做类似的事情吗?我想直接给它提供一个连接字符串,例如当我安装 Redis 附加组件时作为环境变量存储在 Heroku 上的那个。那可能吗?我想使用的语法示例:

redis-cli redis://<username>:<password>@<host>:<port>

不,目前 (v3.2.1) redis-cli 不支持 URI 连接模式。如果需要,您可以在 Redis repository.

中为此创建功能或拉取请求

更新: -u 选项随 Redis 4.0 一起发布,请参阅 Release notes。例如:

redis-cli -u redis://user:pass@host:6379/0

对于那些等不及下一个 Redis 版本将无法更新他们的 redis-cli 的人,我制作了一个简单的 bash 函数,它似乎适用于我的案例 (Heroku)。这是要添加到 ~/.bash_profile~/.bashrc:

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

然后我可以在终端中输入以下内容:

redis-url redis://rediscloud:mysupersecurepassword@hostname.com:1234