Redis-15Redis基础配置文件
文章目錄
- 概述
- Redis CONFIG 命令
- CONFIG GET
- CONFIG SET
- Redis redis.conf文件
- 部分配置項說明
- daemonize
- pidfile
- port
- bind
- timeout
- loglevel
- logfile
- database
- save
- rdbcompression
- dbfilename
- dir
- slaveof
- masterauth
- requirepass
- maxclients
- maxmemory
- appendonly
- appendfilename
- appendfsync
- include
- protected-mode
- maxmemory-policy
概述
Redis 的配置文件放置在其安裝目錄下,如果是 Windows 系統,則默認的配置文件就是 redis .window.conf 如果是 Linux 系統,則是 redis.conf。在大部分的情況下我們都使用到Linux 環境,這里我們以linux環境為例。
Redis CONFIG 命令
可以通過 CONFIG 命令查看或設置配置項,臨時生效。重啟后失效。
CONFIG GET
redis 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME比如
127.0.0.1:6379> CONFIG GET requirepass 1) "requirepass" 2) "artisan" 127.0.0.1:6379> CONFIG GET bind 1) "bind" 2) "" 127.0.0.1:6379> CONFIG GET loglevel 1) "loglevel" 2) "notice" 127.0.0.1:6379>使用 * 號獲取所有配置項
127.0.0.1:6379> CONFIG GET *1) "dbfilename"2) "dump.rdb"3) "requirepass"4) "artisan"5) "masterauth"6) ""7) "cluster-announce-ip"8) ""9) "unixsocket"10) ""11) "logfile"12) ""13) "pidfile"14) "/var/run/redis_6379.pid"15) "slave-announce-ip"16) ""17) "maxmemory"18) "0"19) "proto-max-bulk-len"20) "536870912"21) "client-query-buffer-limit"22) "1073741824"23) "maxmemory-samples"24) "5"25) "lfu-log-factor"26) "10"27) "lfu-decay-time"28) "1"29) "timeout"30) "0"31) "active-defrag-threshold-lower"32) "10"33) "active-defrag-threshold-upper"34) "100"35) "active-defrag-ignore-bytes"36) "104857600"37) "active-defrag-cycle-min"38) "25"39) "active-defrag-cycle-max"40) "75"41) "auto-aof-rewrite-percentage"42) "100"43) "auto-aof-rewrite-min-size"44) "67108864"45) "hash-max-ziplist-entries"46) "512"47) "hash-max-ziplist-value"48) "64"49) "list-max-ziplist-size"50) "-2"51) "list-compress-depth"52) "0"53) "set-max-intset-entries"54) "512"55) "zset-max-ziplist-entries"56) "128"57) "zset-max-ziplist-value"58) "64"59) "hll-sparse-max-bytes"60) "3000"61) "lua-time-limit"62) "5000"63) "slowlog-log-slower-than"64) "10000"65) "latency-monitor-threshold"66) "0"67) "slowlog-max-len"68) "128"69) "port"70) "6379"71) "cluster-announce-port"72) "0"73) "cluster-announce-bus-port"74) "0"75) "tcp-backlog"76) "511"77) "databases"78) "16"79) "repl-ping-slave-period"80) "10"81) "repl-timeout"82) "60"83) "repl-backlog-size"84) "1048576"85) "repl-backlog-ttl"86) "3600"87) "maxclients"88) "4064"89) "watchdog-period"90) "0"91) "slave-priority"92) "100"93) "slave-announce-port"94) "0"95) "min-slaves-to-write"96) "0"97) "min-slaves-max-lag"98) "10"99) "hz" 100) "10" 101) "cluster-node-timeout" 102) "15000" 103) "cluster-migration-barrier" 104) "1" 105) "cluster-slave-validity-factor" 106) "10" 107) "repl-diskless-sync-delay" 108) "5" 109) "tcp-keepalive" 110) "300" 111) "cluster-require-full-coverage" 112) "yes" 113) "cluster-slave-no-failover" 114) "no" 115) "no-appendfsync-on-rewrite" 116) "no" 117) "slave-serve-stale-data" 118) "yes" 119) "slave-read-only" 120) "yes" 121) "stop-writes-on-bgsave-error" 122) "yes" 123) "daemonize" 124) "yes" 125) "rdbcompression" 126) "yes" 127) "rdbchecksum" 128) "yes" 129) "activerehashing" 130) "yes" 131) "activedefrag" 132) "no" 133) "protected-mode" 134) "yes" 135) "repl-disable-tcp-nodelay" 136) "no" 137) "repl-diskless-sync" 138) "no" 139) "aof-rewrite-incremental-fsync" 140) "yes" 141) "aof-load-truncated" 142) "yes" 143) "aof-use-rdb-preamble" 144) "no" 145) "lazyfree-lazy-eviction" 146) "no" 147) "lazyfree-lazy-expire" 148) "no" 149) "lazyfree-lazy-server-del" 150) "no" 151) "slave-lazy-flush" 152) "no" 153) "maxmemory-policy" 154) "noeviction" 155) "loglevel" 156) "notice" 157) "supervised" 158) "no" 159) "appendfsync" 160) "everysec" 161) "syslog-facility" 162) "local0" 163) "appendonly" 164) "no" 165) "dir" 166) "/home/redis/redis" 167) "save" 168) "900 1 300 10 60 10000" 169) "client-output-buffer-limit" 170) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60" 171) "unixsocketperm" 172) "0" 173) "slaveof" 174) "" 175) "notify-keyspace-events" 176) "" 177) "bind" 178) "" 127.0.0.1:6379>CONFIG SET
可以通過修改 redis.conf 文件或使用 CONFIG set 命令來修改配置。
語法
redis 127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE舉例
127.0.0.1:6379> CONFIG SET loglevel "notice" OK 127.0.0.1:6379>Redis redis.conf文件
Redis-02Redis在linux下的安裝及常見問題
部分配置項說明
daemonize
# By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.Redis默認不是以守護進程的方式運行,可以通過該配置項修改,使用yes啟用守護進程
pidfile
# If a pid file is specified, Redis writes it where specified at startup # and removes it at exit. # # When the server runs non daemonized, no pid file is created if none is # specified in the configuration. When the server is daemonized, the pid file # is used even if not specified, defaulting to "/var/run/redis.pid". # # Creating a pid file is best effort: if Redis is not able to create it # nothing bad happens, the server will start and run normally. pidfile /var/run/redis_6379.pid當Redis以守護進程方式運行,如果沒有創建成功,看看是不是在/var/run沒有創建文件的權限 . 前提 daemonize yes
[redis@artisan bin]$ ./redis-cli 127.0.0.1:6379> auth artisan OK 127.0.0.1:6379> CONFIG GET pidfile 1) "pidfile" 2) "/var/run/redis_6379.pid" 127.0.0.1:6379>port
# Accept connections on the specified port, default is 6379 (IANA #815344). # If port 0 is specified Redis will not listen on a TCP socket. port 6379指定redis監聽端口
bind
# By default, if no "bind" configuration directive is specified, Redis listens # for connections from all the network interfaces available on the server. # It is possible to listen to just one or multiple selected interfaces using # the "bind" configuration directive, followed by one or more IP addresses. # # Examples: # # bind 192.168.1.100 10.0.0.1 # bind 127.0.0.1 ::1 # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the # instance to everybody on the internet. So by default we uncomment the # following bind directive, that will force Redis to listen only into # the IPv4 lookback interface address (this means Redis will be able to # accept connections only from clients running into the same computer it # is running). # # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #bind 127.0.0.1綁定的主機地址
timeout
# Close the connection after a client is idle for N seconds (0 to disable) timeout 0當 客戶端閑置多長時間后關閉連接,如果指定為0,表示關閉該功能
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指定日志記錄級別,Redis總共支持四個級別:debug、verbose、notice、warning,默認為verbose
logfile
# Specify the log file name. Also the empty string can be used to force # Redis to log on the standard output. Note that if you use standard # output for logging but daemonize, logs will be sent to /dev/null logfile ""指定日志文件位置.,默認為標準輸出,如果配置Redis為守護進程方式運行,而這里又配置為日志記錄方式為標準輸出,則日志將會發送給/dev/null
database
# Set the number of databases. The default database is DB 0, you can select # a different one on a per-connection basis using SELECT <dbid> where # dbid is a number between 0 and 'databases'-1 databases 16設置數據庫的數量,默認數據庫為0,可以使用SELECT <dbid>命令在連接上指定數據庫id
save
################################ SNAPSHOTTING ################################ # # 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 ""save 900 1 save 300 10 save 60 10000指定在多長時間內,有多少次更新操作,就將數據同步到數據文件,可以多個條件配合.
Redis默認配置文件中提供了三個條件:
save 900 1save 300 10save 60 10000分別表示900秒(15分鐘)內有1個更改,300秒(5分鐘)內有10個更改以及60秒內有10000個更改。
rdbcompression
# Compress string objects using LZF when dump .rdb databases? # For default that's set to 'yes' as it's almost always a win. # If you want to save some CPU in the saving child set it to 'no' but # the dataset will likely be bigger if you have compressible values or keys. rdbcompression yes指定存儲至本地數據庫時是否壓縮數據,默認為yes,Redis采用LZF壓縮,如果為了節省CPU時間,可以關閉該選項,但會導致數據庫文件變的巨大
dbfilename
# The filename where to dump the DB dbfilename dump.rd指定本地數據庫文件名,默認值為dump.rdb
dir
# The working directory. # # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. dir .指定本地數據庫存放目錄
slaveof
# Master-Slave replication. Use slaveof to make a Redis instance a copy of # another Redis server. A few things to understand ASAP about Redis replication. # # 1) Redis replication is asynchronous, but you can configure a master to # stop accepting writes if it appears to be not connected with at least # a given number of slaves. # 2) Redis slaves are able to perform a partial resynchronization with the # master if the replication link is lost for a relatively small amount of # time. You may want to configure the replication backlog size (see the next # sections of this file) with a sensible value depending on your needs. # 3) Replication is automatic and does not need user intervention. After a # network partition slaves automatically try to reconnect to masters # and resynchronize with them. # # slaveof <masterip> <masterport>設置當本機為slave服務時,設置master服務的IP地址及端口,在Redis啟動時,它會自動從master進行數據同步。 即 本機若為salve時,設置master的IP地址和端口,當redis啟動時會自動從master進行數據同步。
masterauth
# If the master is password protected (using the "requirepass" configuration # directive below) it is possible to tell the slave to authenticate before # starting the replication synchronization process, otherwise the master will # refuse the slave request. # # masterauth <master-password>當master服務設置了密碼保護時,slave服務連接master的密碼
requirepass
# Require clients to issue AUTH <PASSWORD> before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. #requirepass artisan設置Redis連接密碼,如果配置了連接密碼,客戶端在連接Redis時需要通過AUTH <password>命令提供密碼,默認關閉
maxclients
# Set the max number of connected clients at the same time. By default # this limit is set to 10000 clients, however if the Redis server is not # able to configure the process file limit to allow for the specified limit # the max number of allowed clients is set to the current file limit # minus 32 (as Redis reserves a few file descriptors for internal uses). # # Once the limit is reached Redis will close all the new connections sending # an error 'max number of clients reached'. # # maxclients 10000設置同一時間最大客戶端連接數,默認無限制,Redis可以同時打開的客戶端連接數為Redis進程可以打開的最大文件描述符數,如果設置 maxclients 0,表示不作限制。當客戶端連接數到達限制時,Redis會關閉新的連接并向客戶端返回max number of clients reached錯誤信息
maxmemory
# Set a memory usage limit to the specified amount of bytes. # When the memory limit is reached Redis will try to remove keys # according to the eviction policy selected (see maxmemory-policy). # # If Redis can't remove keys according to the policy, or if the policy is # set to 'noeviction', Redis will start to reply with errors to commands # that would use more memory, like SET, LPUSH, and so on, and will continue # to reply to read-only commands like GET. # # This option is usually useful when using Redis as an LRU or LFU cache, or to # set a hard memory limit for an instance (using the 'noeviction' policy). # # WARNING: If you have slaves attached to an instance with maxmemory on, # the size of the output buffers needed to feed the slaves are subtracted # from the used memory count, so that network problems / resyncs will # not trigger a loop where keys are evicted, and in turn the output # buffer of slaves is full with DELs of keys evicted triggering the deletion # of more keys, and so forth until the database is completely emptied. # # In short... if you have slaves attached it is suggested that you set a lower # limit for maxmemory so that there is some free RAM on the system for slave # output buffers (but this is not needed if the policy is 'noeviction'). # # maxmemory <bytes>指定Redis最大內存限制,Redis在啟動時會把數據加載到內存中,達到最大內存后,Redis會先嘗試清除已到期或即將到期的Key,當此方法處理 后,仍然到達最大內存設置,將無法再進行寫入操作,但仍然可以進行讀取操作。Redis新的vm機制,會把Key存放內存,Value會存放在swap區
appendonly
# By default Redis asynchronously dumps the dataset on disk. This mode is # good enough in many applications, but an issue with the Redis process or # a power outage may result into a few minutes of writes lost (depending on # the configured save points). # # The Append Only File is an alternative persistence mode that provides # much better durability. For instance using the default data fsync policy # (see later in the config file) Redis can lose just one second of writes in a # dramatic event like a server power outage, or a single write if something # wrong with the Redis process itself happens, but the operating system is # still running correctly. # # AOF and RDB persistence can be enabled at the same time without problems. # If the AOF is enabled on startup Redis will load the AOF, that is the file # with the better durability guarantees. # # Please check http://redis.io/topics/persistence for more information.appendonly no指定是否在每次更新操作后進行日志記錄,Redis在默認情況下是異步的把數據寫入磁盤,如果不開啟,可能會在斷電時導致一段時間內的數據丟失。因為 redis本身同步數據文件是按上面save條件來同步的,所以有的數據會在一段時間內只存在于內存中。默認為no
appendfilename
# The name of the append only file (default: "appendonly.aof")appendfilename "appendonly.aof"指定更新日志文件名,默認為appendonly.aof
appendfsync
# The fsync() call tells the Operating System to actually write data on disk # instead of waiting for more data in the output buffer. Some OS will really flush # data on disk, some other OS will just try to do it ASAP. # # Redis supports three different modes: # # no: don't fsync, just let the OS flush the data when it wants. Faster. # always: fsync after every write to the append only log. Slow, Safest. # everysec: fsync only one time every second. Compromise. # # The default is "everysec", as that's usually the right compromise between # speed and data safety. It's up to you to understand if you can relax this to # "no" that will let the operating system flush the output buffer when # it wants, for better performances (but if you can live with the idea of # some data loss consider the default persistence mode that's snapshotting), # or on the contrary, use "always" that's very slow but a bit safer than # everysec. # # More details please check the following article: # http://antirez.com/post/redis-persistence-demystified.html # # If unsure, use "everysec".# appendfsync always appendfsync everysec # appendfsync no指定更新日志條件,共有3個可選值:
- no:表示等操作系統進行數據緩存同步到磁盤(快)
- always:表示每次更新操作后手動調用fsync()將數據寫到磁盤(慢,安全)
- everysec:表示每秒同步一次(折衷,默認值)
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指定包含其它的配置文件,可以在同一主機上多個Redis實例之間使用同一份配置文件,而同時各個實例又擁有自己的特定配置文件
protected-mode
# Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. # # When protected mode is on and if: # # 1) The server is not binding explicitly to a set of addresses using the # "bind" directive. # 2) No password is configured. # # The server only accepts connections from clients connecting from the # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain # sockets. # # By default protected mode is enabled. You should disable it only if # you are sure you want clients from other hosts to connect to Redis # even if no authentication is configured, nor a specific set of interfaces # are explicitly listed using the "bind" directive. protected-mode yes當安全模式啟用時
- 1)服務器沒有配置bind
- 2)沒有配置密碼
則服務器默認只接受本地的連接和 Unix domain sockets
maxmemory-policy
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # is reached. You can select among five behaviors: # # volatile-lru -> Evict using approximated LRU among the keys with an expire set. # allkeys-lru -> Evict any key using approximated LRU. # volatile-lfu -> Evict using approximated LFU among the keys with an expire set. # allkeys-lfu -> Evict any key using approximated LFU. # volatile-random -> Remove a random key among the ones 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 evict anything, just return an error on write operations. # # LRU means Least Recently Used # LFU means Least Frequently Used # # Both LRU, LFU and volatile-ttl are implemented using approximated # randomized algorithms. # # 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最大內存策略:如果達到內存限制,Redis如何選擇刪除key
可以在以下五個選項里選擇:
注意:對所有策略來說,如果Redis找不到合適的可以刪除的key都會在寫操作的時候返回一個錯誤。
目前涉及的命令有: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
默認值為:maxmemory-policy noeviction
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的Redis-15Redis基础配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis-13Redis发布订阅
- 下一篇: Redis-16Redis备份(持久化)