Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Connecting to an Instance Using PHP

Updated on 2025-01-24 GMT+08:00

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.
  • 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

    NOTE:

    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

      NOTE:

      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

      NOTE:

      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"
      NOTE:

      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 of 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 = 6379;
            $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 of the cluster API on PHP.
        <?php
                $redis_host = "192.xx.xx.xx";  //Enter the load balancer IP address obtained in step 1.
                $redis_port = 6379;
                $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.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback