Redis 配置文件說明

Redis 配置文件

當(dāng)配置中需要配置內(nèi)存大小時(shí),可以使用 1k, 5GB, 4M 等類似的格式,其轉(zhuǎn)換方式如下(不區(qū)分大小寫)
1k => 1000 bytes
1kb => 1024 bytes
1m => 1000000 bytes
1mb => 1024*1024 bytes
1g => 1000000000 bytes
1gb => 1024*1024*1024 bytes

內(nèi)存配置大小寫是一樣的.比如 1gb 1Gb 1GB 1gB

daemonize no 默認(rèn)情況下,redis不是在后臺(tái)運(yùn)行的,如果需要在后臺(tái)運(yùn)行,把該項(xiàng)的值更改為yes
daemonize yes

當(dāng)redis在后臺(tái)運(yùn)行的時(shí)候,Redis默認(rèn)會(huì)把pid文件放在/var/run/redis.pid,你可以配置到其他地址。
當(dāng)運(yùn)行多個(gè)redis服務(wù)時(shí),需要指定不同的pid文件和端口
pidfile /var/run/redis.pid

指定redis運(yùn)行的端口,默認(rèn)是6379
port 6379

指定redis只接收來自于該IP地址的請(qǐng)求,如果不進(jìn)行設(shè)置,那么將處理所有請(qǐng)求,在生產(chǎn)環(huán)境中最好設(shè)置該項(xiàng)
bind 127.0.0.1

Specify the path for the unix socket that will be used to listen for incoming connections. There is no default, so Redis will not listen on a unix socket when not specified.
unixsocket /tmp/redis.sock
unixsocketperm 755

設(shè)置客戶端連接時(shí)的超時(shí)時(shí)間,單位為秒。當(dāng)客戶端在這段時(shí)間內(nèi)沒有發(fā)出任何指令,那么關(guān)閉該連接,0是關(guān)閉此設(shè)置
timeout 0

指定日志記錄級(jí)別,Redis總共支持四個(gè)級(jí)別:debug、verbose、notice、warning,默認(rèn)為verbose
debug 記錄很多信息,用于開發(fā)和測(cè)試;
varbose 有用的信息,不像debug會(huì)記錄那么多;
notice 普通的verbose,常用于生產(chǎn)環(huán)境;
warning 只有非常重要或者嚴(yán)重的信息會(huì)記錄到日志。
loglevel debug

配置log文件地址
默認(rèn)值為stdout,標(biāo)準(zhǔn)輸出,若后臺(tái)模式會(huì)輸出到/dev/null
logfile stdout
logfile /var/log/redis/redis.log

To enable logging to the system logger, just set 'syslog-enabled' to yes, and optionally update the other syslog parameters to suit your needs.
syslog-enabled no

Specify the syslog identity.
syslog-ident redis

Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
syslog-facility local0

可用數(shù)據(jù)庫數(shù),默認(rèn)值為16,默認(rèn)數(shù)據(jù)庫為0,數(shù)據(jù)庫范圍在0-(database-1)之間
databases 16

快照

保存數(shù)據(jù)到磁盤,格式如下: save
<seconds> <changes>

指出在多長(zhǎng)時(shí)間內(nèi),有多少次更新操作,就將數(shù)據(jù)同步到數(shù)據(jù)文件rdb。
相當(dāng)于條件觸發(fā)抓取快照,這個(gè)可以多個(gè)條件配合
比如默認(rèn)配置文件中的設(shè)置,就設(shè)置了三個(gè)條件
save 900 1 900秒內(nèi)至少有1個(gè)key被改變
save 300 10 300秒內(nèi)至少有300個(gè)key被改變
save 60 10000 60秒內(nèi)至少有10000個(gè)key被改變

save 900 1
save 300 10
save 60 10000

存儲(chǔ)至本地?cái)?shù)據(jù)庫時(shí)(持久化到rdb文件)是否壓縮數(shù)據(jù),默認(rèn)為yes
rdbcompression yes

本地持久化數(shù)據(jù)庫文件名,默認(rèn)值為dump.rdb
dbfilename dump.rdb

工作目錄,數(shù)據(jù)庫鏡像備份的文件放置的路徑。
這里的路徑跟文件名要分開配置是因?yàn)閞edis在進(jìn)行備份時(shí),先會(huì)將當(dāng)前數(shù)據(jù)庫的狀態(tài)寫入到一個(gè)臨時(shí)文件中,等備份完成時(shí),再把該臨時(shí)文件替換為上面所指定的文件,而這里的臨時(shí)文件和上面所配置的備份文件都會(huì)放在這個(gè)指定的路徑當(dāng)中。

AOF文件也會(huì)存放在這個(gè)目錄下面
注意這里必須制定一個(gè)目錄而不是文件
dir ./

復(fù)制

主從復(fù)制,設(shè)置該數(shù)據(jù)庫為其他數(shù)據(jù)庫的從數(shù)據(jù)庫.
當(dāng)本機(jī)為slave服務(wù)時(shí),設(shè)置master服務(wù)的IP地址及端口,在Redis啟動(dòng)時(shí),它會(huì)自動(dòng)從master進(jìn)行數(shù)據(jù)同步 slaveof
<masterip> <masterport>

當(dāng)master服務(wù)設(shè)置了密碼保護(hù)時(shí)(用requirepass制定的密碼),slave服務(wù)連接master的密碼
masterauth <master-password>

當(dāng)從庫同主機(jī)失去連接或者復(fù)制正在進(jìn)行,從機(jī)庫有兩種運(yùn)行方式:
1、如果slave-serve-stale-data設(shè)置為“yes”(缺省值),則從設(shè)備仍然會(huì)回應(yīng)客戶端請(qǐng)求,可能是數(shù)據(jù)已過時(shí),或者如果這是第一次同步,數(shù)據(jù)集可能只是空的。
2、如果slave-serve-stale-data設(shè)置為“no”,那么從設(shè)備將回復(fù)一個(gè)帶有主進(jìn)程的錯(cuò)誤SYNC“到所有類型的命令,但是返回到INFO和SLAVEOF。
slave-serve-stale-data yes

從庫會(huì)按照一個(gè)時(shí)間間隔向主庫發(fā)送PING.可以通過repl-ping-slave-period設(shè)置這個(gè)時(shí)間間隔,默認(rèn)是10秒
repl-ping-slave-period 10

repl-timeout 設(shè)置主庫批量數(shù)據(jù)傳輸時(shí)間或者ping回復(fù)時(shí)間間隔,默認(rèn)值是60秒,一定要確保repl-timeout大于repl-ping-slave-period
repl-timeout 60

安全

設(shè)置客戶端連接后進(jìn)行任何其他指定前需要使用的密碼。
警告:因?yàn)閞edis速度相當(dāng)快,所以在一臺(tái)比較好的服務(wù)器下,一個(gè)外部的用戶可以在一秒鐘進(jìn)行150K次的密碼嘗試,這意味著你需要指定非常非常強(qiáng)大的密碼來防止暴力破解
requirepass foobared

命令重命名:
在一個(gè)共享環(huán)境下可以重命名相對(duì)危險(xiǎn)的命令。比如把CONFIG重名為一個(gè)不容易猜測(cè)的字符。
舉例:
rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52

如果想刪除一個(gè)命令,直接把它重命名為一個(gè)空字符""即可,如下:
rename-command CONFIG ""

約束

設(shè)置同一時(shí)間最大客戶端連接數(shù),默認(rèn)無限制,Redis可以同時(shí)打開的客戶端連接數(shù)為Redis進(jìn)程可以打開的最大文件描述符數(shù),如果設(shè)置 maxclients 0 表示不作限制。
當(dāng)客戶端連接數(shù)到達(dá)限制時(shí),Redis會(huì)關(guān)閉新的連接并向客戶端返回max number of clients reached錯(cuò)誤信息。
maxclients 128

指定Redis最大內(nèi)存限制,Redis在啟動(dòng)時(shí)會(huì)把數(shù)據(jù)加載到內(nèi)存中,達(dá)到最大內(nèi)存后,Redis會(huì)先嘗試清除已到期或即將到期的Key,Redis同時(shí)也會(huì)移除空的list對(duì)象
當(dāng)此方法處理后,仍然到達(dá)最大內(nèi)存設(shè)置,將無法再進(jìn)行寫入操作,但仍然可以進(jìn)行讀取操作
注意:Redis新的vm機(jī)制,會(huì)把Key存放內(nèi)存,Value會(huì)存放在swap區(qū)
maxmemory的設(shè)置比較適合于把redis當(dāng)作于類似memcached的緩存來使用,而不適合當(dāng)做一個(gè)真實(shí)的DB。
當(dāng)把Redis當(dāng)做一個(gè)真實(shí)的數(shù)據(jù)庫使用的時(shí)候,內(nèi)存使用將是一個(gè)很大的開銷
maxmemory <bytes>

當(dāng)內(nèi)存達(dá)到最大值的時(shí)候Redis會(huì)選擇刪除哪些數(shù)據(jù)?有五種方式可供選擇:
volatile-lru -> 利用LRU算法移除設(shè)置過過期時(shí)間的key (LRU:最近使用 Least Recently Used )
allkeys-lru -> 利用LRU算法移除任何key
volatile-random -> 移除設(shè)置過過期時(shí)間的隨機(jī)key
allkeys-random -> 隨機(jī)刪除一個(gè)key
volatile-ttl -> 移除即將過期的key(minor TTL)
noeviction -> 不移除任何可以,只是返回一個(gè)寫錯(cuò)誤

注意:對(duì)于上面的策略,如果沒有合適的key可以移除,當(dāng)寫的時(shí)候Redis會(huì)返回一個(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

LRU 和 minimal TTL 算法都不是精準(zhǔn)的算法,但是相對(duì)精確的算法(為了節(jié)省內(nèi)存),隨意你可以選擇樣本大小進(jìn)行檢測(cè)。
Redis默認(rèn)的灰選擇3個(gè)樣本進(jìn)行檢測(cè),你可以通過maxmemory-samples進(jìn)行設(shè)置
maxmemory-samples 3

AOF

默認(rèn)情況下,redis會(huì)在后臺(tái)異步的把數(shù)據(jù)庫鏡像備份到磁盤,但是該備份是非常耗時(shí)的,而且備份也不能很頻繁,如果發(fā)生諸如拉閘限電、拔插頭等狀況,那么將造成比較大范圍的數(shù)據(jù)丟失。

所以redis提供了另外一種更加高效的數(shù)據(jù)庫備份及災(zāi)難恢復(fù)方式。
開啟append only模式之后,redis會(huì)把所接收到的每一次寫操作請(qǐng)求都追加到appendonly.aof文件中,當(dāng)redis重新啟動(dòng)時(shí),會(huì)從該文件恢復(fù)出之前的狀態(tài)。

但是這樣會(huì)造成appendonly.aof文件過大,所以redis還支持了BGREWRITEAOF指令,對(duì)appendonly.aof 進(jìn)行重新整理。

你可以同時(shí)開啟asynchronous dumps 和 AOF
appendonly no

AOF文件名稱 (默認(rèn): "appendonly.aof")
appendfilename appendonly.aof

Redis支持三種同步AOF文件的策略:
no: 不進(jìn)行同步,系統(tǒng)去操作 . Faster.
always: always表示每次有寫操作都進(jìn)行同步. Slow, Safest.
everysec: 表示對(duì)寫操作進(jìn)行累積,每秒同步一次.

Compromise.
默認(rèn)是"everysec",按照速度和安全折中這是最好的。
如果想讓Redis能更高效的運(yùn)行,你也可以設(shè)置為"no",讓操作系統(tǒng)決定什么時(shí)候去執(zhí)行
或者相反想讓數(shù)據(jù)更安全你也可以設(shè)置為"always"
如果不確定就用 "everysec".

appendfsync always
appendfsync everysec
appendfsync no

AOF策略設(shè)置為always或者everysec時(shí),后臺(tái)處理進(jìn)程(后臺(tái)保存或者AOF日志重寫)會(huì)執(zhí)行大量的I/O操作,在某些Linux配置中會(huì)阻止過長(zhǎng)的fsync()請(qǐng)求。注意現(xiàn)在沒有任何修復(fù),即使fsync在另外一個(gè)線程進(jìn)行處理為了減緩這個(gè)問題,可以設(shè)置下面這個(gè)參數(shù)no-appendfsync-on-rewrite

This means that while another child is saving the durability of Redis is the same as "appendfsync none", that in pratical terms means that it is possible to lost up to 30 seconds of log in the worst scenario (with the default Linux settings).
If you have latency problems turn this to "yes". Otherwise leave it as "no" that is the safest pick from the point of view of durability.
no-appendfsync-on-rewrite no

Automatic rewrite of the append only file.
AOF 自動(dòng)重寫
當(dāng)AOF文件增長(zhǎng)到一定大小的時(shí)候Redis能夠調(diào)用 BGREWRITEAOF 對(duì)日志文件進(jìn)行重寫它是這樣工作的:Redis會(huì)記住上次進(jìn)行些日志后文件的大小(如果從開機(jī)以來還沒進(jìn)行過重寫,那日子大小在開機(jī)的時(shí)候確定)基礎(chǔ)大小會(huì)同現(xiàn)在的大小進(jìn)行比較。如果現(xiàn)在的大小比基礎(chǔ)大小大制定的百分比,重寫功能將啟動(dòng)同時(shí)需要指定一個(gè)最小大小用于AOF重寫,這個(gè)用于阻止即使文件很小但是增長(zhǎng)幅度很大也去重寫AOF文件的情況設(shè)置 percentage為0就關(guān)閉這個(gè)特性

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

SLOW LOG

Redis Slow Log 記錄超過特定執(zhí)行時(shí)間的命令。執(zhí)行時(shí)間不包括I/O計(jì)算比如連接客戶端,返回結(jié)果等,只是命令執(zhí)行時(shí)間可以通過兩個(gè)參數(shù)設(shè)置slow log:一個(gè)是告訴Redis執(zhí)行超過多少時(shí)間被記錄的參數(shù)slowlog-log-slower-than(微妙),另一個(gè)是slow log 的長(zhǎng)度。當(dāng)一個(gè)新命令被記錄的時(shí)候最早的命令將被從隊(duì)列中移除,下面的時(shí)間以微妙微單位,因此1000000代表一分鐘。
注意制定一個(gè)負(fù)數(shù)將關(guān)閉慢日志,而設(shè)置為0將強(qiáng)制每個(gè)命令都會(huì)記錄
slowlog-log-slower-than 10000

對(duì)日志長(zhǎng)度沒有限制,只是要注意它會(huì)消耗內(nèi)存,可以通過 SLOWLOG RESET回收被慢日志消耗的內(nèi)存
slowlog-max-len 1024

VM

WARNING! Virtual Memory is deprecated in Redis 2.4 The use of Virtual Memory is strongly discouraged.

Virtual Memory allows Redis to work with datasets bigger than the actual amount of RAM needed to hold the whole dataset in memory.
In order to do so very used keys are taken in memory while the other keys are swapped into a swap file, similarly to what operating systems do with memory pages.
To enable VM just set 'vm-enabled' to yes, and set the following three VM parameters accordingly to your needs.

vm-enabled no
vm-enabled yes

This is the path of the Redis swap file. As you can guess, swap files can't be shared by different Redis instances, so make sure to use a swap file for every redis process you are running. Redis will complain if
the swap file is already in use.
The best kind of storage for the Redis swap file (that's accessed at random) is a Solid State Disk (SSD).
*** WARNING *** if you are using a shared hosting the default of putting the swap file under /tmp is not secure. Create a dir with access granted only to Redis user and configure Redis to create the swap file there.
vm-swap-file /tmp/redis.swap

vm-max-memory configures the VM to use at max the specified amount of RAM. Everything that deos not fit will be swapped on disk if possible, that is, if there is still enough contiguous space in the swap file.

With vm-max-memory 0 the system will swap everything it can. Not a good default, just specify the max amount of RAM you can in bytes, but it's better to leave some margin. For instance specify an amount of RAM that's more or less between 60 and 80% of your free RAM.
vm-max-memory 0

Redis swap files is split into pages. An object can be saved using multiple contiguous pages, but pages can't be shared between different objects. So if your page is too big, small objects swapped out on disk will waste a lot of space. If you page is too small, there is less space in the swap file (assuming you configured the same number of total swap file pages).
If you use a lot of small objects, use a page size of 64 or 32 bytes.
If you use a lot of big objects, use a bigger page size.
If unsure, use the default :)
vm-page-size 32

Number of total memory pages in the swap file. Given that the page table (a bitmap of free/used pages) is taken in memory, every 8 pages on disk will consume 1 byte of RAM.
The total swap size is vm-page-size * vm-pages With the default of 32-bytes memory pages and 134217728 pages Redis will use a 4 GB swap file, that will use 16 MB of RAM for the page table.
It's better to use the smallest acceptable value for your application, but the default is large in order to work in most conditions.
vm-pages 134217728

Max number of VM I/O threads running at the same time. This threads are used to read/write data from/to swap file, since they also encode and decode objects from disk to memory or the reverse, a bigger number of
threads can help with big objects even if they can't help with I/O itself as the physical device may not be able to couple with many reads/writes operations at the same time. The special value of 0 turn off threaded I/O and enables the blocking Virtual Memory implementation.
vm-max-threads 4

ADVANCED CONFIG

當(dāng)hash中包含超過指定元素個(gè)數(shù)并且最大的元素沒有超過臨界時(shí),hash將以一種特殊的編碼方式(大大減少內(nèi)存使用)來存儲(chǔ),這里可以設(shè)置這兩個(gè)臨界值
Redis Hash對(duì)應(yīng)Value內(nèi)部實(shí)際就是一個(gè)HashMap,實(shí)際這里會(huì)有2種不同實(shí)現(xiàn), 這個(gè)Hash的成員比較少時(shí)Redis為了節(jié)省內(nèi)存會(huì)采用類似一維數(shù)組的方式來緊湊存儲(chǔ),而不會(huì)采用真正的HashMap結(jié)構(gòu),對(duì)應(yīng)的value redisObject的encoding為zipmap, 當(dāng)成員數(shù)量增大時(shí)會(huì)自動(dòng)轉(zhuǎn)成真正的HashMap,此時(shí)encoding為ht。
hash-max-zipmap-entries 512
hash-max-zipmap-value 64

list數(shù)據(jù)類型多少節(jié)點(diǎn)以下會(huì)采用去指針的緊湊存儲(chǔ)格式。
list數(shù)據(jù)類型節(jié)點(diǎn)值大小小于多少字節(jié)會(huì)采用緊湊存儲(chǔ)格式。
list-max-ziplist-entries 512
list-max-ziplist-value 64

set數(shù)據(jù)類型內(nèi)部數(shù)據(jù)如果全部是數(shù)值型,且包含多少節(jié)點(diǎn)以下會(huì)采用緊湊格式存儲(chǔ)。
set-max-intset-entries 512

zsort數(shù)據(jù)類型多少節(jié)點(diǎn)以下會(huì)采用去指針的緊湊存儲(chǔ)格式。
zsort數(shù)據(jù)類型節(jié)點(diǎn)值大小小于多少字節(jié)會(huì)采用緊湊存儲(chǔ)格式。
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

Redis將在每100毫秒時(shí)使用1毫秒的CPU時(shí)間來對(duì)redis的hash表進(jìn)行重新hash,可以降低內(nèi)存的使用當(dāng)你的使用場(chǎng)景中,有非常嚴(yán)格的實(shí)時(shí)性需要,不能夠接受Redis時(shí)不時(shí)的對(duì)請(qǐng)求有2毫秒的延遲的話,把這項(xiàng)配置為no。如果沒有這么嚴(yán)格的實(shí)時(shí)性要求,可以設(shè)置為yes,以便能夠盡可能快的釋放內(nèi)存
activerehashing yes

INCLUDES

指定包含其它的配置文件,可以在同一主機(jī)上多個(gè)Redis實(shí)例之間使用同一份配置文件,而同時(shí)各個(gè)實(shí)例又擁有自己的特定配置文件
include /path/to/local.conf
include /path/to/other.conf

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容