Redis的配置文件位於redis的安裝目錄下,一般不要直接操作出廠設置的配置文件,需要對其進行備份。# Redis的配置文件樣例:
# Redis configuration file example.#
# 請注意,為了讀取到配置文件,Redis必須文件路徑作為第一個參數來啟動: # # ./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 # # units are case insensitive so 1GB 1Gb 1gB are all the same.
1.INCLUDE部分:
################################## INCLUDES ################################### # 在配置多個服務器時很有用,比如,多個服務器的很多個配置項是相同的,但是少部分配 # 置又要不相同,例如:bind等配置,這時可以將相同的配置抽取出來,作為一個單獨文件. # 其他配置項根據不同的服務器做不同的配置,最后通過include引用;如果include放在文件 # 的開頭,那么redis.conf的配置會覆蓋引入文件中的配置,所以如果想要使用引入文件中的配置 # 可以將include放在最后 # include /path/to/local.conf # include /path/to/other.conf
2.MODULES部分:
################################## MODULES ##################################### # Load modules at startup. If the server is not able to load modules # it will abort. It is possible to use multiple loadmodule directives. # # loadmodule /path/to/my_module.so # loadmodule /path/to/other_module.so
NETWORK部分:
################################## NETWORK ######################################
# 默認情況下,如果未指定“ bind”配置指令,則Redis監聽服務器上所有可用網絡接口的連接。
# 可以使用“ bind”配置指令偵聽一個或多個選定接口,然后偵聽一個 或更多IP地址 # Examples: # # bind 192.168.1.100 10.0.0.1 # bind 127.0.0.1 ::1 # # ~~~ WARNING ~~~
# 如果運行Redis的電腦直接暴露在互聯網中,那么就會將實例暴露給所有人,這是很危險的。
# 指定 redis 只接收來自於該 IP 地址的請求,如果不進行設置,那么將處理所有請求 bind 127.0.0.1 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. # # 當開啟保護模式時,即使沒有綁定任何IP,也沒有配置密碼,Redis仍然只接受本機的訪問 # # 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 # redis監聽的端口號。 port 6379
# 此參數確定了TCP連接中已完成隊列(完成三次握手之后)的長度,
# 當然此值必須不大於Linux系統定義的/proc/sys/net/core/somaxconn值
# 默認是511,而Linux的默認參數值是128。
# 當系統並發量大並且客戶端速度緩慢的時候,可以將這二個參數一起參考設定。
# 該內核參數默認值一般是128,對於負載很大的服務程序來說大大的不夠。
# 一般會將它修改為2048或者更大。
# 在/etc/sysctl.conf中添加:net.core.somaxconn = 2048,
# 然后在終端中執行sysctl -p。
tcp-backlog 511
# 配置unix socket來讓redis支持監聽本地連接。
# unixsocket /var/run/redis/redis.sock
# 配置unix socket使用文件的權限 # unixsocketperm 700 # 此參數為設置客戶端空閑超過timeout,服務端會斷開連接,為0則服務端不會主動斷開連接,不能小於0。 timeout 0
# tcp keepalive參數。如果設置不為0,就使用配置的SO_KEEPALIVE值,使用keepalive有兩個好處:檢測掛掉的對端。降低中間設備出問題而
# 導致網絡看似連接卻已經與對端端口的問題。
# 在Linux內核中,設置了keepalive,redis會定時給對端發送ack。檢測到對端關閉需要兩倍的設置值。
# 此選項的合理值是300秒,這是從Redis 3.2.1開始的新Redis默認值
tcp-keepalive 300
GENERAL部分:
################################# GENERAL #####################################
# 默認情況下,Redis不會作為守護程序運行。 如果需要,請使用“yes”。
# 請注意,Redis守護進程將在/var/run/redis.pid中寫入一個pid文件
daemonize yes
# If you run Redis from upstart or systemd, Redis can interact with your # supervision tree. Options: # supervised no - no supervision interaction # supervised upstart - signal upstart by putting Redis into SIGSTOP mode # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET # supervised auto - detect upstart or systemd method based on # UPSTART_JOB or NOTIFY_SOCKET environment variables # Note: these supervision methods only signal "process is ready." # They do not enable continuous liveness pings back to your supervisor. supervised no
# 進程管道id文件,如果指定了pid文件,則Redis會在啟動時將其寫入指定位置,然后在退出時將其刪除。
# 當服務器在非守護進程下運行時,如果沒有pid文件,則不會創建在配置中指定。
# 當服務器是后台進程時,pidfile是不被指定也會被使用,默認為/var/run/redis.pid
# 如果Redis無法創建它,則不會發生任何不良情況,服務器將正常啟動並運行。
pidfile /var/run/redis_6379.pid # 日志級別: # debug (開發/測試階段) # verbose (有用信息比較少,但不會像debug這么混亂) # notice (中等冗長,您可能想在生產中使用) # warning (用於生產環境) loglevel notice # 用於指定記錄日志的文件,空字符串的話,日志會打印到標准輸出設備中。后台運行的redis的標准輸出是/dev/null logfile "" # 是否打開記錄syslog的功能 # syslog-enabled no # syslog的標識符 # syslog-ident redis # 指定syslog的設備,值可以是user或者是local0-local7 # syslog-facility local0 # 數據庫的數量,默認使用的是數據庫0,可以使用select命令來切換數據庫 databases 16 # By default Redis shows an ASCII art logo only when started to log to the # standard output and if the standard output is a TTY. Basically this means # that normally a logo is displayed only in interactive sessions. # # However it is possible to force the pre-4.0 behavior and always show a # ASCII art logo in startup logs by setting the following option to yes. always-show-logo yes
SNAPSHOTTING部分(Redis持久化之rdb):
################################ SNAPSHOTTING ################################
# Redis的持久化 # Save the DB on disk: # 保存格式: # save <seconds> <changes> #
# 如果同時發生了給定的秒數和給定的針對數據庫的寫操作,那么就會保存到數據庫。 # # 在下面的實例中就會出現持久化: # 在900s(15min)后,至少有1個鍵值發生變化 # 在300s(5min)后,至少修改了10個鍵值發生了變化 # 在60s(1min)后,至少修改了10000個鍵值發生了變化 # # 注意:你可以注釋掉所有的save命令來禁用save # # 也可以通過配置帶有單個空字符串參數的save命令來刪除所有先前配置的save指令 # 如下所示: # # save "" save 900 1 save 300 10 save 60 10000 # 默認情況下,如果啟用rdb快照保存失敗,那么Redis將停止接受寫入 # 這就會使用戶能夠意識到數據不能正確地持久化存儲在磁盤上,否則很可能會沒人注意到並發生一些災難 # 如果后台保存過程將再次開始工作,那么Redis就會自動允許再次寫入 # 當rdb出現問題時,是否依然繼續進行工作,yes:不能進行工作,no:可以進行工作
# 如果配成no的話,表示你不在乎數據的不一致或者有別的辦法來控制或者維護
stop-writes-on-bgsave-error yes # 對於存儲到磁盤中的快照,你可以設置是和否進行壓縮存儲。如果是的話,Redis會使用LZF壓縮算法進行壓縮, # 如果不想消耗CPU來進行壓縮的話,可以關閉此功能。其實影響不大。 rdbcompression yes # 在存儲和加載rdb文件的時候是否使用CRC64校驗和來進行校驗,如果開啟,將消耗一定的性能,保持默認設置即可。 # 在禁用校驗和的情況下創建rdb文件,校驗和為0,這將指示加載代碼跳過校驗。 rdbchecksum yes # rdb的文件的名稱 dbfilename dump.rdb # 工作目錄: # 在哪個目錄下啟動Redis,那么這個路徑就是工作目錄,那么redis的日志就是生成在這個目錄下 # 數據目錄,數據庫的寫入就是在這個目錄下。aof和rdb文件也會寫入這個文件夾中。 # 請注意,您必須在此指定路徑而不是文件名。
# 可以使用config get dir命令來獲取工作目錄。
dir ./
如何觸發RDB快照:
1.配置文件中的默認快照設置(冷拷貝之后重新使用 copy dump.rdb dump_new.rdb,最好主機和備份機是兩台服務器)
2.命令save和bgsave都可以立即生成dump.rdb文件然后之前的舊的rdb文件
save:save只管保存,即當之前save命令的時候,就無法存儲數據了
bgsave:Redis會在后台異步進行快照操作。
執行flushall命令也會立即生成dump.rdb文件但是此文件為空。
如何恢復Redis中的數據:
將備份文件復制到Redis的暗安裝目錄下,然后重新啟動服務。
RDB的優勢:
適合大數據內容的存儲和恢復
相較於AOF,RDB更適合大數據集的恢復
RDB的劣勢:
容易丟失最后一次數據的快照
在fork一個子進程的時候,接下來的工作全部由子進程來操作,父進程不進行任何IO操作,所以內存中的數據被克隆了一份,內存的膨脹需要考慮。
如何停止:
動態停止所有保存rdb保存規則的方法:redis-cli config save ""
SECUTITY部分:
################################## SECURITY ################################### #
# 需要用戶在執行任何命令之前先輸入AUTH<PASSWORD>
# # 為了保持向后兼容,應該注釋掉該命令,因為大部分的用戶也不要認證。 # # 警告:在使用requirepass的時候,由於redis實在是太快了,所以因為設置一個更加安全的密碼 # # requirepass foobared # 命令重命名。 # 可以在共享環境中更改危險命令的名稱。 例如,可以將CONFIG命令重命名為一些難以猜測的名稱,以便它仍可用於內部使用的工具,但不適用於一般客戶。 # 例如: # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 # # 通過將命令重命名為空字符串也可以完全取消該命令: # rename-command CONFIG "" # # 請注意,更改登錄到AOF文件或傳輸到副本的命令的名稱可能會導致問題。
secutity部分是全部被注釋掉的,所以Redis默認是不需要輸入密碼的,因為Redis是在Linux環境下的服務器中運行,那么安全要求肯定是很高的。
Clients部分:
################################### CLIENTS #################################### # 設置同時連接的最大客戶端數量。默認情況下,此限制設置為10000個客戶端連接, # 但是,如果Redis服務器無法配置進程文件限制以允許指定的限制,則允許的最大客戶端數將設置為當前文件限制減去32(因為Redis保留了 內部使用的文件描述符很少)
# 一旦達到限制,Redis將關閉所有新連接,並發送錯誤消息“已達到最大客戶端數”。
# maxclients 10000
內存管理MEMORY MANAGEMENT部分:
############################## MEMORY MANAGEMENT ################################
# 設置能連上redis的最大客戶端連接數量。默認是10000個客戶端連接。由於redis不區分連接是客戶端連接還是內部打開文件或者和slave連接等,所以maxclients最小建議設置到 # 32。如果超過了maxclients,redis會給新的連接發送’max number of clients reached’,並關閉連接。
# 當達到內存限制時,Redis將嘗試根據所選的逐出策略(請參見maxmemory-policy)刪除key。 # 當Redis無法根據策略刪除key時,或者如果策略被設置為“noeviction”時,Redis會對set、push這些指令返回錯誤消息,而對get之類的指定繼續回復
# 當將Redis用作LRU或LFU緩存,或為實例設置硬盤限制(使用“ noeviction”策略)時,此選項通常很有用。 # 注意slave的輸出緩沖區是不計算在maxmemory內的。所以為了防止主機內存使用完,建議設置的maxmemory需要更小一些。 # maxmemory <bytes> # MAXMEMORY POLICY: 當內存容量超過設置的maxmemory后的處理策略: # # volatile-lru -> 利用LRU算法移除設置過過期時間的key # 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 -> 隨機移除設置過過期時間的key。 # 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 # LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated # algorithms (in order to save memory), so you can tune it for speed or # accuracy. For default Redis will check five keys and pick the one that was # used less recently, you can change the sample size using the following # configuration directive. # # The default of 5 produces good enough results. 10 Approximates very closely # true LRU but costs more CPU. 3 is faster but not very accurate. # # maxmemory-samples 5 # Starting from Redis 5, by default a replica will ignore its maxmemory setting # (unless it is promoted to master after a failover or manually). It means # that the eviction of keys will be just handled by the master, sending the # DEL commands to the replica as keys evict in the master side. # # This behavior ensures that masters and replicas stay consistent, and is usually # what you want, however if your replica is writable, or you want the replica to have # a different memory setting, and you are sure all the writes performed to the # replica are idempotent, then you may change this default (but be sure to understand # what you are doing). # # Note that since the replica by default does not evict, it may end using more # memory than the one set via maxmemory (there are certain buffers that may # be larger on the replica, or data structures may sometimes take more memory and so # forth). So make sure you monitor your replicas and make sure they have enough # memory to never hit a real out-of-memory condition before the master hits # the configured maxmemory setting. # # replica-ignore-maxmemory yes
APPEND ONLY MODE(Redis持久化之aof)
############################## APPEND ONLY MODE ############################### # 默認情況下,redis異步將數據寫入磁盤中。此模式在許多應用中已經足夠好了,但是redis進程問題或者電源中斷可能會導致 # 幾分鍾的寫入丟失(取決於配置的保存點) # 可以同時啟用aof和rdb,並且redis先加載aof # aof是默認關閉的,需要手動開啟 appendonly no # aof文件的名稱為appendonly.aof appendfilename "appendonly.aof" # Redis支持三種模式: # appendfsync always: 持續持久化,每次數據發生變更就會記錄到磁盤中,數據完整性較好但性能較差 # appendfsync everysec: 出廠默認推薦,每秒記錄一次,如果一秒內出現宕機,有數據丟失 # appendfsync no :從不同步 # 如果不確定,就是用默認出廠推薦everysec
# appendfsync always appendfsync everysec # appendfsync no # 重寫數據的時候能否使用appendfsync對aof文件進行數據的追加。 # 默認使用no,保證數據的一致性。 no-appendfsync-on-rewrite no # 自動重寫aof文件 # 當AOF文件大小增加指定百分比時,Redis會自動調用BGREWRITEAOF 進行重寫 # # 觸發機制:Redis會記住最近一次重寫后文件的大小(如果自重新啟動以來,沒有發生過重寫,那么就是用啟動時aof文件的大小) # 將當前文件的大小和之間記錄的文件的大小進行比較,如果超過了指定的百分比就會進行重寫。另外,需要指定文件的最小大小,即使超過了指定的百分比,但是沒有超過最小的文件大小,也是不會重寫的。 # 這對於重寫aof文件非常有用。
# 100%就是一倍 auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb # # If aof-load-truncated is set to yes, a truncated AOF file is loaded and # the Redis server starts emitting a log to inform the user of the event. # Otherwise if the option is set to no, the server aborts with an error # and refuses to start. When the option is set to no, the user requires # to fix the AOF file using the "redis-check-aof" utility before to restart # the server. # # Note that if the AOF file will be found to be corrupted in the middle # the server will still exit with an error. This option only applies when # Redis will try to read more data from the AOF file but not enough bytes # will be found. aof-load-truncated yes # When rewriting the AOF file, Redis is able to use an RDB preamble in the # AOF file for faster rewrites and recoveries. When this option is turned # on the rewritten AOF file is composed of two different stanzas: # # [RDB file][AOF tail] # # When loading Redis recognizes that the AOF file starts with the "REDIS" # string and loads the prefixed RDB file, and continues loading the AOF # tail. aof-use-rdb-preamble yes
相同數據集的數據要遠大於rdb文件,恢復速度較慢,所以導致aof的運行效率較慢。
AOF總結:
1.RDB持久化方式能夠在指定的時間間隔內對數據進行快照存儲
2.AOF持久化操作記錄每次對數據的寫的操作,當服務器重啟的時候就會重新執行這些命令來恢復原始的數據。AOF命令還能以Redis追加協議追加保存每次寫的操作到文件的末尾。
3.當兩種持久化方式都出現的時候,先加載aof文件恢復原始數據。因為AOF文件保存的數據要比RDB文件來的完整。建議不要只使用AOF
4.性能推薦:
因為RDB只用作后備用途,建議只在Slave上保存RDB文件,而且只要15分鍾備份一次就夠了。只保留save 900 1這條命令就夠了。
如果Enable AOF,好處是在最壞的情況下不會丟失超過兩秒的數據,啟動腳本比較簡單,只要load自己的aof文件就行了。代價一是帶來了持續的IO,二是AOF rewrite的最后將rewrite過程中產生的新數據寫入到新文件中造成的阻塞幾乎是不可避免的。只要硬盤許可,應該盡量減少AOF rewrite的頻率,AOF重寫的基礎大小是64M,太小了,可以設置到5G以上。默認超過原大小的100%時就會觸發重寫機制。
如果不Enable AOF,只靠Master-slave Replication實現高可用性也可以。能省掉一大筆IO也減少了rewrite時帶來的系統波動。代價是如果Master Slave同時倒掉,會丟失十幾分鍾的數據;載入腳本也要比較Master和Slave的RDB文件,加載較新的那個。