Updated on 2024-02-08 GMT+08:00

Connecting to an Instance Using PHP

This section describes how to use PHP to access a GeminiDB Redis instance.

Prerequisites

  • A GeminiDB Redis instance has been created and is in the Available status.
  • An ECS is available. For details, see Purchasing an ECS.
  • The GNU Compiler Collection (GCC) has been installed on the ECS.
  • The created ECS is in the same region, AZ, VPC, and security group as the GeminiDB Redis instance.

Procedure

  1. Obtain the load balancer IP address and port of the GeminiDB Redis instance that you want to access.

  2. Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
  3. Install the PHP development kit and command line tool.

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

    yum install php-devel php-common php-cli

    CentOS (Red Hat series) is used as an example. If Ubuntu (Debian series) is used, run the corresponding installation command.

  4. After the installation is complete, check the version number to ensure that the installation is successful.

    php --version

  5. Install the PHP client of Redis.

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

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

      The preceding clients are of the latest version. You can download the phpredis client of other versions from the 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 PHP installation method and location depend on the operating system. Before the configuration, run the find / -name php.ini command to check the directory of the file.

    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.

      Run the following command to find the php.ini file:

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

      Add the following configuration item to the php.ini file:

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

      The directories of the php.ini and redis.so files may be different. You can run the following command to query the directories.

      find / -name php.ini

      find / -name redis.so

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

      php -m |grep redis

      If redis is returned, the PHP Redis client environment has been set up.

  6. Use the phpredis client to connect to the instance.

    1. Compile the test code redis.php.
      • Connect to a GeminiDB Redis cluster using the SDK for the single-node API on PHP.
        <?php
            // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
            // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
            $pwd =getenv('EXAMPLE_PASSWORD_ENV');
            $redis_host = "192.xx.xx.xx";  //Enter the load balancer IP address obtained in step 1.
            $redis_port = 8635;
            $user_pwd = pwd;
            $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("key", "php test ok!") == false) {
                die($redis->getLastError());
            }
            $value = $redis->get("key");
            echo $value;
            $redis->close();
        ?>
      • Connect to the GeminiDB Redis cluster using the SDK for the cluster API on PHP.
        <?php
                $redis_host = "192.xx.xx.xx";  //Enter the load balancer IP address obtained in step 1.
                $redis_port = 8635;
                $user_pwd = "pwd";
                // Connect with read/write timeout as well as specify that phpredis should use
                // persistent connections to each node.
                $redis = new RedisCluster(NULL, Array("$redis_host:$redis_port"), 1.5, 1.5, true,  $user_pwd);
                if ($redis->set("key", "php test ok!") == false) {
                    die($redis->getLastError());
                }
                $value = $redis->get("key");
                echo $value;
                $redis->close();
            ?>
    2. Run the redis.php command to check whether the result is normal.