Using phpredis to Access an Instance

Access a DCS Redis instance through phpredis on an ECS in the same VPC. For more information on how to use other Redis clients, visit https://redis.io/clients.

The operations described in this section apply only to single-node, master/standby, and Proxy Cluster instances. To use phpredis to connect to a Redis Cluster instance, see https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#readme.

Prerequisites

  • The DCS Redis instance you want to access is in the Running state.
  • An ECS has been created to serve as your Redis client. For more information on how to create ECSs, see the Elastic Cloud Server User Guide.
  • If the ECS runs the Linux OS, ensure that the GCC compilation environment has been installed on the ECS.

Procedure

  1. View the IP address/domain name and port number of the DCS Redis instance to be accessed.

    For details, see Viewing Instance Details.

  2. Log in to the ECS.
  3. Install GCC-C++ and Make compilation components.

    yum install gcc-c++ make

  4. Install the PHP development package and CLI tool.

    Run the following yum command to install the PHP development package:

    yum install php-devel php-common php-cli

    After the installation is complete, run the following command to query the PHP version and check whether the installation is successful:

    php --version

  5. Install the phpredis client.

    1. Run the following command to download the source phpredis package:

      wget http://pecl.php.net/get/redis-4.1.0RC3.tgz

      The phpredis client downloaded by running the preceding command is of the latest version. To download phpredis clients of other versions, visit the Redis or PHP official website.

    2. Run the following commands to decompress the source phpredis package:

      tar -zxvf redis-4.1.0RC3.tgz

      cd redis-4.1.0RC3

    3. Run the following extension command before compilation:

      phpize

    4. Run the following command to configure the php-config file:

      ./configure --with-php-config=/usr/bin/php-config

      The location of the file varies depending on the OS and PHP installation mode. You are advised to locate the directory where the file is saved before the configuration.

      find / -name php-config

    5. Run the following command to compile and install the phpredis client:

      make && make install

    6. After the installation, add the extension configuration in the php.ini file to reference the Redis module.

      vim /usr/local/php/etc/php.ini

      Add the following configuration:

      extension = "/usr/lib64/php/modules/redis.so"

      The redis.so file may be saved in a different directory from php.ini. Run the following command to locate the directory:

      find / -name php.ini

    7. Save the configuration and exit. Then, run the following command to check whether the extension takes effect:

      php -m |grep redis

      If the command output contains redis, the phpredis client environment has been set up.

  6. Access the DCS instance by using phpredis.

    1. Edit a redis.php file.
      <?php
          $redis_host = "{redis_instance_address}";
          $redis_port = 6379;
          $user_pwd = "{password}";
          $redis = new Redis();
          if ($redis->connect($redis_host, $redis_port) == false) {
             die($redis->getLastError());
          }
          if ($redis->auth($user_pwd) == false) {
              die($redis->getLastError());
          }
          if ($redis->set("welcome", "Hello, DCS for Redis!") == false) {
              die($redis->getLastError());
          }
          $value = $redis->get("welcome");
          echo $value;
          $redis->close();
      ?>

      {redis_instance_address} indicates the IP address/domain name of DCS instance and 6379 is an example port number of DCS instance. For details on how to obtain the IP address/domain name and port, see 1. Change the IP address/domain name and port as required. {password} indicates the password used for login to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation. If password-free access is enabled, shield the if statement for password authentication.

    2. Run the php redis.php command to access the DCS instance.

Video Guide

Watch the following video about accessing a DCS Redis instance.

Accessing a DCS Instance