Redis的配置文件位於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. # # 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 # 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有兩個好處:檢測掛掉的對端。下降中間設備出問題而網絡
# 致使網絡看似鏈接卻已經與對端端口的問題。session
# 在Linux內核中,設置了keepalive,redis會定時給對端發送ack。檢測到對端關閉須要兩倍的設置值。併發
# 此選項的合理值是300秒,這是從Redis 3.2.1開始的新Redis默認值app
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文件,加載較新的那個。