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

Connecting to Memcached on the Libmemcached (PHP)

Updated on 2024-10-28 GMT+08:00

Access a DCS Memcached instance using a PHP client on an ECS in the same VPC.

Prerequisites

  • The DCS Memcached instance you want to access is in the Running state.
  • Log in to the ECS. For details on how to create ECSs, see the Elastic Cloud Server User Guide.
    NOTE:

    An ECS can communicate with a DCS instance that belongs to the same VPC and is configured with the same security group.

    • If the ECS and DCS instance are in different VPCs, establish a VPC peering connection to achieve network connectivity between the ECS and DCS instance. For details, see Does DCS Support Cross-VPC Access?
    • If different security groups have been configured for the ECS and DCS instance, set security group rules to achieve network connectivity between the ECS and DCS instance. For details, see How Do I Configure a Security Group?

OSs of Red Hat Series

The following uses CentOS 7.0 as an example to describe how to install a PHP client and use it to access a DCS Memcached instance. The procedure is also applicable to a PHP client running the Red Hat or Fedora OS.

  1. Install GCC-C++ and Make compilation components.

    yum install gcc-c++ make

  2. Install related SASL packages.

    yum install cyrus-sasl*

  3. Install the libMemcached library.

    Installing the libMemcached library requires SASL authentication parameters. Therefore, you cannot install the library by running the yum command.

    wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz

    tar -xvf libmemcached-1.0.18.tar.gz

    cd libmemcached-1.0.18

    ./configure --prefix=/usr/local/libmemcached --enable-sasl

    make && make install

    NOTE:

    Before installing the libMemcached library, install GCC-C++ and SASL components. Otherwise, an error will be reported during compilation. After you resolve the error, run the make clean command and then run the make command again.

  4. Install the PHP environment.

    yum install php-devel php-common php-cli

    NOTICE:

    PHP 7.x does not support SASL authentication. Use PHP 5.6. If the yum php version is not 5.6, download one from the Internet.

  5. Install the Memcached client.

    Note that you must add a parameter used to enable SASL when running the configure command.

    wget http://pecl.php.net/get/memcached-2.1.0.tgz

    tar zxvf memcached-2.1.0.tgz

    cd memcached-2.1.0

    phpize

    ./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl

    make && make install

  6. Modify the php.ini file.

    Run the find or locate command to find the php.ini file.

    find / -name php.ini

    Add the following two lines to the php.ini file:

    extension=memcached.so
    memcached.use_sasl = 1
    Figure 1 Modifying the php.ini file

  7. Access a DCS Memcached instance.

    Create a memcached.php file and add the following content to the file:

    <?php
        $connect = new Memcached; //Declares a Memcached connection.
        $connect->setOption(Memcached::OPT_COMPRESSION, false); //Disables compression.
        $connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //Uses the binary protocol.
        $connect->setOption(Memcached::OPT_TCP_NODELAY, true); //Disables the TCP network delay policy.
        $connect->addServer('{memcached_instance_ip}', 11211); //Specifies the instance IP address and port number.
        $connect->setSaslAuthData('{username}', '{password}'); //If password-free access is enabled for the instance, delete or comment out this line.
        $connect->set("DCS", "Come on!");
        echo 'DCS: ',$connect->get("DCS");
        echo "\n";
        $connect->quit();
    ?>

    Save and run the memcached.php file. The following result is displayed.

    [root@testphpmemcached ~]# php memcached.php 
        DCS: Come on!
    [root@testphpmemcached ~]# 

OSs of Debian Series

The following uses the Ubuntu OS as an example to describe how to install a PHP client and use it to access a DCS Memcached instance.

  1. Install GCC and Make compilation components.

    apt install gcc make

  2. Install the PHP environment.

    PHP 5.x is recommended for better compatibility with SASL authentication.

    Run the following commands to add the image source of PHP of an earlier version, and then install the php.5.6 and php.5.6-dev packages:

    apt-get install -y language-pack-en-base;

    LC_ALL=en_US.UTF-8;

    add-apt-repository ppa:ondrej/php;

    apt-get update;

    apt-get install php5.6 php5.6-dev;

    After the installation is complete, run the php -version command to check the PHP version. If the following result is displayed, the PHP version is 5.6, indicating that PHP 5.6 is successfully installed.

    root@dcs-nodelete:/etc/apt# php -version
    PHP 5.6.36-1+ubuntu16.04.1+deb.sury.org+1 (cli) 
    Copyright (c) 1997-2016 The PHP Group
    NOTE:

    To uninstall PHP, run the following commands:

    apt install aptitude -y

    aptitude purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`

  3. Install the SASL component.

    apt install libsasl2-dev cloog.ppl

  4. Install the libMemcached library.

    wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz

    tar -xvf libmemcached-1.0.18.tar.gz

    cd libmemcached-1.0.18

    ./configure --prefix=/usr/local/libmemcached

    make && make install

    NOTE:

    Before installing the libMemcached library, install GCC-C++ and SASL components. Otherwise, an error will be reported during compilation. After you resolve the error, run the make clean command and then run the make command again.

  5. Install the Memcached client.

    Install the zlib component.

    apt install zlib1g.dev

    Note that you must add a parameter used to enable SASL when running the configure command.

    wget http://pecl.php.net/get/memcached-2.2.0.tgz;

    tar zxvf memcached-2.2.0.tgz;

    cd memcached-2.2.0;

    phpize5.6;

    ./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl;

    make && make install;

  6. Modify the pdo.ini file.

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

    find / -name pdo.ini

    By default, the pdo.ini file is stored in the /etc/php/5.6/mods-available directory. Add the following two lines to the php.ini file:

    extension=memcached.so
    memcached.use_sasl = 1
    Figure 2 Modifying the pdo.ini file

  7. Access a DCS Memcached instance.

    Create a memcached.php file and add the following content to the file:

    <?php
        $connect = new Memcached; //Declares a Memcached connection.
        $connect->setOption(Memcached::OPT_COMPRESSION, false); //Disables compression.
        $connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //Uses the binary protocol.
        $connect->setOption(Memcached::OPT_TCP_NODELAY, true); //Disables the TCP network delay policy.
        $connect->addServer('{memcached_instance_ip}', 11211); //Specifies the instance IP address and port number.
        $connect->setSaslAuthData('{username}', '{password}'); //If password-free access is enabled for the instance, delete or comment out this line.
        $connect->set("DCS", "Come on!");
        echo 'DCS: ',$connect->get("DCS");
        echo "\n";
        $connect->quit();
    ?>

    Save and run the memcached.php file. The following result is displayed.

    [root@dcs-nodelete ~]# php memcached.php 
        DCS: Come on!
    [root@dcs-nodelete ~]# 

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