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文件,加载较新的那个。