Reviewed-by: Liudmila Denisova <ldenisov@noreply.gitea.eco.tsi-dev.otc-service.com> Co-authored-by: chenjunjie <chenjunjie@huawei.com> Co-committed-by: chenjunjie <chenjunjie@huawei.com>
15 KiB
Connecting to Redis on redis-py (Python)
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).
Notes and Constraints
Use redis-py to connect to single-node, master/standby, read/write splitting, and Proxy Cluster instances and redis-py-cluster to connect to Redis Cluster instances.
To access a Redis 7.0 instance, use a redis-py 4.3.0 or later client. 5.0.0 and later versions are recommended.
Prerequisites
- A Redis instance is created, and is in the Running state.
- An ECS has been created. For details about how to create an ECS, see Elastic Cloud Server User Guide
- If the ECS runs the Linux OS, ensure that the Python compilation environment has been installed on the ECS.
Procedure
- To access a single-node, master/standby, read/write splitting, or Proxy Cluster instance, see Accessing a Non-Redis Cluster Instance Using redis-py.
- To access a Redis Cluster instance, see Accessing a Redis Cluster Instance Using redis-py.
Accessing a Non-Redis Cluster Instance Using redis-py
- View the IP address/domain name and port of the DCS Redis instance to be accessed.
For details, see Viewing and Modifying DCS Instance Information.
- Log in to the ECS.
The following uses CentOS as an example to describe how to access an instance using a Python client.
- Access the DCS Redis instance.
- If the ECS OS does not provide Python, run the following yum command to install it:
yum install python
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:
- Run the rm -rf python command to delete the Python symbolic link.
- 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.
- Install Python and redis-py.
- If the system does not provide Python, run the yum command 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 unzip master.zip
- 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:
- 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.)
- Run the python command to enter the CLI mode. You have entered CLI mode if the following command output is displayed:
- 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/domain name of the DCS instance and 6379 is an example port number of the instance. For details about how to obtain the IP address/domain name 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. Omit the , password='******' part in the command for a password-free instance.
You have successfully accessed the instance if the following command output is displayed. Enter commands to perform read and write operations on the database.
- If the ECS OS does not provide Python, run the following yum command to install it:
Accessing a Redis Cluster Instance Using redis-py
- View the IP address/domain name and port of the DCS Redis instance to be accessed.
For details, see Viewing and Modifying DCS Instance Information.
- Log in to the ECS.
The following uses CentOS as an example to describe how to access an instance using a Python client.
- Access the DCS Redis instance.
- If the ECS OS does not provide Python, run the following yum command to install it:
yum install python
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:
- Run the rm -rf python command to delete the Python symbolic link.
- 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.
- Install the redis-py-cluster client.
- 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
- Decompress the package.
tar -xvf redis-py-cluster-2.1.3.tar.gz
- Go to the directory where the decompressed redis-py-cluster package is saved, and install redis-py-cluster.
python setup.py install
- Download the released version.
- 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.)
- Run the python command to enter the CLI mode.
- Run the following command to access the chosen DCS Redis instance:
>>> 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' >>>****** indicates the password used for logging in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation. Omit the , password='******' part in the command for a password-free instance.
You have successfully accessed the instance if the following command output is displayed. Enter commands to perform read and write operations on the database.
- If the ECS OS does not provide Python, run the following yum command to install it:


