Access a DCS Redis instance through redis-py on an ECS in the same VPC. For more information about how to use other Redis clients, visit the Redis official website.
Use redis-py to connect to single-node, master/standby, and Proxy Cluster instances and redis-py-cluster to connect to Redis Cluster instances.
For details, see Viewing Details of a DCS Instance.
The following uses CentOS as an example to describe how to access an instance using a Python client.
If the system does not provide Python, run the following yum command to install it:
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:
wget https://github.com/andymccurdy/redis-py/archive/master.zip
unzip master.zip
After the installation, run the python command. redis-py have been successfully installed if the following command output is displayed:
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 the IP address/domain name and port 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.
In the following steps, commands are executed in CLI mode. (Alternatively, write the commands into a Python script and then execute the script.)
>>> from rediscluster import RedisCluster >>> startup_nodes = [{"host": "192.168.0.143", "port": "6379"}] >>> rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True) >>> rc.set("foo", "bar") True >>> print(rc.get("foo")) 'bar'