Updated on 2024-05-20 GMT+08:00

Connecting to an Instance Using Python

This section describes how to use Python 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 Python client Redis-py of Python and Redis.

    1. If the system does not provide Python, you can use yum to install it.

      yum install python

    2. Run the following command to download and decompress the redis-py package:

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

    3. Go to the decompression directory and install the Python client Redis-py of Redis.

      python setup.py install

    4. After the installation, run the python command. If the following information is displayed, Redis-py is successfully installed:
      Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
      [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import redis
      >>> 

  4. 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.)

    • Connect to the GeminiDB Redis cluster using the single-node API.
      1. Run the python command to enter the CLI mode.
        You have entered CLI mode if the following command output is displayed:
        Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
        [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
        Type "help", "copyright", "credits" or "license" for more information.
        >>> import redis
        >>> 
      2. Run the following command in the CLI to check whether the result is normal.
        >>> r = redis.StrictRedis(host='192.xx.xx.xx', port=8635, password='pwd');
        >>> r.set('key', 'Python tst ok!')
        True
        >>> r.get('key')
        'Python tst ok!'

        Modify the following information based on service requirements before running the preceding command.

        • In the preceding command, host and port indicate the load balancer IP address and corresponding port of the GeminiDB Redis instance obtained in 1.
        • password indicates the password of the instance.
    • Connect to the GeminiDB Redis cluster using the cluster API.
      Configure config set CompatibleMode ClusterClient first.
      Python 3.7.4 (default, Jan 30 2021, 09:00:44) 
      [GCC 7.3.0] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> from redis.cluster import RedisCluster as Redis
      >>> rc = Redis(host='127.0.0.1', port=6379, password='a')
      >>> rc.set('key', 'Python test ok!')
      True
      >>> rc.get('key')
      b'Python test ok!'