1. 程式人生 > 實用技巧 >redis的配置簡介

redis的配置簡介

Redis的配置檔案

redis啟動的時候依賴的配置檔案

  • 配置檔案對單位的大小寫不敏感

    # Redis configuration file example.
    #
    # Note that in order to read the configuration file, Redis must be
    # started with the file path as first argument:
    #
    # ./redis-server /path/to/redis.conf
    
    # Note on units: when memory size is needed, it is possible to specify
    # it in the usual form of 1k 5GB 4M and so forth:
    #
    # 1k => 1000 bytes
    # 1kb => 1024 bytes
    # 1m => 1000000 bytes
    # 1mb => 1024*1024 bytes
    # 1g => 1000000000 bytes
    # 1gb => 1024*1024*1024 bytes
    
  • 可以使用include來包含別的檔案

    # Include one or more other config files here.  This is useful if you
    # have a standard template that goes to all Redis servers but also need
    # to customize a few per-server settings.  Include files can include
    # other files, so use this wisely.
    #
    # Notice option "include" won't be rewritten by command "CONFIG REWRITE"
    # from admin or Redis Sentinel. Since Redis always uses the last processed
    # line as value of a configuration directive, you'd better put includes
    # at the beginning of this file to avoid overwriting config change at runtime.
    #
    # If instead you are interested in using includes to override configuration
    # options, it is better to use include as the last line.
    #
    # include /path/to/local.conf
    # include /path/to/other.conf
    
  • 網路模組

    bind:可以使用萬用字元或者自己的公網ip來使我們的redis服務能夠公開

    protected-mode:是否開始保護模式

    port:繫結的埠

  • 通用的配置

    daemobize:是否開啟守護程序,預設是no

    pidfile:如果是守護程序的方式執行,則需要制定這個配置檔案

    loglevel:日誌級別

    # Specify the server verbosity level.
    # This can be one of:
    # debug (a lot of information, useful for development/testing)
    # verbose (many rarely useful info, but not a mess like the debug level)
    # notice (moderately verbose, what you want in production probably)
    # warning (only very important / critical messages are logged)
    loglevel notice
    

    logfile:日誌的檔名

    databases:資料庫的數量,預設是16個數據庫

    always-show-logo:是否顯示開啟時候的logo

  • 快照

    持久化,在規定的時間內執行了多少次的操作,則會生快照

    一般會有rdbaof兩個檔案

    • 預設的規則

      # Save the DB on disk:
      #
      #   save <seconds> <changes>
      #
      #   Will save the DB if both the given number of seconds and the given
      #   number of write operations against the DB occurred.
      #
      #   In the example below the behaviour will be to save:
      #   after 900 sec (15 min) if at least 1 key changed
      #   after 300 sec (5 min) if at least 10 keys changed
      #   after 60 sec if at least 10000 keys changed
      #
      #   Note: you can disable saving completely by commenting out all "save" lines.
      #
      #   It is also possible to remove all the previously configured save
      #   points by adding a save directive with a single empty string argument
      #   like in the following example:
      #
      #   save ""
      
      # 如果900s內,有一次key進行了修改,那麼就進行持久化操作
      save 900 1 
      # 如果300s內,有超過10個key進行了修改,那麼就進行持久化操作
      save 300 10
      # 如果60s內,有超過10000個key進行了修改,那麼就進行持久化操作
      save 60 10000
      
    • stop-writes-on-bgsave-error:持久化出現錯誤後是否繼續工作

    • rdbcompression:是否壓縮rdb檔案

    • rdbchecksum:儲存rdb檔案的時候進行校驗

    • dir:rdb檔案儲存的目錄

  • 安全

    requirepass:設定登入密碼

  • 客戶端

    maxclients:最大客戶端數

    maxmemory <bytes>:redis最大記憶體設定

    maxmemory-policy:記憶體達到上限後的處理策略

    # volatile-lru -> remove the key with an expire set using an LRU algorithm
    # allkeys-lru -> remove any key according to the LRU algorithm
    # volatile-random -> remove a random key with an expire set
    # allkeys-random -> remove a random key, any key
    # volatile-ttl -> remove the key with the nearest expire time (minor TTL)
    # noeviction -> don't expire at all, just return an error on write operations
    #
    # Note: with any of the above policies, Redis will return an error on write
    #       operations, when there are no suitable keys for eviction.
    #
    #       At the date of writing these commands are: set setnx setex append
    #       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
    #       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
    #       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
    #       getset mset msetnx exec sort
    #
    # The default is:
    #
    # maxmemory-policy noeviction
    
  • APPEND ONLY模式,aof配置

    appendonly:是否開啟aof

    appendfilename:aof的檔名

    # appendfsync always ## 每次修改都執行一次
    appendfsync everysec  ## 每秒都執行一次,可能會丟失1s的資料
    # appendfsync no ## 不執行不同步