Halaman ini belum tersedia dalam bahasa lokal Anda. Kami berusaha keras untuk menambahkan lebih banyak versi bahasa. Terima kasih atas dukungan Anda.

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 redis-py (Python)

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

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

The following operations are based on an example of accessing a Redis instance on a client on an elastic cloud server (ECS).

NOTE:

Use redis-py to connect to single-node, master/standby, and Proxy Cluster instances and redis-py-cluster to connect to Redis Cluster instances.

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 Python compilation environment has been installed on the ECS.

Connecting to Redis on redis-py

  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.

    The following uses CentOS as an example to describe how to access an instance using a Python client.

  3. Access the DCS Redis instance.

    If the system does not provide Python, run the following yum command to install it:

    yum install python
    NOTE:

    The Python version must be 3.6 or later. If the default Python version is earlier than 3.6, perform the following operations to change it:

    1. Run the rm -rf python command to delete the Python symbolic link.
    2. Run the ln -s pythonX.X.X python command to create another Python link. In the command, X.X.X indicates the Python version number.
    • If the instance is a single-node, master/standby, or Proxy Cluster instance:
      1. Install Python and redis-py.
        1. If the system does not provide Python, run the yum command to install it.
        2. Run the following command to download and decompress the redis-py package:

          wget https://github.com/andymccurdy/redis-py/archive/master.zip

          unzip master.zip

        3. Go to the directory where the decompressed redis-py package is saved, and install redis-py.

          python setup.py install

          After the installation, run the python command. redis-py have been successfully installed if the following command output is displayed:

          Figure 1 Running the python command
      2. Use the redis-py client to connect to the instance. In the following steps, commands are executed in CLI mode. (Alternatively, write the commands into a Python script and then execute the script.)
        1. Run the python command to enter the CLI mode. You have entered CLI mode if the following command output is displayed:
          Figure 2 Entering the CLI mode
        2. Run the following command to access the chosen DCS Redis instance:
          r = redis.StrictRedis(host='XXX.XXX.XXX.XXX', port=6379, password='******');

          XXX.XXX.XXX.XXX indicates the IP address of the DCS instance and 6379 is an example port number of the 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.

          You have successfully accessed the instance if the following command output is displayed. Enter commands to perform read and write operations on the database.

          Figure 3 Redis connected successfully
    • If the instance is a Redis Cluster instance:
      1. Install the redis-py-cluster client.
        1. Download the released version.

          wget https://github.com/Grokzen/redis-py-cluster/releases/download/2.1.3/redis-py-cluster-2.1.3.tar.gz

        2. Decompress the package.

          tar -xvf redis-py-cluster-2.1.3.tar.gz

        3. Go to the directory where the decompressed redis-py-cluster package is saved, and install redis-py-cluster.

          python setup.py install

      2. Access the DCS Redis instance by using redis-py-cluster.

        In the following steps, commands are executed in CLI mode. (Alternatively, write the commands into a Python script and then execute the script.)

        1. Run the python command to enter the CLI mode.
        2. Run the following command to access the chosen DCS Redis instance. If the instance does not have a password, exclude password='******' from the command.
          >>> from rediscluster import RedisCluster
          
          >>> startup_nodes = [{"host": "192.168.0.143", "port": "6379"},{"host": "192.168.0.144", "port": "6379"},{"host": "192.168.0.145", "port": "6379"},{"host": "192.168.0.146", "port": "6379"}]
          
          >>> rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True, password='******')
          
          >>> rc.set("foo", "bar")
          True
          >>> print(rc.get("foo"))
          'bar'

Kami menggunakan cookie untuk meningkatkan kualitas situs kami dan pengalaman Anda. Dengan melanjutkan penelusuran di situs kami berarti Anda menerima kebijakan cookie kami. Cari tahu selengkapnya

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback