【Azure Redis 緩存】Azure Redis 服務不支持指令CONFIG
- 2020 年 12 月 16 日
- 筆記
- 【Azure Redis 緩存】, Azure Redis, Azure Redis不支持的命令, Redis逐出策略, Set-AzRedisCache
問題描述
在Azure Redis的門戶頁面中,通過Redis Console連接到Redis後,想通過CONFIG命令來配置Redis,但是系統提示CONFIG命令不能用。 錯誤消息為:(error) ERR unknown command `config`。
根本原因
因為 Azure Redis 緩存實例的配置和管理由 微軟進行管理,所以禁用了以下命令。 如果嘗試調用它們,將收到一條類似於 "(error) ERR unknown command"
的錯誤消息。
- BGREWRITEAOF
- BGSAVE
- CONFIG
- DEBUG
- MIGRATE
- SAVE
- SHUTDOWN
- SLAVEOF
- CLUSTER – 群集寫命令已禁用,但允許使用只讀群集命令。
更詳細的說明見官方文檔說明:Azure Redis 緩存中不支持 Redis 命令
解決方案
方式一:在門戶上配置Redis的參數。如SSL, Maxmemory-Policy, Maxmemory-reserved, Maxfragmentationmemory-reserved.
- SSL:默認情況下,Azure為新緩存禁用非 TLS/SSL 訪問。 要啟用非 TLS 端口,需在如下截圖頁面中修改。
- Maxmemory policy 緩存逐出策略, 默認值為volatile-lru, 即在配置過過期時間的Key中移除最近不使用的Key以騰出空間存儲新值。其他的逐出策略見本文附錄一
- Maxmemory-reserved:設置用於配置群集中保留給非緩存操作(例如故障轉移期間的複製)的每個實例的內存量(以 MB 為單位)
- Maxfragmentationmemory-reserved:設置用於配置群集中保留以容納內存碎片的每個實例的內存量(以 MB 為單位)
而如果需要設置其他的配置,則只能通過 方式二PowerShell命令 來設置。
方式二:通過PowerShell命令(Set-AzRedisCache)來修改Redis的配置。Set-AzRedisCache命令中,最主要的配置包含在RedisConfiguration參數中。
Set-AzRedisCache:
PS C:\> Get-Help Set-AzRedisCache -detailed NAME Set-AzRedisCache SYNOPSIS Set Azure Cache for Redis updatable parameters. SYNTAX Set-AzRedisCache -Name <String> -ResourceGroupName <String> [-Size <String>] [-Sku <String>] [-MaxMemoryPolicy <String>] [-RedisConfiguration <Hashtable>] [-EnableNonSslPort <Boolean>] [-ShardCount <Integer>] [<CommonParameters>] DESCRIPTION The Set-AzRedisCache cmdlet sets Azure Cache for Redis parameters. PARAMETERS -Name <String> Name of the Azure Cache for Redis to update. -ResourceGroupName <String> Name of the resource group for the cache. -Size <String> Size of the Azure Cache for Redis. The default value is 1GB or C1. Possible values are P1, P2, P3, P4, C0, C1, C2, C3, C4, C5, C6, 250MB, 1GB, 2.5GB, 6GB, 13GB, 26GB, 53GB. -Sku <String> Sku of Azure Cache for Redis. The default value is Standard. Possible values are Basic, Standard and Premium. -MaxMemoryPolicy <String> The 'MaxMemoryPolicy' setting has been deprecated. Please use 'RedisConfiguration' setting to set MaxMemoryPolicy. e.g. -RedisConfiguration @{"maxmemory-policy" = "allkeys-lru"} -RedisConfiguration <Hashtable> All Redis Configuration Settings. Few possible keys: rdb-backup-enabled, rdb-storage-connection-string, rdb-backup-frequency, maxmemory-reserved, maxmemory-policy, notify-keyspace-events, hash-max-ziplist-entries, hash-max-ziplist-value, set-max-intset-entries, zset-max-ziplist-entries, zset-max-ziplist-value. -EnableNonSslPort <Boolean> EnableNonSslPort is used by Azure Cache for Redis. The default value is null and no change will be made to the currently configured value. Possible values are true and false. -ShardCount <Integer> The number of shards to create on a Premium Cluster Cache. <CommonParameters> This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. For more information, see about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216).
Set-AzRedisCache 示例:(示例引用來自:如何設置讓Azure Redis中的RDB文件暫留更久(如7天))
$RedisConfiguration = @{"rdb-backup-enabled"="true"; "rdb-backup-frequency"="60"; "rdb-storage-connection-string"="$StorageConnectionString"; "rdb-backup-max-days"="7"} Set-AzRedisCache -ResourceGroupName $ResourceGroupName -Name $CacheName -RedisConfiguration $RedisConfiguration
註:配置完成後,可以使用 Get-AzRedisCache cmdlet 檢索有關緩存的信息。
附錄一:Redis逐出策略 [less recently used (LRU),Least Frequently Used(LFU) ]
-
volatile-lru :通過嘗試先刪除最近不使用的(LRU)密鑰來移出密鑰,但只有在已設置過期的密鑰中才能移出這些密鑰,以便為添加的新數據騰出空間。
-
allkeys-lru:通過嘗試先刪除最近不使用的(LRU)鍵來移出鍵,以便為添加的新數據騰出空間。
-
volatile-random:為添加新的鍵騰出空間,隨機逐出其他鍵,但是僅逐出設置了過期時間的鍵。
-
allkeys-random:隨機逐出其他鍵,以便為添加新的鍵騰出空間。
-
volatile-ttl:逐出設置過期的密鑰,並嘗試首先逐出具有較短生存時間(TTL)的密鑰,以便為添加新的鍵騰出空間。
-
noeviction:當達到內存限制並且客戶端嘗試執行可能導致使用更多內存的命令時,將返回錯誤(大多數寫入命令,但DEL和一些其他例外)。
-
volatile-lfu:使用設置過期並且使用頻率最少的鍵(LFU)中進行驅逐。
-
allkeys-lfu:使用頻率最少的鍵(LFU)逐出任何密鑰。
參考資料
如何設置讓Azure Redis中的RDB文件暫留更久(如7天): //www.cnblogs.com/lulight/p/13842979.html
Azure Redis 緩存中不支持 Redis 命令: //docs.azure.cn/zh-cn/azure-cache-for-redis/cache-configure#redis-commands-not-supported-in-azure-cache-for-redis
Redis Eviction policies: //redis.io/topics/lru-cache#eviction-policies
更新 Azure Redis 緩存: //docs.azure.cn/zh-cn/azure-cache-for-redis/cache-how-to-manage-redis-cache-powershell#to-update-an-azure-cache-for-redis