如何设置Redis最大内存?

如何设置Redis最大内存?,redis,Redis,我在中找到configure,它只是说使用指定configure的命令: ./redis-server <path>/redis.conf 顺便问一下,我想知道默认内存是多少。我想把内存设置为2GB,怎么做 然后,我将这一行添加到redis配置中,以将maxmemory设置为40GB: maxmemory 41943040 我使用redis cli中的命令: config get maxmemory 它告诉我: 127.0.0.1:6379> config get max

我在中找到configure,它只是说使用指定configure的命令:

./redis-server <path>/redis.conf
顺便问一下,我想知道默认内存是多少。我想把内存设置为2GB,怎么做

然后,我将这一行添加到redis配置中,以将maxmemory设置为40GB:

maxmemory 41943040
我使用redis cli中的命令:

config get maxmemory
它告诉我:

127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "41943040"
但是,我的java程序在键值约为200000时抛出如下异常:

Exception in thread "Thread-228" redis.clients.jedis.exceptions.JedisDataException: OOM command not allowed when used memory > 'maxmemory'.
    at redis.clients.jedis.Protocol.processError(Protocol.java:117)
    at redis.clients.jedis.Protocol.process(Protocol.java:151)
    at redis.clients.jedis.Protocol.read(Protocol.java:205)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:196)
    at redis.clients.jedis.Jedis.hmset(Jedis.java:644)
    at cn.ict.dt2redis.analyser.AbstractAnalyser.pushOne(AbstractAnalyser.java:21)
    at cn.ict.dt2redis.analyser.BatchAbstractAnalyser.run(BatchAbstractAnalyser.java:16)
    at java.lang.Thread.run(Thread.java:722)

我对此一无所知,我是否成功地将最大内存设置为40GB?怎么做?请给我一些详细的代码。

是-要设置内存限制,只需取消注释.conf文件中的
maxmemory
行即可。默认值为0,这意味着无限(直到操作系统耗尽RAM并终止进程-我建议始终将maxmemory设置为sane值)

更新:正如@Eric Uldall在评论中提到的,一个
配置集maxmemory
,然后是一个
配置重写
,应该也能做到这一点。这将修改redis.conf,以便在重新启动时保留更改

maxmemory 41943040


选项以字节为单位设置,因此您可以在注释中设置40MB的文档调出字节,但我使用了诸如mb和gb之类的扩展,没有任何问题

$ grep ^maxmemory /etc/redis-server.conf
maxmemory 8gb
maxmemory-policy allkeys-lru
并确认:

$ redis-cli
...
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "8589934592"

如果任何人在本地环境中设置
maxmemory
config设置时仍有问题,实际步骤如下:

  • 在终端中,启动redis实例
    redis服务器
  • 在新的终端窗口中运行redis cli
  • 在新终端窗口(步骤2)中运行
    config set maxmemory 2mb
  • 通过在与步骤2/3相同的终端窗口中运行
    config get maxmemory
    验证
    maxmemory

  • 由于这是一个老问题,2019年阅读并使用Ubuntu18.04的用户,配置文件位于
    /etc/Redis/Redis.conf
    中,如果您使用(默认/推荐方法)进行安装,则在服务器运行时更改Redis配置一节中有所记录
    apt install redis server
    默认内存限制设置为“0”,这实际上意味着没有“限制”,如果用户的RAM/内存有限/很少,这可能会带来麻烦。要设置自定义内存限制,您只需编辑配置文件并键入“maxmemory 1gb”作为第一行。重新启动redis服务以使更改生效。要验证更改,请使用redis cli config get maxmemory

    Ubuntu 18.04用户可以在这里阅读更多内容:

    不需要更改.conf文件中的任何内容,只需执行以下步骤

    步骤1:检查redis服务器是否正常工作

    $ redis-cli
    127.0.0.1:6379> ping
    PONG
    
    如果回复是
    PONG
    ,那么您的服务器工作绝对正常

    步骤2:要获取当前最大内存,请运行以下命令-

    $ redis-cli
    127.0.0.1:6379> config get maxmemory
    1) "maxmemory"
    2) "0"
    
    最初,它默认设置为0

    步骤3:运行上述步骤后,运行以下命令以设置maxmemory

    127.0.0.1:6379> config set maxmemory 128M
    OK
    
    要检查maxmemory是否设置为128M,请再次运行
    步骤2

    步骤4:更改maxmemory后,重新启动redis服务器

    $ sudo systemctl restart redis
    OR
    $ sudo systemctl restart redis-server
    

    我添加了行“maxmemory 41943040”来设置最大内存40GB,但它会抛出异常“OOM command not allowed when used memory>“maxmemory”。“我明白了,您已经将maxmemory设置为41943040字节=40960KB=40MBOh。我已改为“maxmemory 40gb”进行测试。谢谢这也值得注意。如果在
    config set[key][value]
    之后运行
    config rewrite
    ,它会将更改提交到redis.conf,以便在重新启动时保留更改。@EricUldall very true:)
    $ sudo systemctl restart redis
    OR
    $ sudo systemctl restart redis-server