Redis配置文件逐行详解!
- 1.文件大小定义和大小写
- 2.包含配置文件
- 3.网络相关
- (1)bind默认是127.0.0.1,如果需要同局域网下的其他主机连接到该redis,需要绑定本机在局域网中的地址,或者可以 0.0.0.0(不推荐)
- (2)protected-mode保护模式,默认开启。port端口,默认6379
- 4.通用配置
- (1)daemonize 默认是no,需要手动设置为yes。使redis以守护进程的方式后台运行
- (2)pidfile:如果以守护进程的方式后台运行需要指定pid。在集群时,如果修改了port,则也要修改对应的pidfile名.
- (3)databases:数据库的数量,默认16个。always-show-logo:是否开启日志logo
- 5.快照
- (1)在规定时间内执行多少次操作,则会持久化到文件 .rdb .aof。因为redis是内存数据库,如果不进行持久化操作,会断电即失。同样也是因为在内存中,存取速度才会那么优越
- (2)stop-writes-on-bgsave-error:持久化如果出错是否继续工作。
- 6.主从复制部分(重要)
- (1)简介
- (2)密码校验
- (3)复制时从服务器设置
- (4)设置从服务器只读
- (5)是否开启无盘复制
- (6)无盘复制延迟设置
- (7)从服务器心跳
- (8)从服务器超时
- (9)从服务器后续同步设置
- (10)积压限制
- (11)积压释放时间
- (12)从服务器权重
- (13)从服务器健康状态设置
- 7.安全
- (1)密码设置
- (2)内存到达上限时的处理策略
- 8.aof配置
1.文件大小定义和大小写
配置文件说明了文件大小的定义,以及对单位大小写不敏感
2.包含配置文件
可以包含其他的配置文件,类似于Nginx中的配置文件,根据项目的不同区分出来不同的文件,避免配置文件的过大和繁杂
3.网络相关
(1)bind默认是127.0.0.1,如果需要同局域网下的其他主机连接到该redis,需要绑定本机在局域网中的地址,或者可以 0.0.0.0(不推荐)
(2)protected-mode保护模式,默认开启。port端口,默认6379
4.通用配置
(1)daemonize 默认是no,需要手动设置为yes。使redis以守护进程的方式后台运行
(2)pidfile:如果以守护进程的方式后台运行需要指定pid。在集群时,如果修改了port,则也要修改对应的pidfile名.
loglevel:日志级别
logfile:日志输出文件地址,为空表示默认标准输出
(3)databases:数据库的数量,默认16个。always-show-logo:是否开启日志logo
5.快照
(1)在规定时间内执行多少次操作,则会持久化到文件 .rdb .aof。因为redis是内存数据库,如果不进行持久化操作,会断电即失。同样也是因为在内存中,存取速度才会那么优越
如果在3600s内有key被修改,则进行持久化操作
(2)stop-writes-on-bgsave-error:持久化如果出错是否继续工作。
# rdbcompression:是否压缩rdb文件
# rdbchecksum yes:保存时是否会进行错误检验
#dir ./:rdb文件保存的目录
6.主从复制部分(重要)
(1)简介
# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# +------------------+ +---------------+
# | Master | ---> | Replica |
# | (receive writes) | | (exact copy) |
# +------------------+ +---------------+
#
# 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 replicas.
# 2) Redis replicas 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 replicas automatically try to reconnect to masters
# and resynchronize with them.
#
# replicaof <masterip> <masterport>
主从复制。 使用copyof来使Redis实例成为另一个Redis服务器的副本。 尽快了解有关Redis复制的几件事Redis复制是异步的,但是您可以配置一个主服务器以停止接收写操作,如果它似乎没有与给定数量的副本连接。如果复制链接在相对较短的时间内丢失,则Redis副本能够与主副本执行部分重新同步。 您可能需要根据需要将复制积压大小(请参阅此文件的下一部分)配置为合理的值。复制是自动的,不需要用户干预。 网络分区副本之后,副本会自动尝试重新连接到母版并与它们重新同步。
(2)密码校验
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>
如果主服务器受密码保护(使用下面的“ requirepass”配置指令),则可以在开始复制同步过程之前告诉副本服务器进行身份验证,否则主服务器将拒绝副本请求。
(3)复制时从服务器设置
When masteruser is specified, the replica will authenticate against its
# master using the new AUTH form: AUTH <username> <password>.
# When a replica loses its connection with the master, or when the replication
# is still in progress, the replica can act in two different ways:
#
# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will
# still reply to client requests, possibly with out of date data, or the
# data set may just be empty if this is the first synchronization.
#
# 2) If replica-serve-stale-data is set to 'no' the replica will reply with
# an error "SYNC with master in progress" to all commands except:
# INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE,
# UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST,
# HOST and LATENCY.
#
replica-serve-stale-data yes
当副本失去与主数据库的连接时,或者仍在进行复制时,副本可以以两种不同的方式起作用:如果复制副本服务过时数据设置为“是”(默认值),则复制副本仍将回复客户端请求,可能包含过期数据,或者如果这是第一次同步,则数据集可能只是空的。如果复制副本服务过时的数据设置为“否”,则复制副本将对所有类型的命令除了信息,复制副本,授权,PING,关机,REPLCONF,角色, 配置,订阅,取消订阅,PSUBSCRIBE PUNSUBSCRIBE,发布,PUBSUB,COMMAND,POST,HOST:和延迟。
就是说主从复制过程中或者与服务器断开连接的时候,如果配置项设置成为yes,那么从服务器还能正常使用,但是数据不一定准确,如果配置项设置成no,那么除了系统的基本命令从服务器将不能使用。
(4)设置从服务器只读
# You can configure a replica instance to accept writes or not. Writing against
# a replica instance may be useful to store some ephemeral data (because data
# written on a replica will be easily deleted after resync with the master) but
# may also cause problems if clients are writing to it because of a
# misconfiguration.
#
# Since Redis 2.6 by default replicas are read-only.
#
# Note: read only replicas are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only replica exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
# security of read only replicas using 'rename-command' to shadow all the
# administrative / dangerous commands.
replica-read-only yes
您可以配置副本实例以接受或不接受写入。 针对副本实例进行拒绝写操作可能对存储某些临时数据很有用(因为与主实例重新同步后,写入副本上的数据将很容易删除),但是如果客户端由于配置错误而向其进行写操作,也可能导致问题。自从Redis 2.6默认情况下副本是只读的
注:由于现有项目中都是部署多个redis服务器进行集群,因此在读写分离时,往往主服务器进行写操作,从服务器进行读操作
(5)是否开启无盘复制
# Replication SYNC strategy: disk or socket.
#
# New replicas and reconnecting replicas that are not able to continue the
# replication process just receiving differences, need to do what is called a
# "full synchronization". An RDB file is transmitted from the master to the
# replicas.
#
# The transmission can happen in two different ways:
#
# 1) Disk-backed: The Redis master creates a new process that writes the RDB
# file on disk. Later the file is transferred by the parent
# process to the replicas incrementally.
# 2) Diskless: The Redis master creates a new process that directly writes the
# RDB file to replica sockets, without touching the disk at all.
#
# With disk-backed replication, while the RDB file is generated, more replicas
# can be queued and served with the RDB file as soon as the current child
# producing the RDB file finishes its work. With diskless replication instead
# once the transfer starts, new replicas arriving will be queued and a new
# transfer will start when the current one terminates.
#
# When diskless replication is used, the master waits a configurable amount of
# time (in seconds) before starting the transfer in the hope that multiple
# replicas will arrive and the transfer can be parallelized.
#
# With slow disks and fast (large bandwidth) networks, diskless replication
# works better.
repl-diskless-sync no
# When diskless replication is enabled, it is possible to configure the delay
# the server waits in order to spawn the child that transfers the RDB via socket
# to the replicas.
#
# This is important since once the transfer starts, it is not possible to serve
# new replicas arriving, that will be queued for the next RDB transfer, so the
# server waits a delay in order to let more replicas arrive.
#
# The delay is specified in seconds, and by default is 5 seconds. To disable
# it entirely just set it to 0 seconds and the transfer will start ASAP.
repl-diskless-sync-delay 5
RDB文件从主数据库传输到副本数据库。 传输可以通过两种不同的方式进行:1)Redis主服务器创建一个新过程,将RDB文件写入磁盘。 后来,该文件由父进程逐步传输到副本。2)Redis主服务器创建一个新进程,该进程将RDB文件直接写入副本套接字,而完全不接触磁盘。使用磁盘支持的复制,在生成RDB文件的同时,只要生成RDB文件的当前子级完成工作,就可以将更多副本排入队列并与RDB文件一起使用。 如果使用无盘复制,则一旦传输开始,新的副本将排队,并且当当前副本终止时将开始新的传输。使用无盘复制时,主服务器在开始传输之前会等待一段可配置的时间(以秒为单位),以希望多个副本可以到达并且传输可以并行化。对于慢速磁盘和快速(大带宽)网络,无盘复制效果更好。
(6)无盘复制延迟设置
# When diskless replication is enabled, it is possible to configure the delay
# the server waits in order to spawn the child that transfers the RDB via socket
# to the replicas.
#
# This is important since once the transfer starts, it is not possible to serve
# new replicas arriving, that will be queued for the next RDB transfer, so the
# server waits a delay in order to let more replicas arrive.
#
# The delay is specified in seconds, and by default is 5 seconds. To disable
# it entirely just set it to 0 seconds and the transfer will start ASAP.
repl-diskless-sync-delay 5
启用无盘复制后,可以配置服务器等待的延迟,以生成通过套接字将RDB传输到副本的子代。这一点很重要,因为一旦传输开始,就无法为到达的新副本提供服务,新副本将排队等待下一次RDB传输,因此服务器会等待一段时间才能让更多副本到达。延迟以秒为单位指定,默认情况下为5秒。 要完全禁用它,只需将其设置为0秒,传输就会尽快开始。
(7)从服务器心跳
# Replicas send PINGs to server in a predefined interval. It's possible to
# change this interval with the repl_ping_replica_period option. The default
# value is 10 seconds.
#
# repl-ping-replica-period 10
副本以预定义的时间间隔将PING发送到服务器。 可以使用repl_ping_replica_period选项更改此间隔。 默认值为10秒.
SLAVE周期性的ping MASTER间隔,可直接理解成SLAVE -> MASTER间的心跳间隔
注:在集群中或者哨兵模式中,需要主服务器和从服务器定时发送心跳,判断是否服务可用
(8)从服务器超时
# The following option sets the replication timeout for:
#
# 1) Bulk transfer I/O during SYNC, from the point of view of replica.
# 2) Master timeout from the point of view of replicas (data, pings).
# 3) Replica timeout from the point of view of masters (REPLCONF ACK pings).
#
# It is important to make sure that this value is greater than the value
# specified for repl-ping-replica-period otherwise a timeout will be detected
# every time there is low traffic between the master and the replica. The default
# value is 60 seconds.
#
# repl-timeout 60
以下选项设置了复制超时
1)从副本的角度来看,在SYNC期间进行批量传输I / O。
2)从副本(数据,Ping)的角度来看,主超时。
3)从主服务器角度来看,副本超时(REPLCONF ACK ping)。
重要的是要确保此值大于为repl-ping-replica-period指定的值,否则,每当主服务器和副本之间的通信量较低时,就会检测到超时。
上面设置了心跳,这个配置就是从服务的超时时间,当客户使用较少的时候一定要设置超时时间大于心跳时间.
(9)从服务器后续同步设置
# Disable TCP_NODELAY on the replica socket after SYNC?
#
# If you select "yes" Redis will use a smaller number of TCP packets and
# less bandwidth to send data to replicas. But this can add a delay for
# the data to appear on the replica side, up to 40 milliseconds with
# Linux kernels using a default configuration.
#
# If you select "no" the delay for data to appear on the replica side will
# be reduced but more bandwidth will be used for replication.
#
# By default we optimize for low latency, but in very high traffic conditions
# or when the master and replicas are many hops away, turning this to "yes" may
# be a good idea.
repl-disable-tcp-nodelay no
在同步后禁用副本套接字上的TCP_NODELAY?如果选择“是”,则Redis将使用更少的TCP数据包和更少的带宽将数据发送到副本。 但这会增加数据出现在副本端的延迟,对于使用默认配置的Linux内核,此延迟最多可达40毫秒。如果选择“否”,则将减少数据在副本端出现的延迟,但是将使用更多带宽进行复制。默认情况下,我们针对低延迟进行优化,但是在流量非常高的情况下,或者当主服务器和副本距离很多跳时,将其设置为“是”可能是个好主意。
(10)积压限制
# Set the replication backlog size. The backlog is a buffer that accumulates
# replica data when replicas are disconnected for some time, so that when a
# replica wants to reconnect again, often a full resync is not needed, but a
# partial resync is enough, just passing the portion of data the replica
# missed while disconnected.
#
# The bigger the replication backlog, the longer the replica can endure the
# disconnect and later be able to perform a partial resynchronization.
#
# The backlog is only allocated if there is at least one replica connected.
#
# repl-backlog-size 1mb
设置复制积压大小。 待办事项是一个缓冲区,当副本断开连接一段时间后,该缓冲区会累积副本数据,因此当副本想要再次重新连接时,通常不需要完全重新同步,但是部分重新同步就足够了,只需传递副本的部分数据即可 断开连接时错过。复制积压越大,副本可以断开连接并稍后能够执行部分重新同步的时间越长。仅当至少有一个副本连接时,才分配积压
(11)积压释放时间
# After a master has no connected replicas for some time, the backlog will be
# freed. The following option configures the amount of seconds that need to
# elapse, starting from the time the last replica disconnected, for the backlog
# buffer to be freed.
#
# Note that replicas never free the backlog for timeout, since they may be
# promoted to masters later, and should be able to correctly "partially
# resynchronize" with other replicas: hence they should always accumulate backlog.
#
# A value of 0 means to never release the backlog.
#
# repl-backlog-ttl 3600
主服务器在一段时间内不再连接副本后,积压的事务将被释放。 以下选项配置了从断开最后一个副本的时间开始到释放待办事项缓冲区所需的秒数。请注意,副本永远不会释放积压的超时,因为它们可能会在以后升级为主副本,并且应该能够与副本正确“部分重新同步”:因此,它们应该始终累积积压。值为0表示永不释放积压
(12)从服务器权重
# The replica priority is an integer number published by Redis in the INFO
# output. It is used by Redis Sentinel in order to select a replica to promote
# into a master if the master is no longer working correctly.
#
# A replica with a low priority number is considered better for promotion, so
# for instance if there are three replicas with priority 10, 100, 25 Sentinel
# will pick the one with priority 10, that is the lowest.
#
# However a special priority of 0 marks the replica as not able to perform the
# role of master, so a replica with priority of 0 will never be selected by
# Redis Sentinel for promotion.
#
# By default the priority is 100.
replica-priority 100
副本优先级是Redis在INFO输出中发布的整数。 如果主服务器不再正常工作,Redis Sentinel会使用它来选择要升级为主服务器的副本。具有较低优先级编号的副本被认为更适合提升,因此,例如,如果存在三个具有10、100、25优先级的副本,Sentinel将选择具有最低优先级10的副本。但是,特殊优先级0会将副本标记为不能执行主角色,因此Redis Sentinel永远不会选择优先级为0的副本进行升级。
注:在哨兵模式中,权重的大小影响着谁被选举成为主服务器
(13)从服务器健康状态设置
# It is possible for a master to stop accepting writes if there are less than
# N replicas connected, having a lag less or equal than M seconds.
#
# The N replicas need to be in "online" state.
#
# The lag in seconds, that must be <= the specified value, is calculated from
# the last ping received from the replica, that is usually sent every second.
#
# This option does not GUARANTEE that N replicas will accept the write, but
# will limit the window of exposure for lost writes in case not enough replicas
# are available, to the specified number of seconds.
#
# For example to require at least 3 replicas with a lag <= 10 seconds use:
#
# min-replicas-to-write 3
# min-replicas-max-lag 10
#
# Setting one or the other to 0 disables the feature.
#
# By default min-replicas-to-write is set to 0 (feature disabled) and
# min-replicas-max-lag is set to 10.
7.安全
(1)密码设置
通过命令设置密码
(2)内存到达上限时的处理策略
如果内存满了后的处理策略:
LRU算法:是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰
1. volatile-lru:只对设置了过期时间的key进行LRU(默认值)
2. allkeys-lru:对所有的key执行LRU
3. volatile-random:随机删除即将过期的key
4. allkeys-random:随机删除
5. volatile-ttl:删除即将过期的
6. noeviction:永不过期,返回错误
8.aof配置
appendonly no :默认不开启aof模式,因为默认使用rdb方式持久化,大部分场景下rdb足够
appendfilename :文件名
# appendfsync always //每次修改都会sync,消耗性能
appendfsync everysec //每秒执行一次sync,可能会丢失1s的数据
# appendfsync no //不执行sync,由操作系统自己同步数据,速度最快