Search code examples
phpredispredisphpredis

PHP Redis invalid DB index


I'm connecting to Redis Labs to use redis for our application.

Here is my configurations:

$conf=[
 'scheme'=>'tcp',
 'host'=>'ec2.cloud.redislabs.com',
 'port'=>12860,
 'database'=>'selector',
 'password'=>'redispassword'
];

Then I do this to load the predis library:

require "lib/predis/autoload.php";

After which I do this:

$client = new Predis\Client($conf);

Then I try to run a command:

$client->hset('comments',"comment:1","{json_encoded_data}");

And this is the error I receive:

PHP Fatal error:  Uncaught exception 'Predis\Connection\ConnectionException' with message '`SELECT` failed: ERR invalid DB index [tcp://cloud.redislabs.com:12860]' in /redis/lib/predis/src/Connection/AbstractConnection.php:155

I've looked online and people say I should set the database as 0 however If I do that how do I use my selected database?

Not sure what to do here.


Solution

  • The host parameter is for your instance endpoint:

    enter image description here

    And RedisLabs only allows one database per instance, so you'll have no problem using 0 in the database parameter:

    That is exactly the reason why each of our Redis Cloud instances allows using just one database (the default, 0-numbered database). This ensures that any two databases in our service will never compete on the resources of a single thread. In fact, trying to use any database other than 0 with our service should produce an error.

    Source