【Azure Redis 缓存】Azure Redis 服务不支持指令CONFIG

问题描述

在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. 

  1. SSL:默认情况下,Azure为新缓存禁用非 TLS/SSL 访问。 要启用非 TLS 端口,需在如下截图页面中修改。
  2. Maxmemory policy 缓存逐出策略, 默认值为volatile-lru, 即在配置过过期时间的Key中移除最近不使用的Key以腾出空间存储新值。其他的逐出策略见本文附录一
  3. Maxmemory-reserved:设置用于配置群集中保留给非缓存操作(例如故障转移期间的复制)的每个实例的内存量(以 MB 为单位)
  4. 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天): https://www.cnblogs.com/lulight/p/13842979.html
Azure Redis 缓存中不支持 Redis 命令: https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-configure#redis-commands-not-supported-in-azure-cache-for-redis
Redis Eviction policies: https://redis.io/topics/lru-cache#eviction-policies
更新 Azure Redis 缓存: https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-how-to-manage-redis-cache-powershell#to-update-an-azure-cache-for-redis

原文地址:https://www.cnblogs.com/lulight/p/14143448.html