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 Redis on ioredis (Node.js)

Updated on 2024-07-29 GMT+08:00

This section describes how to access a Redis instance on ioredis. For more information about how to use other Redis clients, visit the Redis official website.

NOTE:

The operations described in this section apply only to single-node, master/standby, and Proxy Cluster instances. To access a Redis Cluster instance on ioredis, see Node.js Redis client description.

Prerequisites

  • A DCS Redis instance has been created and is in the Running state.
  • An ECS has been created. For details about how to create an ECS, 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.

Connecting to Redis on ioredis

  • For client servers running Ubuntu (Debian series):
  1. View the IP address and port number of the DCS Redis instance to be accessed.

    For details, see Viewing Details of a DCS Instance.

  2. Log in to the ECS.
  3. Install Node.js.

    apt install nodejs-legacy

    If the preceding command does not work, run the following commands:

    wget https://nodejs.org/dist/v0.12.4/node-v0.12.4.tar.gz --no-check-certificate

    tar -xvf node-v4.28.5.tar.gz

    cd node-v4.28.5

    ./configure

    make

    make install

    NOTE:

    After the installation is complete, run the node --version command to query the Node.js version to check whether the installation is successful.

  4. Install the node package manager (npm).

    apt install npm

  5. Install the Redis client ioredis.

    npm install ioredis

  6. Edit the sample script for connecting to a DCS instance.

    Add the following content to the ioredisdemo.js script, including information about connection and data reading.

    var Redis = require('ioredis');
    var redis = new Redis({
      port: 6379,          // Redis port
      host: '192.168.0.196',   // Redis host
      family: 4,           // 4 (IPv4) or 6 (IPv6)
      password: '******',
      db: 0
    }); 
    redis.set('foo', 'bar');
    redis.get('foo', function (err, result) {
      console.log(result);
    }); 
    // Or using a promise if the last argument isn't a function
    redis.get('foo').then(function (result) {
      console.log(result);
    });
    // Arguments to commands are flattened, so the following are the same:
    redis.sadd('set', 1, 3, 5, 7);
    redis.sadd('set', [1, 3, 5, 7]);
    // All arguments are passed directly to the redis server:
    redis.set('key', 100, 'EX', 10);

    host indicates the example IP address of the DCS instance and port indicates the port number of the DCS instance. For details about how to obtain the IP address and port, see 1. Change them as required. ****** indicates the password used for logging in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation.

  7. Run the sample script to access the chosen DCS instance.

    node ioredisdemo.js

  • For client servers running CentOS (Red Hat series):
  1. View the IP address and port number of the DCS Redis instance to be accessed.

    For details, see .

  2. Log in to the ECS.
  3. Install Node.js.

    yum install nodejs

    If the preceding command does not work, run the following commands:

    wget https://nodejs.org/dist/v0.12.4/node-v0.12.4.tar.gz --no-check-certificate

    tar -xvf node-v0.12.4.tar.gz

    cd node-v0.12.4

    ./configure

    make

    make install

    NOTE:

    After the installation is complete, run the node --version command to query the Node.js version to check whether the installation is successful.

  4. Install npm.

    yum install npm

  5. Install the Redis client ioredis.

    npm install ioredis

  6. Edit the sample script for connecting to a DCS instance.

    Add the following content to the ioredisdemo.js script, including information about connection and data reading.

    var Redis = require('ioredis');
    var redis = new Redis({
      port: 6379,          // Redis port
      host: '192.168.0.196',   // Redis host
      family: 4,           // 4 (IPv4) or 6 (IPv6)
      password: '******',
      db: 0
    });
    redis.set('foo', 'bar');
    redis.get('foo', function (err, result) {
      console.log(result);
    });
    // Or using a promise if the last argument isn't a function
    redis.get('foo').then(function (result) {
      console.log(result);
    }); 
    // Arguments to commands are flattened, so the following are the same:
    redis.sadd('set', 1, 3, 5, 7);
    redis.sadd('set', [1, 3, 5, 7]); 
    // All arguments are passed directly to the redis server:
    redis.set('key', 100, 'EX', 10);

    host indicates the example IP address of the DCS instance and port indicates the port number of the DCS instance. For details about how to obtain the IP address and port, see 1. Change them as required. ****** indicates the password used for logging in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation.

  7. Run the sample script to access the chosen DCS instance.

    node ioredisdemo.js

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