获取 Azure Redis 缓存的主机名、端口和密钥Get the hostname, ports, and keys for Azure Cache for Redis

在此方案中,将学习如何检索用于连接到 Azure Redis 缓存实例的主机名、端口和密钥。In this scenario, you learn how to retrieve the hostname, ports, and keys used to connect to an Azure Cache for Redis instance.

若要运行此示例,请确保已安装最新版本的 Azure CLITo run this sample, make sure you have installed the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

此示例在 Bash shell 中正常工作。This sample works in a Bash shell. 有关在 Windows 客户端上运行 Azure CLI 脚本的选项,请参阅在 Windows 上安装 Azure CLIFor options on running Azure CLI scripts on Windows client, see Install the Azure CLI on Windows.

示例脚本Sample script

#/bin/bash

# Retrieve the hostname, ports, and keys for contosoCache located in contosoGroup

# Retrieve the hostname and ports for an Azure Redis Cache instance
redis=($(az redis show --name contosoCache --resource-group contosoGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))

# Retrieve the keys for an Azure Redis Cache instance
keys=($(az redis list-keys --name contosoCache --resource-group contosoGroup --query [primaryKey,secondaryKey] --output tsv))

# Display the retrieved hostname, keys, and ports
echo "Hostname:" ${redis[0]}
echo "Non SSL Port:" ${redis[2]}
echo "Non SSL Port Enabled:" ${redis[1]}
echo "SSL Port:" ${redis[3]}
echo "Primary Key:" ${keys[0]}
echo "Secondary Key:" ${keys[1]}

脚本说明Script explanation

此脚本使用以下命令检索 Azure Redis 缓存实例的主机名、密钥和端口。This script uses the following commands to retrieve the hostname, keys, and ports of an Azure Cache for Redis instance. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az redis showaz redis show 检索 Azure Redis 缓存实例的详细信息。Retrieve details of an Azure Cache for Redis instance.
az redis list-keysaz redis list-keys 检索 Azure Redis 缓存实例的访问密钥。Retrieve access keys for an Azure Cache for Redis instance.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Redis 缓存文档中找到其他 Azure Redis 缓存 CLI 脚本示例。Additional Azure Cache for Redis CLI script samples can be found in the Azure Cache for Redis documentation.