redis-cli is a built-in command-line interface (CLI) of native Redis. You can use redis-cli to connect to an ApsaraDB for Redis instance from an Elastic Compute Service (ECS) instance or on-premises host and manage data.

The type of network used to connect to an ApsaraDB for Redis instance.

Network type Description
VPC (recommended) A virtual private cloud (VPC) is a private network dedicated to you on Alibaba Cloud. VPCs are logically isolated from each other at Layer 2 to provide higher security and performance. When redis-cli is deployed on an ECS instance, you can connect to an ApsaraDB for Redis instance through a VPC to improve security and reduce network latency.
Internet If you want to manage an ApsaraDB for Redis instance on an on-premises host, you can deploy redis-cli on the host and connect to the ApsaraDB for Redis instance over the Internet.
Note No network traffic data is charged if you connect to the ApsaraDB for Redis instance over the Internet. But the connection may be exposed to security risks. We recommend that you use a VPC to improve security.

Overview

  • redis-cli is a built-in command-line interface (CLI) of native Redis. You can deploy the tool on an ECS instance or an on-premises host by installing Redis services.
    Note In addition to redis-cli, you can also use Data Management Service (DMS) to connect to a database of an ApsaraDB for Redis instance without a client program. This helps manage the database in a graphic user interface. For more information, see Use DMS.
  • The redis-cli version can be different from the major version of the ApsaraDB for Redis instance.

redis-cli connection method (Linux)

  1. Install redis-cli. Skip this step if it is already installed.
    1. Log on to the device on which you want to install redis-cli, such as an ECS instance or an on-premises host.
    2. Run the following command to download the Redis source package:
      wget https://download.redis.io/releases/redis-6.0.9.tar.gz
      Note This topic uses Redis 6.0.9 as an example to demonstrate the operations. You can also install other versions. For more information, see the Redis official website.
    3. Run the following command to decompress the Redis source code:
      tar xzf redis-6.0.9.tar.gz
    4. Run the following command to go to the directory where the Redis source code is extracted. Compile and install Redis source code:
      cd redis-6.0.9&&make
      Note It takes about two or three minutes to compile and install redis-cli.
  2. Perform the following operations based on redis-cli installation location:
    redis-cli installation location Operations
    ECS instance (Recommended)
    1. Make sure that the ECS instance and the ApsaraDB for Redis instance are deployed in the same VPC. The ECS instance and ApsaraDB for Redis instance must have the same VPC ID.
      Note If they are deployed in different VPCs, you must change the VPC to which the ECS instance belongs. For more information, see Change the VPC of an ECS instance.
    2. Obtain the internal IP address of an ECS instance. For more information, see How do I query IP addresses of ECS instances?
    3. Add the internal IP address of the ECS instance to the whitelist of the ApsaraDB for Redis instance. For more information, see Set IP address whitelists.
    On-premises host
    1. By default, an ApsaraDB for Redis instance provides only an internal endpoint. You must apply for a public IP address when you want to connect to an ApsaraDB for Redis instance over the Internet. For more information, see Apply for a public endpoint for an ApsaraDB for Redis instance.
    2. Run the curl ipinfo.io |grep ip command on the on-premises host to obtain the public IP address. The returned result is shown in the following figure:View the public IP address
    3. Add the public IP address of the on-premises host to the whitelist of the ApsaraDB for Redis instance. For more information, see Set IP address whitelists.
  3. Obtain the connection information and run the following command to connect to an ApsaraDB for Redis instance.
    src/redis-cli -h <hostname> -p <port> [-c]
    Table 1. Parameters
    Parameter Description Method
    <hostname> The endpoint of the ApsaraDB for Redis instance.
    • If you connect an ECS instance to the ApsaraDB for Redis instance through a VPC, you must obtain the endpoint of the ApsaraDB for Redis instance in the VPC.
    • If you connect an on-premises host to the ApsaraDB for Redis instance over the Internet, you must obtain the public IP address of the ApsaraDB for Redis instance.

    For more information, see View endpoints.

    <port> The port number of the ApsaraDB for Redis instance. The default port number is 6379. You can also specify a custom port number. For more information, see Modify the port for the endpoint.
    -c Enable the cluster mode Only when you connect to a cluster instance of ApsaraDB for Redis by using a private endpoint, you must add the -c parameter.
    Command example by using the default endpoint (Applicable to scenarios where ApsaraDB for Redis instances are connected by using default endpoints, such as endpoints of standard instances and proxy endpoints of cluster instances):
    src/redis-cli -h r-bp1zxszhcgatnx****.redis.rds.aliyuncs.com -p 6379
    Command example for private endpoints (Applicable to scenarios where an ApsaraDB for Redis instance is connected through a direct connection.:
    src/redis-cli -h r-bp1zxszhcgatnx****.redis.rds.aliyuncs.com -p 6379 -c
  4. Run the following command to verify the password:
    AUTH <password>
    Note If password-free access is enabled for the instance, you can skip this step if you connect to the ApsaraDB for Redis instance through a VPC.
    <password>: the password of a specific account. The format of the password varies based on the account. If you forget your password, you can reset it. For more information, see Change or reset the password.
    • Default account (the account named after the instance ID): Directly enter the password.
    • New account: The format of the password must be <user>:<password>. For example, if the username of the custom account is testaccount and the password is Rp829dlwa, the database password is testaccount:Rp829dlwa.

    Example:

    AUTH testaccount:Rp829dlwa
    If the password is verified, the following result is returned:
    OK
    Note If an error occurs, see Common connection errors.
  5. After the verification is complete, you can run Redis commands. For example, run the DBSIZE command to query the number of keys in the current database.
    For more information about Redis commands supported by ApsaraDB for Redis, see Overview. For more information about all Redis commands and their usage, see Redis official website.

redis-cli connection method (Windows)

Note Only the 64-bit Windows operating system is supported.
  1. Install redis-cli. Skip this step if it is already installed.
    1. Log on to the device on which you want to install redis-cli, such as an ECS instance or an on-premises host.
    2. Download the Redis-x64-3.2.100.zip file.
      Note Redis-3.2 (the latest version) is used as an example to demonstrate the operation process. You can also install other versions. For more information, see MicrosoftArchive-Redis.
    3. Decompress the Redis-x64-3.2.100.zip file to the directory where you want to install Redis. The D:\Redis-x64-3.2.100 directory is used in this example.
  2. Perform the following operations based on redis-cli installation location:
    redis-cli installation location Operations
    ECS instance (Recommended)
    1. Make sure that the ECS instance and the ApsaraDB for Redis instance are deployed in the same VPC. The ECS instance and ApsaraDB for Redis instance must have the same VPC ID.
      Note If they are deployed in different VPCs, you must change the VPC to which the ECS instance belongs. For more information, see Change the VPC of an ECS instance.
    2. Obtain the internal IP address of an ECS instance. For more information, see How do I query IP addresses of ECS instances.
    3. Add the internal IP address of the ECS instance to the whitelist of the ApsaraDB for Redis instance. For more information, see Set IP address whitelists.
    On-premises host
    1. By default, an ApsaraDB for Redis instance provides only an internal endpoint. You must apply for a public IP address when you want to connect to an ApsaraDB for Redis instance over the Internet. For more information, see Apply for a public endpoint for an ApsaraDB for Redis instance.
    2. On the on-premises host, visitipinfo to obtain the public IP address of the on-premises host.
    3. Add the public IP address of the on-premises host to the whitelist of the ApsaraDB for Redis instance. For more information, see Set IP address whitelists.
  3. On the ECS instance or the on-premises host with redis-cli installed, press Windows key + R key to open the Run dialog box, enter the cmd command, and then click OK.
    The Windows CLI is opened.
    Figure 1. Windows CLI
    Windows CLI
  4. Run the following command to access the directory where redis-cli is installed:
    cd /d <path>

    <path>: the full path of the directory where the Redis-x64-3.2.100.zip file is decompressed. D:\Redis-x64-3.2.100 is used in this example.

    Example:

    cd /d D:\Redis-x64-3.2.100
  5. Obtain the connection information and run the following command to connect to an ApsaraDB for Redis instance.
    redis-cli -h <hostname> -p <port> [-c]
    Table 2. Parameters
    Parameter Description Method
    <hostname> The endpoint of the ApsaraDB for Redis instance.
    • If you connect an ECS instance to the ApsaraDB for Redis instance through a VPC, you must obtain the endpoint of the ApsaraDB for Redis instance in the VPC.
    • If you connect an on-premises host to the ApsaraDB for Redis instance over the Internet, you must obtain the public IP address of the ApsaraDB for Redis instance.

    For more information, see View endpoints.

    <port> The port number of the ApsaraDB for Redis instance. The default port number is 6379. You can also specify a custom port number. For more information, see Modify the port for the endpoint.
    -c Enable the cluster mode Only when you connect to a cluster instance of ApsaraDB for Redis by using a private endpoint, you must add the -c parameter.
    Command example by using the default endpoint (Applicable to scenarios where ApsaraDB for Redis instances are connected by using default endpoints, such as endpoints of standard instances and proxy endpoints of cluster instances):
    redis-cli -h r-bp1zxszhcgatnx****.redis.rds.aliyuncs.com -p 6379
    Command example for private endpoints (Applicable to scenarios where an ApsaraDB for Redis instance is connected through a direct connection.:
    redis-cli -h r-bp1zxszhcgatnx****.redis.rds.aliyuncs.com -p 6379 -c
  6. Run the following command to verify the password:
    AUTH <password>
    Note If password-free access is enabled for the instance, you can skip this step if you connect to the ApsaraDB for Redis instance through a VPC.
    <password>: the password of a specific account. The format of the password varies based on the account. If you forget your password, you can reset it. For more information, see Change or reset the password.
    • Default account (the account named after the instance ID): Directly enter the password.
    • New account: The format of the password must be <user>:<password>. For example, if the username of the custom account is testaccount and the password is Rp829dlwa, the database password is testaccount:Rp829dlwa.

    Example:

    AUTH testaccount:Rp829dlwa
    If the password is verified, the following result is returned:
    OK
    Note If an error occurs, see #d6e500.
  7. After the verification is complete, you can run Redis commands. For example, run the DBSIZE command to query the number of keys in the current database.
    For more information about Redis commands supported by ApsaraDB for Redis, see Overview. For more information about all Redis commands and their usage, see Redis official website.

Common connection errors

Note If the minor version of your instance is earlier than required, the returned error message may be misleading. We recommend that you upgrade your instance to the latest minor version. For more information, see Upgrade the minor version.
Error message Cause and solution
(error) ERR illegal address The whitelist setting is not configured as required. For more information, see Step 2 in redis-cli connection method (Linux) or redis-cli connection method (Windows).
(error) ERR client ip is not in whitelist
(error) ERR invalid password The password is invalid. Make sure that you use the correct password in valid format. The password format varies based on the selected account.
  • Default account (the account named after the instance ID): Directly enter the password.
  • New account: The format of the password must be <user>:<password>. For example, if the username of a custom account is testaccount and the password is Rp829dlwa, the password is testaccount:Rp829dlwa.
Note If you forget your password, you can reset it. For more information, see Change or reset the password.