redis-cli

运行 Redis 命令。

Redis 企业软件 Redis 云

redis-cli命令行实用程序允许您与 Redis 数据库进行交互。跟redis-cli中,您可以直接从命令行终端运行Redis命令,也可以使用交互模式运行Redis命令

如果您想在没有redis-cli,您可以使用 Redis Insight 连接到数据库,并改用内置的 CLI 提示符。

安装redis-cli

安装 Redis Enterprise Software 或 Redis Community Edition 时,它还会安装redis-cli命令行实用程序。

要了解如何安装 Redis 和redis-cli,请参阅以下安装指南:

连接到数据库

要使用redis-cli,您需要连接到 Redis 数据库。

您可以在 Databases (数据库) 列表或数据库的 Configuration (配置) 屏幕中找到终端节点和端口详细信息。

远程连接

如果你有redis-cli安装在本地计算机上,您可以使用它来连接到远程 Redis 数据库。您需要提供数据库的连接详细信息,例如主机名或 IP 地址、端口和密码。

$ redis-cli -h <endpoint> -p <port> -a <password>

You can also provide the password with the REDISCLI_AUTH environment variable instead of the -a option:

$ export REDISCLI_AUTH=<password>
$ redis-cli -h <endpoint> -p <port>

Connect over TLS

To connect to a Redis Enterprise Software or Redis Cloud database over TLS:

  1. Download or copy the Redis Enterprise server (or proxy) certificates.

    • For Redis Cloud, see Download certificates for detailed instructions on how to download the server certificates (redis_ca.pem) from the Redis Cloud console.

    • For Redis Enterprise Software, copy the proxy certificate from the Cluster Manager UI (Cluster > Security > Certificates > Server authentication) or from a cluster node (/etc/opt/redislabs/proxy_cert.pem).

  2. Copy the certificate to each client machine.

  3. If your database doesn't require client authentication, provide the Redis Enterprise server certificate (redis_ca.pem for Cloud or proxy_cert.pem for Software) when you connect:

    redis-cli -h <endpoint> -p <port> --tls --cacert <redis_cert>.pem
    
  4. If your database requires client authentication, provide your client's private and public keys along with the Redis Enterprise server certificate (redis_ca.pem for Cloud or proxy_cert.pem for Software) when you connect:

    redis-cli -h <endpoint> -p <port> --tls --cacert <redis_cert>.pem \
        --cert redis_user.crt --key redis_user_private.key
    

Connect with Docker

If your Redis database runs in a Docker container, you can use docker exec to run redis-cli commands:

$ docker exec -it <Redis container name> redis-cli -p <port>

Basic use

You can run redis-cli commands directly from the command-line terminal:

$ redis-cli -h <endpoint> -p <port> <Redis command>

For example, you can use redis-cli to test your database connection and store a new Redis string in the database:

$ redis-cli -h <endpoint> -p 12000 PING
PONG
$ redis-cli -h <endpoint> -p 12000 SET mykey "Hello world"
OK
$ redis-cli -h <endpoint> -p 12000 GET mykey              
"Hello world"

For more information, see Command line usage.

Interactive mode

In redis-cli interactive mode, you can:

  • Run any redis-cli command without prefacing it with redis-cli.
  • Enter ? for more information about how to use the HELP command and set redis-cli preferences.
  • Enter HELP followed by the name of a command for more information about the command and its options.
  • Press the Tab key for command completion.
  • Enter exit or quit or press Control+D to exit interactive mode and return to the terminal prompt.

This example shows how to start interactive mode and run Redis commands:

$ redis-cli -p 12000
127.0.0.1:12000> PING
PONG
127.0.0.1:12000> SET mykey "Hello world"
OK
127.0.0.1:12000> GET mykey
"Hello world"

Examples

Check slowlog

Run slowlog get for a list of recent slow commands:

redis-cli -h <endpoint> -p <port> slowlog get <number of entries>

Scan for big keys

Scan the database for big keys:

redis-cli -h <endpoint> -p <port> --bigkeys

See Scanning for big keys for more information.

More info

RATE THIS PAGE
Back to top ↑