This topic describes how to use the Redis command-line tool (redis-cli) to connect to an ApsaraDB for Redis instance.

Introduction to redis-cli

Redis-cli is a built-in command-line tool of Redis. You can use redis-cli to connect to an ApsaraDB for Redis instance and manage data.

Redis-cli allows you to connect to an ApsaraDB for Redis instance from the Linux system deployed on an ECS instance, or access an ApsaraDB for Redis instance from a local host over the Internet. ApsaraDB for Redis allows you to access an instance over a private network to ensure high security and performance. If you want to connect an ECS instance to an ApsaraDB for Redis instance deployed in the same VPC network or to an ApsaraDB for Redis instance deployed in the same region of a classic network, you can use redis-cli on the ECS instance. You can also connect to an ApsaraDB for Redis instance from your on-premises host over the Internet. To do so, follow the instructions in Through the Internet to apply for a public endpoint. Then, follow the instructions in the Connect to an ApsaraDB for Redis instance section of this topic to connect to the instance.

Install redis-cli

Install a Linux-based version of Redis to use redis-cli. For more information, visit the Redis official website.

Prerequisites

Connections over an internal network

  • If the network type for both the ECS instance and the ApsaraDB for Redis instance is VPC, these two instances must be deployed in the same VPC network of a region.
  • If the network type for both the ECS instance and the ApsaraDB for Redis instance is classic network, these two instances must be deployed in the same region.
  • You have added the private IP address of the ECS instance to the whitelist of the ApsaraDB for Redis instance.
  • You have installed the Linux-based version of Redis on the ECS instance.
  • If you use a custom account to connect to the ApsaraDB for Redis instance, the format of the connection string must be <user>:<password>. For example, if the username of the custom account is admin and the password is Rp829dlwa, you can use admin:Rp829dlwa to log on to the instance.

Connections over the Internet

  • You have applied for a public endpoint for the ApsaraDB for Redis instance. For more information, see Through the Internet.
  • You have added the public IP address of the local host to the whitelist of the ApsaraDB for Redis instance.
  • The operating system of the local host must be Linux.
  • You have installed the Linux-based version of Redis on the local host.
  • If you use a custom account to connect to the ApsaraDB for Redis instance, the format of the connection string must be <user>:<password>. For example, if the username of the custom account is admin and the password is Rp829dlwa, you can use admin:Rp829dlwa to log on to the instance.

Notes

  • If you access an ApsaraDB for Redis instance by using an internal endpoint and have enabled the VPC password-free access feature, no password is required. For more information, see VPC password-free access.
  • If you access an ApsaraDB for Redis instance by using a public endpoint and have enabled the VPC password-free access feature, a password is required.
  • For more information, see Troubleshooting for connection issues in ApsaraDB for Redis.

Connect to an ApsaraDB for Redis instance

Run the following command in redis-cli to connect to an ApsaraDB for Redis instance.

redis-cli -h <host> -p <port> -a '<password>'
Note To use a direct connection to access an ApsaraDB for Redis instance, you must specify the -c parameter, as shown in the following example.
redis-cli -h <host> -p <port> -a '<password>' -c
Table 1. Parameters
Parameter Description
-h

Specifies the endpoint of the ApsaraDB for Redis instance.

  • Private network connections: Access an ApsaraDB for Redis instance over a private network by using an internal endpoint. For more information, see View endpoints.
  • Public network connections: Access an ApsaraDB for Redis instance over the Internet by using a public endpoint. For more information, see Through the Internet.
-p

Specifies the service port of an ApsaraDB for Redis instance. The default port number is 6379. You can change the port number. For more information, see Modify the port for the endpoint.

-a The password used to connect to the ApsaraDB for Redis instance. Enclose the password with single quotation marks ('). For example, 'Rp829dlwa'.
Note You can skip this parameter to avoid revealing the password in plaintext. After you run the preceding command, you can enter auth <password> for authentication, as shown in the following figure.
-c This parameter is required when you use a direct connection to access an ApsaraDB for Redis cluster instance.
Figure 1. Example