问题描述
使用 rediscloud 1GB plan,我似乎无法连接到指定的孤立数据库。例如,在 ruby 中(使用 ruby 2.2.10,redis ruby 库 v3.3.5):
redis = Redis.new(url: ENV['REdisCLOUD_URL'],db: 4)
redis.get 'foo' # => Redis::CommandError: ERR DB index is out of range
在url
参数中指定db时得到类似的结果:
redis = Redis.new(url: "#{ENV['REdisCLOUD_URL']}/4")
redis.get 'foo' # => Redis::CommandError: ERR DB index is out of range
我在其他redis实例中没有遇到过这个问题。我对 redis、dbs 或 rediscloud 有什么误解吗?
解决方法
当使用 url
选项初始化 Redis 时,这不能与任何其他选项结合使用。来自 source code 的评论:
# @option options [String] :url (value of the environment variable REDIS_URL)
# a Redis URL,for a TCP connection:
# `redis://:[password]@[hostname]:[port]/[db]` (password,port and database
# are optional),for a unix socket connection: `unix://[path to Redis socket]`.
# This overrides all other options.
这意味着当您想将另一个数据库配置为默认数据库(在您的示例中为 4
而不是 0
),那么您必须将数据库编号作为路径段添加到 URL 中,如下所示:
redis = Redis.new(url: "#{ENV['REDISCLOUD_URL']}/4")
,
据我所知,rediscloud 是一种不会自动附带数据库的产品。它需要您手动添加它们,尽管该计划表明已包含这些数据库。 Relevant documentation.