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.
- 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
- Obtain the load balancer IP address and port of the GeminiDB Redis instance that you want to access.
- For how to obtain the load balancer IP address, see Viewing the Load Balancer IP Address and Port.
- For how to obtain the port, see Viewing the Port for Accessing Each Instance Node.
- Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
- Install the Python client Redis-py of Python and Redis.
- If the system does not provide Python, you can use yum to install it.
- Run the following command to download and decompress the redis-py package:
wget https://github.com/andymccurdy/redis-py/archive/master.zip
- Go to the decompression directory and install the Python client Redis-py of Redis.
- 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 >>>
- 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.
- 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 >>>
- 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.
- Run the python command to enter the CLI mode.
- 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!'
- Connect to the GeminiDB Redis cluster using the single-node API.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot