Redis-15Redis基础配置文件
文章目錄
- 概述
- Redis CONFIG 命令
- CONFIG GET
- CONFIG SET
- Redis redis.conf文件
- 部分配置項(xiàng)說(shuō)明
- 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 系統(tǒng),則默認(rèn)的配置文件就是 redis .window.conf 如果是 Linux 系統(tǒng),則是 redis.conf。在大部分的情況下我們都使用到Linux 環(huán)境,這里我們以linux環(huán)境為例。
Redis CONFIG 命令
可以通過(guò) CONFIG 命令查看或設(shè)置配置項(xiàng),臨時(shí)生效。重啟后失效。
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>使用 * 號(hào)獲取所有配置項(xiàng)
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
可以通過(guò)修改 redis.conf 文件或使用 CONFIG set 命令來(lái)修改配置。
語(yǔ)法
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下的安裝及常見(jiàn)問(wèn)題
部分配置項(xiàng)說(shuō)明
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默認(rèn)不是以守護(hù)進(jìn)程的方式運(yùn)行,可以通過(guò)該配置項(xiàng)修改,使用yes啟用守護(hù)進(jìn)程
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當(dāng)Redis以守護(hù)進(jìn)程方式運(yùn)行,如果沒(méi)有創(chuàng)建成功,看看是不是在/var/run沒(méi)有創(chuàng)建文件的權(quán)限 . 前提 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監(jiān)聽(tīng)端口
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綁定的主機(jī)地址
timeout
# Close the connection after a client is idle for N seconds (0 to disable) timeout 0當(dāng) 客戶(hù)端閑置多長(zhǎng)時(shí)間后關(guān)閉連接,如果指定為0,表示關(guān)閉該功能
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指定日志記錄級(jí)別,Redis總共支持四個(gè)級(jí)別:debug、verbose、notice、warning,默認(rèn)為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 ""指定日志文件位置.,默認(rèn)為標(biāo)準(zhǔn)輸出,如果配置Redis為守護(hù)進(jìn)程方式運(yùn)行,而這里又配置為日志記錄方式為標(biāo)準(zhǔn)輸出,則日志將會(huì)發(fā)送給/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設(shè)置數(shù)據(jù)庫(kù)的數(shù)量,默認(rèn)數(shù)據(jù)庫(kù)為0,可以使用SELECT <dbid>命令在連接上指定數(shù)據(jù)庫(kù)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指定在多長(zhǎng)時(shí)間內(nèi),有多少次更新操作,就將數(shù)據(jù)同步到數(shù)據(jù)文件,可以多個(gè)條件配合.
Redis默認(rèn)配置文件中提供了三個(gè)條件:
save 900 1save 300 10save 60 10000分別表示900秒(15分鐘)內(nèi)有1個(gè)更改,300秒(5分鐘)內(nèi)有10個(gè)更改以及60秒內(nèi)有10000個(gè)更改。
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指定存儲(chǔ)至本地?cái)?shù)據(jù)庫(kù)時(shí)是否壓縮數(shù)據(jù),默認(rèn)為yes,Redis采用LZF壓縮,如果為了節(jié)省CPU時(shí)間,可以關(guān)閉該選項(xiàng),但會(huì)導(dǎo)致數(shù)據(jù)庫(kù)文件變的巨大
dbfilename
# The filename where to dump the DB dbfilename dump.rd指定本地?cái)?shù)據(jù)庫(kù)文件名,默認(rèn)值為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 .指定本地?cái)?shù)據(jù)庫(kù)存放目錄
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>設(shè)置當(dāng)本機(jī)為slave服務(wù)時(shí),設(shè)置master服務(wù)的IP地址及端口,在Redis啟動(dòng)時(shí),它會(huì)自動(dòng)從master進(jìn)行數(shù)據(jù)同步。 即 本機(jī)若為salve時(shí),設(shè)置master的IP地址和端口,當(dāng)redis啟動(dòng)時(shí)會(huì)自動(dòng)從master進(jìn)行數(shù)據(jù)同步。
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>當(dāng)master服務(wù)設(shè)置了密碼保護(hù)時(shí),slave服務(wù)連接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設(shè)置Redis連接密碼,如果配置了連接密碼,客戶(hù)端在連接Redis時(shí)需要通過(guò)AUTH <password>命令提供密碼,默認(rèn)關(guān)閉
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設(shè)置同一時(shí)間最大客戶(hù)端連接數(shù),默認(rèn)無(wú)限制,Redis可以同時(shí)打開(kāi)的客戶(hù)端連接數(shù)為Redis進(jìn)程可以打開(kāi)的最大文件描述符數(shù),如果設(shè)置 maxclients 0,表示不作限制。當(dāng)客戶(hù)端連接數(shù)到達(dá)限制時(shí),Redis會(huì)關(guān)閉新的連接并向客戶(hù)端返回max number of clients reached錯(cuò)誤信息
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最大內(nèi)存限制,Redis在啟動(dòng)時(shí)會(huì)把數(shù)據(jù)加載到內(nèi)存中,達(dá)到最大內(nèi)存后,Redis會(huì)先嘗試清除已到期或即將到期的Key,當(dāng)此方法處理 后,仍然到達(dá)最大內(nèi)存設(shè)置,將無(wú)法再進(jìn)行寫(xiě)入操作,但仍然可以進(jìn)行讀取操作。Redis新的vm機(jī)制,會(huì)把Key存放內(nèi)存,Value會(huì)存放在swap區(qū)
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指定是否在每次更新操作后進(jìn)行日志記錄,Redis在默認(rèn)情況下是異步的把數(shù)據(jù)寫(xiě)入磁盤(pán),如果不開(kāi)啟,可能會(huì)在斷電時(shí)導(dǎo)致一段時(shí)間內(nèi)的數(shù)據(jù)丟失。因?yàn)?redis本身同步數(shù)據(jù)文件是按上面save條件來(lái)同步的,所以有的數(shù)據(jù)會(huì)在一段時(shí)間內(nèi)只存在于內(nèi)存中。默認(rèn)為no
appendfilename
# The name of the append only file (default: "appendonly.aof")appendfilename "appendonly.aof"指定更新日志文件名,默認(rèn)為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個(gè)可選值:
- no:表示等操作系統(tǒng)進(jìn)行數(shù)據(jù)緩存同步到磁盤(pán)(快)
- always:表示每次更新操作后手動(dòng)調(diào)用fsync()將數(shù)據(jù)寫(xiě)到磁盤(pán)(慢,安全)
- everysec:表示每秒同步一次(折衷,默認(rèn)值)
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指定包含其它的配置文件,可以在同一主機(jī)上多個(gè)Redis實(shí)例之間使用同一份配置文件,而同時(shí)各個(gè)實(shí)例又擁有自己的特定配置文件
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當(dāng)安全模式啟用時(shí)
- 1)服務(wù)器沒(méi)有配置bind
- 2)沒(méi)有配置密碼
則服務(wù)器默認(rèn)只接受本地的連接和 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最大內(nèi)存策略:如果達(dá)到內(nèi)存限制,Redis如何選擇刪除key
可以在以下五個(gè)選項(xiàng)里選擇:
注意:對(duì)所有策略來(lái)說(shuō),如果Redis找不到合適的可以刪除的key都會(huì)在寫(xiě)操作的時(shí)候返回一個(gè)錯(cuò)誤。
目前涉及的命令有: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
默認(rèn)值為:maxmemory-policy noeviction
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的Redis-15Redis基础配置文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Redis-13Redis发布订阅
- 下一篇: Redis-16Redis备份(持久化)