Redis配置文件详解(上)

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: Redis配置文件详解

是Redis版本:6.0.10


redis.conf 默认路径:/opt/redis-6.0.10 路径下(如果为自定制安装,则在您设置的redis路径下)


还记得我刚入行的时候,我的师傅就经常告诉我们配置比开发更重要,因为很多时候就是由于配置不当,而造成后期的难以预想的各种问题,以至于项目难以维护等等由于配置。进入一个公司首先的也是看相关项目的文档。so,如果需要对于redis有更深入的了解,与使用配置文件不可不读。为CURD,但不止于CURD。那么接下来我们对于redis.conf有个了解,话不多说,开干。


前言INCLUDESMODULESNETWORK(重要)TLS/SSLGENERALSNAPSHOTTING(快照,重要!!!)REPLICATION(主从复制)KEYS TRACKINGSECURITY(重要)CLIENTSMEMORY MANAGEMENTLAZY FREEINGTHREADED I/OKERNEL OOM CONTROLAPPEND ONLY MODE(重要)LUA SCRIPTINGREDIS CLUSTER(重要)CLUSTER DOCKER/NAT supportSLOW LOGLATENCY MONITOREVENT NOTIFICATIONGOPHER SERVERADVANCED CONFIGACTIVE DEFRAGMENTATION


前言


1Redis configuration file example.
 2
 3Note that in order to read the configuration file, Redis must be
 4started with the file path as first argument:
 5
 6./redis-server /path/to/redis.conf
 7
 8Note on units: when memory size is needed, it is possible to specify
 9it in the usual form of 1k 5GB 4M and so forth:
10
111k => 1000 bytes
121kb => 1024 bytes
131m => 1000000 bytes
141mb => 1024*1024 bytes
151g => 1000000000 bytes
161gb => 1024*1024*1024 bytes
17
18units are case insensitive so 1GB 1Gb 1gB are all the same.


Redis启动必须指定配置文件路径(如果没有则使用默认的配置文件),


说明如果我们把默认的配置文件给删除了,是无法启动redis的。


同理既然可以指定路径,那么我们也可以参考默认配置文件,定制化配置Redis


需要使用内存大小时,可以指定单位,通常是以 k,gb,m的形式出现,并且单位不区分大小写


仔细看你会发现它只支持bytes类型,不支持`bit等类型


INCLUDES


1Include one or more other config files here.  This is useful if you
 2have a standard template that goes to all Redis servers but also need
 3to customize a few per-server settings.  Include files can include
 4other files, so use this wisely.
 5# 在此处包括一个或多个其他配置文件。如果您具有可用于所有Redis服务器的标准模板,但还需要自定义一些每台服务器设置,则此功能很有用。包含文件可以包含其他文件,因此请明智地使用它
 6Note that option "include" won't be rewritten by command "CONFIG REWRITE"
 7from admin or Redis Sentinel. Since Redis always uses the last processed
 8line as value of a configuration directive, you'd better put includes
 9at the beginning of this file to avoid overwriting config change at runtime.
10# 请注意,选项“ include”将不会被admin或Redis Sentinel中的命令“ CONFIG REWRITE”重写。由于Redis始终使用最后处理的行作为配置指令的值,因此最好将include放在此文件的开头,以避免在运行时覆盖配置更改
11If instead you are interested in using includes to override configuration
12options, it is better to use include as the last line.
13# 相反,如果您有兴趣使用include覆盖配置选项,则最好使用include作为最后一行
14include /path/to/local.conf
15include /path/to/other.conf


我们知道Redis只有一个配置文件,如果多个人进行开发维护,那么就需要多个这样的配置文件,这时候多个配置文件就可以在此通过 include /path/to/local.conf 配置进来,而原本的 redis.conf 配置文件就作为一个总闸。


另外需要注意的时,如果将此配置写在redis.conf 文件的开头,那么后面的配置会覆盖引入文件的配置,如果想以引入文件的配置为主,那么需要将 include 配置写在 redis.conf 文件的末尾。


MODULES


1Load modules at startup. If the server is not able to load modules
2it will abort. It is possible to use multiple loadmodule directives.
3
4loadmodule /path/to/my_module.so
5loadmodule /path/to/other_module.so


通过这里的 loadmodule 配置将引入自定义模块来新增一些功能。


NETWORK(重要)


1By default, if no "bind" configuration directive is specified, Redis listens
 2for connections from all available network interfaces on the host machine.
 3It is possible to listen to just one or multiple selected interfaces using
 4the "bind" configuration directive, followed by one or more IP addresses.
 5# 默认情况下,如果未指定“ bind”配置指令,则Redis侦听主机上所有可用网络接口的连接。可以使用“ bind”配置指令仅侦听一个或多个所选接口,然后侦听一个或多个IP地址
 6Examples:
 7
 8bind 192.168.1.100 10.0.0.1
 9bind 127.0.0.1 ::1
10
11~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
12internet, binding to all the interfaces is dangerous and will expose the
13instance to everybody on the internet. So by default we uncomment the
14following bind directive, that will force Redis to listen only on the
15IPv4 loopback interface address (this means Redis will only be able to
16accept client connections from the same host that it is running on).
17# ~~~警告~~~如果运行Redis的计算机直接暴露于Internet,则绑定到所有接口都是很危险的,并且会将实例暴露给Internet上的所有人。因此,默认情况下,我们取消注释以下bind指令,这将强制Redis仅在IPv4环回接口地址上侦听(这意味着Redis将只能接受来自其运行所在主机的客户端连接)
18IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
19JUST COMMENT OUT THE FOLLOWING LINE.
20# 如果您确定要立即侦听所有接口,只需在后续行中注明即可。
21~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22bind 127.0.0.1 ::1
23
24
25
26Protected mode is a layer of security protection, in order to avoid that
27Redis instances left open on the internet are accessed and exploited.
28# 保护模式是安全保护的一层,目的是避免访问和利用Internet上打开的Redis实例。
29When protected mode is on and if:
30# 当保护模式开启时,如果
311) The server is not binding explicitly to a set of addresses using the
32"bind" directive.
33# 服务器未使用“ bind”指令显式绑定到一组地址
342) No password is configured.
35# 没有配置密码
36The server only accepts connections from clients connecting from the
37IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
38sockets.
39# 服务器仅接受来自客户端的连接,这些客户端从IPv4和IPv6回送地址127.0.0.1和:: 1以及Unix域套接字连接


bind:绑定redis服务器网卡IP,默认为127.0.0.1,即本地回环地址。这样的话,访问redis服务只能通过本机的客户端连接,而无法通过远程连接。如果bind选项为空的话,那会接受所有来自于可用网络接口的连接。


同时需要注意的是 如果注释掉bind后面的内容,运行Redis的计算机将直接暴露于在internet上,绑定到所有接口是危险的,并且会暴露向互联网上的每个人提供实例。需谨慎选择


至少也得加个密码(见下文)


1By default protected mode is enabled. You should disable it only if
2you are sure you want clients from other hosts to connect to Redis
3even if no authentication is configured, nor a specific set of interfaces
4are explicitly listed using the "bind" directive.
5# 默认情况下启用保护模式。仅当您确定您希望其他主机的客户端连接到Redis时,即使未配置身份验证,也不要使用“ bind”指令显式列出一组特定的接口,才应禁用它
6protected-mode yes


默认情况下,启用保护模式。只有当您确定希望来自其他主机的客户端连接到Redis时才应该禁用它,即使没有配置身份验证,也没有使用“bind”指令显式列出一组特定的接口。


1Accept connections on the specified port, default is 6379 (IANA 815344).
2If port 0 is specified Redis will not listen on a TCP socket.
3# 接受指定端口上的连接,默认为6379(IANA 815344)。如果指定了端口0,则Redis将不会在TCP套接字上侦听
4port 6379


接受指定端口上的连接,默认值为6379。如果指定了端口0,Redis将不会侦听TCP套接字。由于Redis是单线程模型,因此单机开多个Redis进程的时候需要修改端口。


1TCP listen() backlog.
 2
 3In high requests-per-second environments you need a high backlog in order
 4to avoid slow clients connection issues. Note that the Linux kernel
 5will silently truncate it to the value of /proc/sys/net/core/somaxconn so
 6make sure to raise both the value of somaxconn and tcp_max_syn_backlog
 7in order to get the desired effect.
 8tcp-backlog 511
 9
10在每秒高请求的环境中,您需要一个高积压工作,以避免客户端连接速度慢的问题。请注意,Linux内核将静默地将其截断为/proc/sys/net/core/somaxconn so的值
11确保同时提高somaxconn和tcp\u max\u syn\u backlog的值以获得所需的效果


默认值 511


tcp-backlog:511


此参数确定了TCP连接中已完成队列(完成三次握手之后)的长度, 当然此值必须不大于Linux系统定义的/proc/sys/net/core/somaxconn值,默认是511,而Linux的默认参数值是128。当系统并发量大并且客户端速度缓慢的时候,可以将这二个参数一起参考设定。


建议修改为 2048


修改somaxconn


该内核参数默认值一般是128,对于负载很大的服务程序来说大大的不够。一般会将它修改为2048或者更大。


echo 2048 > /proc/sys/net/core/somaxconn 但是这样系统重启后保存不了


在/etc/sysctl.conf中添加如下


net.core.somaxconn = 2048


然后在终端中执行


sysctl -p


1Unix socket.
2
3Specify the path for the Unix socket that will be used to listen for
4incoming connections. There is no default, so Redis will not listen
5on a unix socket when not specified.
6
7unixsocket /tmp/redis.sock
8unixsocketperm 700


指定用于侦听传入连接的Unix套接字的路径。没有默认值,因此Redis在未指定时不会侦听unix套接字。


1Close the connection after a client is idle for N seconds (0 to disable)
2timeout 0


客户端空闲N秒后关闭连接(0表示禁用)


1TCP keepalive.
 2
 3If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
 4of communication. This is useful for two reasons:
 5# 如果不为零,请在没有通信的情况下使用SO_KEEPALIVE向客户端发送TCP ACK。这很有用,有两个原因:
 61) Detect dead peers.   # 检测死者
 72) Force network equipment in the middle to consider the connection to be
 8alive.                  # 强制中间的网络设备考虑连接处于活动状态。
 9
10On Linux, the specified value (in seconds) is the period used to send ACKs.
11Note that to close the connection the double of the time is needed.
12On other kernels the period depends on the kernel configuration.
13# 在Linux上,指定的值(以秒为单位)是用于发送ACK的时间段。请注意,关闭连接需要两倍的时间。在其他内核上,周期取决于内核配置
14A reasonable value for this option is 300 seconds, which is the new
15Redis default starting with Redis 3.2.1.
16# 此选项的合理值是300秒,这是从Redis 3.2.1开始的新Redis默认值。
17tcp-keepalive 300


TCP保持连接。


如果非零,则在没有通信的情况下,使用SO_KEEPALIVE向客户端发送TCP确认。这有两个原因:


1) 检测死掉的同伴。


2) 强制中间的网络设备认为连接是活动的。


在Linux上,指定的值(以秒为单位)是用于发送ACK的时间段。请注意,要关闭连接,需要两倍的时间。


在其他内核上,周期取决于内核配置。


这个选项的合理值是300秒,这是从redis3.2.1开始的新Redis默认值。


tcp保持300


TLS/SSL


1By default, TLS/SSL is disabled. To enable it, the "tls-port" configuration
2directive can be used to define TLS-listening ports. To enable TLS on the
3default port, use:
4# 默认情况下,TLSSSL被禁用。要启用它,可以使用“ tls-port”配置指令来定义TLS侦听端口。要在默认端口上启用TLS,请使用
5port 0
6tls-port 6379


默认情况下,TLS/SSL处于禁用状态。要启用它,“tls端口”配置


指令可用于定义TLS侦听端口。


1Configure a X.509 certificate and private key to use for authenticating the
 2server to connected clients, masters or cluster peers.  These files should be
 3PEM formatted.
 4# 配置X.509证书和私钥,用于对连接的客户端,主服务器或集群对等服务器进行身份验证。这些文件应为PEM格式
 5tls-cert-file redis.crt 
 6tls-key-file redis.key
 7
 8Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange:
 9
10tls-dh-params-file redis.dh
11
12Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL
13clients and peers.  Redis requires an explicit configuration of at least one
14of these, and will not implicitly use the system wide configuration.
15
16tls-ca-cert-file ca.crt
17tls-ca-cert-dir /etc/ssl/certs
18
19By default, clients (including replica servers) on a TLS port are required
20to authenticate using valid client side certificates.
21# 默认情况下,要求TLS端口上的客户端(包括副本服务器)使用有效的客户端证书进行身份验证
22If "no" is specified, client certificates are not required and not accepted.
23If "optional" is specified, client certificates are accepted and must be
24valid if provided, but are not required.
25# 如果指定“否”,则不需要也不接受客户端证书。如果指定了“可选”,则接受客户端证书,并且如果提供的话,客户端证书必须有效,但不是必需的
26tls-auth-clients no
27tls-auth-clients optional
28
29By default, a Redis replica does not attempt to establish a TLS connection
30with its master.
31# 默认情况下,Redis副本不会尝试与其主服务器建立TLS连接
32Use the following directive to enable TLS on replication links.
33# 使用以下指令在复制链接上启用TLS
34tls-replication yes
35
36By default, the Redis Cluster bus uses a plain TCP connection. To enable
37TLS for the bus protocol, use the following directive:
38
39tls-cluster yes
40
41Explicitly specify TLS versions to support. Allowed values are case insensitive
42and include "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" (OpenSSL >= 1.1.1) or
43any combination. To enable only TLSv1.2 and TLSv1.3, use:
44
45tls-protocols "TLSv1.2 TLSv1.3"
46
47Configure allowed ciphers.  See the ciphers(1ssl) manpage for more information
48about the syntax of this string.
49
50Note: this configuration applies only to <= TLSv1.2.
51
52tls-ciphers DEFAULT:!MEDIUM
53
54Configure allowed TLSv1.3 ciphersuites.  See the ciphers(1ssl) manpage for more
55information about the syntax of this string, and specifically for TLSv1.3
56ciphersuites.
57
58tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256
59
60When choosing a cipher, use the server's preference instead of the client
61preference. By default, the server follows the client's preference.
62
63tls-prefer-server-ciphers yes
64
65By default, TLS session caching is enabled to allow faster and less expensive
66reconnections by clients that support it. Use the following directive to disable
67caching.
68
69tls-session-caching no
70
71Change the default number of TLS sessions cached. A zero value sets the cache
72to unlimited size. The default size is 20480.
73
74tls-session-cache-size 5000
75
76Change the default timeout of cached TLS sessions. The default timeout is 300
77seconds.
78
79tls-session-cache-timeout 60


GENERAL


1By default Redis does not run as a daemon. Use 'yes' if you need it.
2Note that Redis will write a pid file in /usr/local/var/run/redis.pid when daemonized.
3daemonize no


 daemonize:设置为yes表示指定Redis以守护进程的方式启动(即后台启动)。默认值为 no


1If you run Redis from upstart or systemd, Redis can interact with your
 2supervision tree. Options:
 3supervised no      - no supervision interaction
 4supervised upstart - signal upstart by putting Redis into SIGSTOP mode
 5                    requires "expect stop" in your upstart job config
 6supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
 7supervised auto    - detect upstart or systemd method based on
 8                    UPSTART_JOB or NOTIFY_SOCKET environment variables
 9Note: these supervision methods only signal "process is ready."
10   They do not enable continuous pings back to your supervisor.
11supervised no


如果你使用系统的upstart或者systemd运行redis。他们可以管理监控redis。默认不启用


参数


supervised no:- no supervision interaction(无监督-无监督互动)


supervised upstart - 监督upstart-通过将Redis置于SIGSTOP模式来发出upstart信号在upstart作业配置中需要“expect stop”


supervised systemd - 受监控的systemd-通过写入READY=1到$NOTIFY\u SOCKET发送信号systemd


supervised auto - 基于upstart\u JOB或NOTIFY\u SOCKET环境变量检测upstart或systemd方法


注意:这些监督方法仅表示“过程准备就绪”,它们不支持连续ping返回到您的主管。


1If a pid file is specified, Redis writes it where specified at startup
 2and removes it at exit.
 3
 4When the server runs non daemonized, no pid file is created if none is
 5specified in the configuration. When the server is daemonized, the pid file
 6is used even if not specified, defaulting to "/usr/local/var/run/redis.pid".
 7
 8Creating a pid file is best effort: if Redis is not able to create it
 9nothing bad happens, the server will start and run normally.
10pidfile /var/run/redis_6379.pid


 pidfile: 配置PID文件路径,当redis作为守护进程运行的时候,它会把 pid 默认写到

/var/redis/run/redis_6379.pid 文件里面


1Specify the server verbosity level.
 2This can be one of:
 3debug (a lot of information, useful for development/testing)
 4verbose (many rarely useful info, but not a mess like the debug level)
 5notice (moderately verbose, what you want in production probably)
 6warning (only very important / critical messages are logged)
 7loglevel notice
 8
 9Specify the log file name. Also the empty string can be used to force
10Redis to log on the standard output. Note that if you use standard
11output for logging but daemonize, logs will be sent to /dev/null
12logfile ""


 loglevel :定义日志级别。默认值为notice,有如下4种取值:


debug(大量信息,对开发/测试有用)


verbose(许多很少有用的信息,但不像调试级别那样混乱)


notice(适度冗长,可能是生产中需要的内容)


warning(只记录非常重要/关键的消息)


logfile :配置log文件地址,默认打印在命令行终端的窗口上


1To enable logging to the system logger, just set 'syslog-enabled' to yes,
 2and optionally update the other syslog parameters to suit your needs.
 3syslog-enabled no
 4
 5Specify the syslog identity.
 6syslog-ident redis
 7
 8Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
 9syslog-facility local0
10
11Set the number of databases. The default database is DB 0, you can select
12a different one on a per-connection basis using SELECT <dbid> where
13dbid is a number between 0 and 'databases'-1
14databases 16


databases:设置数据库的数目。默认的数据库是DB 0 ,可以在每个连接上使用select命令选择一个不同的数据库,dbid是一个介于0到databases - 1 之间的数值。默认值是 16,也就是说默认Redis有16个数据库。


1By default Redis shows an ASCII art logo only when started to log to the
2standard output and if the standard output is a TTY. Basically this means
3that normally a logo is displayed only in interactive sessions.
4
5However it is possible to force the pre-4.0 behavior and always show a
6ASCII art logo in startup logs by setting the following option to yes.
7always-show-logo yes


SNAPSHOTTING(快照,重要!!!)


1Save the DB on disk:
 2
 3save <seconds> <changes>
 4
 5Will save the DB if both the given number of seconds and the given
 6number of write operations against the DB occurred.
 7
 8In the example below the behavior will be to save:
 9after 900 sec (15 min) if at least 1 key changed
10after 300 sec (5 min) if at least 10 keys changed
11after 60 sec if at least 10000 keys changed
12
13Note: you can disable saving completely by commenting out all "save" lines.
14
15It is also possible to remove all the previously configured save
16points by adding a save directive with a single empty string argument
17like in the following example:
18
19save ""


save 900 1


save 300 10


save 60 10000


save:这里是用来配置触发 Redis的持久化条件,也就是什么时候将内存中的数据保存到硬盘。默认如下配置:


1save 900 1:表示900 秒内如果至少有 1 个 key 的值变化,则保存
2save 300 10:表示300 秒内如果至少有 10 个 key 的值变化,则保存
3save 60 10000:表示60 秒内如果至少有 10000 个 key 的值变化,则保存


1By default Redis will stop accepting writes if RDB snapshots are enabled
 2 (at least one save point) and the latest background save failed.
 3 This will make the user aware (in a hard way) that data is not persisting
 4 on disk properly, otherwise chances are that no one will notice and some
 5 disaster will happen.
 6
 7  If the background saving process will start working again Redis will
 8 automatically allow writes again.
 9
10  However if you have setup your proper monitoring of the Redis server
11 and persistence, you may want to disable this feature so that Redis will
12 continue to work as usual even if there are problems with disk,
13 permissions, and so forth.
14stop-writes-on-bgsave-error yes


默认情况下,如果启用RDB快照,Redis将停止接受写操作


(至少一个保存点)和最新的后台保存失败。


这将使用户意识到(以一种困难的方式)数据没有持久化


在磁盘上正确,否则很可能没有人会注意到和一些


灾难就会发生。


如果后台保存过程将重新开始工作,Redis将自动允许再次写入。但是,如果您已经设置了对Redis服务器的适当监视


和持久性,您可能希望禁用此功能,以便Redis


即使磁盘、权限等出现问题,也要继续正常工作。


1Compress string objects using LZF when dump .rdb databases?
2By default compression is enabled as it's almost always a win.
3If you want to save some CPU in the saving child set it to 'no' but
4the dataset will likely be bigger if you have compressible values or keys.
5rdbcompression yes


转储.rdb数据库时使用LZF压缩字符串对象?


默认情况下,压缩是启用的,因为它几乎总是一个胜利。


如果您想在保存子进程中保存一些CPU,请将其设置为“否”,但是


如果有可压缩的值或键,数据集可能会更大。


1Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
 2This makes the format more resistant to corruption but there is a performance
 3hit to pay (around 10%) when saving and loading RDB files, so you can disable it
 4for maximum performances.
 5
 6RDB files created with checksum disabled have a checksum of zero that will
 7tell the loading code to skip the check.
 8rdbchecksum yes
 9
10由于RDB版本5,CRC64校验和放在文件的末尾。这使格式更能抵抗损坏,但在保存和加载RDB文件时,性能会受到影响(约10%),因此可以禁用它以获得最大性能。在禁用校验和的情况下创建的RDB文件的校验和为零,这将告诉加载代码跳过检查。


1The filename where to dump the DB
2dbfilename dump.rdb
3
4将数据库转储到的文件名


1Remove RDB files used by replication in instances without persistence
 2enabled. By default this option is disabled, however there are environments
 3where for regulations or other security concerns, RDB files persisted on
 4disk by masters in order to feed replicas, or stored on disk by replicas
 5in order to load them for the initial synchronization, should be deleted
 6ASAP. Note that this option ONLY WORKS in instances that have both AOF
 7and RDB persistence disabled, otherwise is completely ignored.
 8
 9An alternative (and sometimes better) way to obtain the same effect is
10to use diskless replication on both master and replicas instances. However
11in the case of replicas, diskless is not always an option.
12rdb-del-sync-files no
13
14The working directory.
15
16The DB will be written inside this directory, with the filename specified
17above using the 'dbfilename' configuration directive.
18
19The Append Only File will also be created inside this directory.
20
21Note that you must specify a directory here, not a file name.
22
23dir /usr/local/var/db/redis/
24
25在没有持久性的实例中删除复制使用的RDB文件启用。默认情况下,此选项处于禁用状态,但是在某些环境中,出于管理法规或其他安全考虑,应尽快删除由主服务器保留在磁盘上以馈送副本的RDB文件,或由副本存储在磁盘上以加载它们以进行初始同步。请注意,此选项仅适用于同时禁用AOF和RDB持久性的实例,否则将完全忽略。
26另一种(有时更好)获得相同效果的方法是在主实例和副本实例上使用无盘复制。但是,对于副本,无磁盘并不是一种好的选择。
27rdb del同步文件编号
28工作目录。
29数据库将被写入这个目录,并指定文件名
30使用“dbfilename”配置指令。
31只附加的文件也将在这个目录中创建。
32请注意,必须在此处指定目录,而不是文件名。


 dbfilename :设置快照的文件名,默认是 dump.rdb


  ⑥、dir:设置快照文件的存放路径,这个配置项一定是个目录,而不能是文件名。使用上面的 dbfilename 作为保存的文件名。


REPLICATION(主从复制)


1REPLICATION 
  2
  3Master-Replica replication. Use replicaof to make a Redis instance a copy of
  4another Redis server. A few things to understand ASAP about Redis replication.
  5
  6+------------------+      +---------------+
  7|      Master      | ---> |    Replica    |
  8| (receive writes) |      |  (exact copy) |
  9+------------------+      +---------------+
 10
 111) Redis replication is asynchronous, but you can configure a master to
 12stop accepting writes if it appears to be not connected with at least
 13a given number of replicas.
 14# Redis复制是异步的,但是您可以配置一个主机,如果它看起来没有连接到至少给定数量的副本,那么它就停止接受写操作。
 152) Redis replicas are able to perform a partial resynchronization with the
 16master if the replication link is lost for a relatively small amount of
 17time. You may want to configure the replication backlog size (see the next
 18sections of this file) with a sensible value depending on your needs.
 19# 如果复制链路丢失的时间相对较短,Redis复制副本可以执行与主机的部分重新同步。您可能需要根据需要使用合理的值来配置复制积压工作大小(请参阅本文件的下一节)。
 203) Replication is automatic and does not need user intervention. After a
 21network partition replicas automatically try to reconnect to masters
 22and resynchronize with them.
 23# 复制是自动的,不需要用户干预。在网络分区之后,复制副本会自动尝试重新连接到主机并与它们重新同步。
 24
 25replicaof <masterip> <masterport>
 26
 27If the master is password protected (using the "requirepass" configuration
 28directive below) it is possible to tell the replica to authenticate before
 29starting the replication synchronization process, otherwise the master will
 30refuse the replica request.
 31# 如果主机受密码保护(使用下面的“requirepass”配置指令),则可以在启动复制同步过程之前通知复制副本进行身份验证,否则主机将拒绝副本请求。
 32
 33masterauth <master-password>
 34
 35However this is not enough if you are using Redis ACLs (for Redis version
 366 or greater), and the default user is not capable of running the PSYNC
 37command and/or other commands needed for replication. In this case it's
 38better to configure a special user to use with replication, and specify the
 39masteruser configuration as such:
 40# 但是,如果您正在使用Redis ACL(用于Redis版本6或更高版本),并且默认用户无法运行PSYNC命令和/或其他复制所需的命令,这还不够。在这种情况下,最好配置一个特殊用户以用于复制
 41masteruser <username>
 42
 43When masteruser is specified, the replica will authenticate against its
 44master using the new AUTH form: AUTH <username> <password>.
 45# 指定masteruser时,副本将使用新的AUTH表单针对其主服务器进行身份验证
 46When a replica loses its connection with the master, or when the replication
 47is still in progress, the replica can act in two different ways:
 48# 当副本失去与主数据库的连接时,或者仍在进行复制时,副本可以采取两种不同的方式进行操作
 491) if replica-serve-stale-data is set to 'yes' (the default) the replica will
 50still reply to client requests, possibly with out of date data, or the
 51data set may just be empty if this is the first synchronization.
 52# 如果复制副本服务过时数据设置为“是”(默认值),则复制副本仍将回复客户端请求,可能包含过期数据,或者如果这是第一次同步,则数据集可能只是空的。
 532) If replica-serve-stale-data is set to 'no' the replica will reply with
 54an error "SYNC with master in progress" to all commands except:
 55INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE,
 56UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST,
 57HOST and LATENCY.
 58# 如果将replica-serve-stale-data设置为“ no”,则该副本将对所有命令(“ INFO,REPLICAOF,AUTH,PING,SHUTDOWN,REPLCONF,ROLE,CONFIG,SUBSCRIBE)进行错误答复” ,退订,PSUBSCRIBE,PUNSUBSCRIBE,PUBLISH,PUBSUB,COMMAND,POST,HOST和LATENCY。
 59replica-serve-stale-data yes
 60
 61You can configure a replica instance to accept writes or not. Writing against
 62a replica instance may be useful to store some ephemeral data (because data
 63written on a replica will be easily deleted after resync with the master) but
 64may also cause problems if clients are writing to it because of a
 65misconfiguration.
 66# 您可以配置副本实例以接受或不接受写入。针对副本实例进行写操作可能对存储一些临时数据很有用(因为与主实例重新同步后,写入副本上的数据将很容易删除),但是如果客户端由于配置错误而向其进行写操作,也会导致问题。
 67Since Redis 2.6 by default replicas are read-only.
 68
 69Note: read only replicas are not designed to be exposed to untrusted clients
 70on the internet. It's just a protection layer against misuse of the instance.
 71Still a read only replica exports by default all the administrative commands
 72such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
 73security of read only replicas using 'rename-command' to shadow all the
 74administrative / dangerous commands.
 75# 只读副本并非旨在向Internet上不受信任的客户端公开。它只是防止实例滥用的保护层。默认情况下,只读副本仍会导出所有管理命令,例如CONFIG,DEBUG等。在一定程度上,您可以使用'rename-command'隐藏所有管理危险命令来提高只读副本的安全性
 76replica-read-only yes
 77Replication SYNC strategy: disk or socket.
 78
 79New replicas and reconnecting replicas that are not able to continue the
 80replication process just receiving differences, need to do what is called a
 81"full synchronization". An RDB file is transmitted from the master to the
 82replicas.
 83# 仅仅接受差异就无法继续复制过程的新副本和重新连接的副本需要进行所谓的“完全同步”。RDB文件从主数据库传输到副本数据库
 84The transmission can happen in two different ways:
 85
 861) Disk-backed: The Redis master creates a new process that writes the RDB
 87             file on disk. Later the file is transferred by the parent
 88             process to the replicas incrementally.
 892) Diskless: The Redis master creates a new process that directly writes the
 90          RDB file to replica sockets, without touching the disk at all.
 91
 92With disk-backed replication, while the RDB file is generated, more replicas
 93can be queued and served with the RDB file as soon as the current child
 94producing the RDB file finishes its work. With diskless replication instead
 95once the transfer starts, new replicas arriving will be queued and a new
 96transfer will start when the current one terminates.
 97# 使用磁盘支持的复制,当生成RDB文件时,只要生成RDB文件的当前子级完成工作,就可以将更多副本排入队列并与RDB文件一起使用。如果使用无盘复制,则一旦传输开始,新的副本将排队,并且当当前副本终止时将开始新的传输
 98When diskless replication is used, the master waits a configurable amount of
 99time (in seconds) before starting the transfer in the hope that multiple
100replicas will arrive and the transfer can be parallelized.
101# 使用无盘复制时,主服务器在开始传输之前等待一段可配置的时间(以秒为单位),以希望多个副本可以到达并且传输可以并行化
102With slow disks and fast (large bandwidth) networks, diskless replication
103works better.
104# 对于慢速磁盘和快速(大带宽)网络,无盘复制效果更好
105repl-diskless-sync no
106
107When diskless replication is enabled, it is possible to configure the delay
108the server waits in order to spawn the child that transfers the RDB via socket
109to the replicas.
110# 启用无盘复制后,可以配置服务器等待的延迟,以便生成通过套接字将RDB传输到副本的子代。
111This is important since once the transfer starts, it is not possible to serve
112new replicas arriving, that will be queued for the next RDB transfer, so the
113server waits a delay in order to let more replicas arrive.
114# 这一点很重要,因为一旦传输开始,就无法为到达下一个RDB传输的新副本提供服务,因此服务器会等待一段时间才能让更多副本到达。
115The delay is specified in seconds, and by default is 5 seconds. To disable
116it entirely just set it to 0 seconds and the transfer will start ASAP.
117# 延迟以秒为单位指定,默认情况下为5秒。要完全禁用它,只需将其设置为0秒,传输就会尽快开始。
118repl-diskless-sync-delay 5
119
120-----------------------------------------------------------------------------
121WARNING: RDB diskless load is experimental. Since in this setup the replica
122does not immediately store an RDB on disk, it may cause data loss during
123failovers. RDB diskless load + Redis modules not handling I/O reads may also
124cause Redis to abort in case of I/O errors during the initial synchronization
125stage with the master. Use only if your do what you are doing.
126-----------------------------------------------------------------------------
127# 警告:RDB无盘加载是实验性的。因为在此设置中,副本不会立即在磁盘上存储RDB,所以它可能会导致故障转移期间的数据丢失。在与主机的初始同步阶段,如果IO错误,则RDB无盘负载+ Redis模块不处理IO读取也可能导致Redis中止。仅在执行自己的操作时使用
128Replica can load the RDB it reads from the replication link directly from the
129socket, or store the RDB to a file and read that file after it was completely
130received from the master.
131# 副本可以直接从套接字加载从复制链接读取的RDB,也可以将RDB存储到文件中,并在从主服务器完全接收到该文件后读取该文件。
132In many cases the disk is slower than the network, and storing and loading
133the RDB file may increase replication time (and even increase the master's
134Copy on Write memory and salve buffers).
135However, parsing the RDB file directly from the socket may mean that we have
136to flush the contents of the current database before the full rdb was
137received. For this reason we have the following options:
138# 在许多情况下,磁盘的速度比网络慢,并且存储和加载RDB文件可能会增加复制时间(甚至会增加主服务器的“写时复制”内存和从属缓冲区)。但是,直接从套接字解析RDB文件可能意味着我们必须在收到完整的rdb之前刷新当前数据库的内容。因此,我们有以下选择
139"disabled"    - Don't use diskless load (store the rdb file to the disk first)
140# 不要使用无盘负载(首先将rdb文件存储到磁盘)
141"on-empty-db" - Use diskless load only when it is completely safe.
142# 仅在完全安全的情况下使用无盘加载
143"swapdb"      - Keep a copy of the current db contents in RAM while parsin 
144the data directly from the socket. note that this requires sufficient memory, if you don't have it, you risk an OOM kill.
145# 直接从套接字解析数据时,将当前数据库内容的副本保留在RAM中。请注意,这需要足够的内存,如果没有足够的内存,则可能会杀死OOM
146repl-diskless-load disabled
147
148Replicas send PINGs to server in a predefined interval. It's possible to
149change this interval with the repl_ping_replica_period option. The default
150value is 10 seconds.
151# 副本以预定义的时间间隔将PING发送到服务器。可以使用repl_ping_replica_period选项更改此间隔。默认值为10秒
152repl-ping-replica-period 10
153
154The following option sets the replication timeout for:
155# 以下选项设置了复制超时
1561) Bulk transfer I/O during SYNC, from the point of view of replica.
157# 从副本的角度来看,在SYNC期间进行批量传输IO。
1582) Master timeout from the point of view of replicas (data, pings).
159# 从副本(数据,ping)的角度来看主超时
1603) Replica timeout from the point of view of masters (REPLCONF ACK pings).
161# 从主服务器角度来看副本超时(REPLCONF ACK ping)
162It is important to make sure that this value is greater than the value
163specified for repl-ping-replica-period otherwise a timeout will be detected
164every time there is low traffic between the master and the replica. The default
165value is 60 seconds.
166# 重要的是要确保该值大于为repl-ping-replica-period指定的值,否则,每当主机和副本之间的通信量较低时,就会检测到超时。默认值为60秒。
167repl-timeout 60
168
169Disable TCP_NODELAY on the replica socket after SYNC?
170# 同步后在副本套接字上禁用TCP_NODELAY
171
172If you select "yes" Redis will use a smaller number of TCP packets and
173less bandwidth to send data to replicas. But this can add a delay for
174the data to appear on the replica side, up to 40 milliseconds with
175Linux kernels using a default configuration.
176# 如果选择“是”,则Redis将使用更少的TCP数据包和更少的带宽将数据发送到副本。但这会增加数据在副本端显示的延迟,对于使用默认配置的Linux内核,此延迟最多40毫秒
177If you select "no" the delay for data to appear on the replica side will
178be reduced but more bandwidth will be used for replication.
179# 如果选择“否”,则将减少数据在副本侧出现的延迟,但将使用更多带宽进行复制
180By default we optimize for low latency, but in very high traffic conditions
181or when the master and replicas are many hops away, turning this to "yes" may
182be a good idea.
183# 默认情况下,我们会针对低延迟进行优化,但是在流量非常高的情况下,或者当主服务器和副本距离很多跳时,将其设置为“是”可能是个好主意
184repl-disable-tcp-nodelay no
185
186Set the replication backlog size. The backlog is a buffer that accumulates
187replica data when replicas are disconnected for some time, so that when a
188replica wants to reconnect again, often a full resync is not needed, but a
189partial resync is enough, just passing the portion of data the replica
190missed while disconnected.
191# 设置复制积压大小。待办事项是一个缓冲区,当副本断开连接一段时间后,该缓冲区将累积副本数据,因此,当副本要重新连接时,通常不需要完全重新同步,但是部分重新同步就足够了,只需传递副本中的部分数据断开连接时错过
192The bigger the replication backlog, the longer the replica can endure the
193disconnect and later be able to perform a partial resynchronization.
194# 复制积压量越大,副本可以承受断开连接并随后能够执行部分重新同步的时间越长
195The backlog is only allocated if there is at least one replica connected.
196# 仅在连接至少一个副本时分配积压
197repl-backlog-size 1mb
198
199After a master has no connected replicas for some time, the backlog will be
200freed. The following option configures the amount of seconds that need to
201elapse, starting from the time the last replica disconnected, for the backlog
202buffer to be freed.
203# 主服务器在一段时间内没有连接的副本后,积压的订单将被释放。以下选项配置了从断开最后一个副本的时间开始,释放待办事项缓冲区所需的秒数
204Note that replicas never free the backlog for timeout, since they may be
205promoted to masters later, and should be able to correctly "partially
206resynchronize" with other replicas: hence they should always accumulate backlog.
207# 请注意,副本永远不会释放积压的超时,因为它们可能稍后会升级为主副本,并且应该能够与其他副本正确“部分重新同步”:因此,它们应始终累积积压。
208A value of 0 means to never release the backlog.
209# 值为0表示永不释放积压
210repl-backlog-ttl 3600
211
212The replica priority is an integer number published by Redis in the INFO
213output. It is used by Redis Sentinel in order to select a replica to promote
214into a master if the master is no longer working correctly.
215# 副本优先级是Redis在INFO输出中发布的整数。如果主服务器不再正常工作,Redis Sentinel会使用它来选择要升级为主服务器的副本
216A replica with a low priority number is considered better for promotion, so
217for instance if there are three replicas with priority 10, 100, 25 Sentinel
218will pick the one with priority 10, that is the lowest.
219# 优先级数字低的副本被认为更适合升级,例如,如果有三个副本的优先级分别为10、100和25,Sentinel将选择优先级为10的副本,这是最低的
220However a special priority of 0 marks the replica as not able to perform the
221role of master, so a replica with priority of 0 will never be selected by
222Redis Sentinel for promotion.
223# 但是,特殊优先级0会将副本标记为不能执行主角色,因此Redis Sentinel永远不会选择优先级为0的副本进行升级,默认情况下,优先级为100
224By default the priority is 100.
225replica-priority 100
226
227It is possible for a master to stop accepting writes if there are less than
228N replicas connected, having a lag less or equal than M seconds.
229# 如果连接的副本少于N个,且延迟小于或等于M秒,则主服务器可能会停止接受写入
230The N replicas need to be in "online" state.
231# N个副本需要处于“联机”状态
232The lag in seconds, that must be <= the specified value, is calculated from
233the last ping received from the replica, that is usually sent every second.
234# 延迟(以秒为单位)必须小于等于指定值,该延迟是从副本接收到的最后一次ping计算得出的,通常每秒钟发送一次
235This option does not GUARANTEE that N replicas will accept the write, but
236will limit the window of exposure for lost writes in case not enough replicas
237are available, to the specified number of seconds.
238# 此选项不能保证N个副本将接受写操作,但是如果没有足够的副本可用,则会将丢失写操作的暴露窗口限制为指定的秒数
239For example to require at least 3 replicas with a lag <= 10 seconds use:
240# 例如,要求至少3个副本的延迟<= 10秒,请使用
241min-replicas-to-write 3
242min-replicas-max-lag 10
243
244Setting one or the other to 0 disables the feature.
245# 将一个或另一个设置为0将禁用该功能
246By default min-replicas-to-write is set to 0 (feature disabled) and
247min-replicas-max-lag is set to 10.
248# 默认情况下,将要写入的最小副本设置为0(禁用功能),并且将最小副本最大延迟设置为10
249A Redis master is able to list the address and port of the attached
250replicas in different ways. For example the "INFO replication" section
251offers this information, which is used, among other tools, by
252Redis Sentinel in order to discover replica instances.
253Another place where this info is available is in the output of the
254"ROLE" command of a master.
255# Redis主服务器能够以不同方式列出附加副本的地址和端口。例如,“ INFO复制”部分提供了此信息,Redis Sentinel使用此信息以及其他工具来发现副本实例。该信息可用的另一个位置是主服务器的“ ROLE”命令的输出
256The listed IP address and port normally reported by a replica is
257obtained in the following way:
258# 副本通常报告的列出的IP地址和端口可以通过以下方式获得
259IP: The address is auto detected by checking the peer address
260of the socket used by the replica to connect with the master.
261# IP:通过检查副本用来与主服务器连接的套接字的对等地址来自动检测该地址
262Port: The port is communicated by the replica during the replication
263handshake, and is normally the port that the replica is using to
264listen for connections.
265# 端口:端口在复制握手期间由副本进行通信,通常是副本用来侦听连接的端口。
266However when port forwarding or Network Address Translation (NAT) is
267used, the replica may actually be reachable via different IP and port
268pairs. The following two options can be used by a replica in order to
269report to its master a specific set of IP and port, so that both INFO
270and ROLE will report those values.
271# 但是,当使用端口转发或网络地址转换(NAT)时,实际上可以通过不同的IP和端口对访问该副本。副本可以使用以下两个选项,以便向其主服务器报告特定的IP和端口集,以便INFO和ROLE都将报告这些值
272There is no need to use both the options if you need to override just
273the port or the IP address.
274# 如果只需要覆盖端口或IP地址,则无需使用这两个选项。
275replica-announce-ip 5.5.5.5
276replica-announce-port 1234
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
NoSQL Redis 数据库
【Redis源码】轻松看懂rdb文件(四)
【Redis源码】轻松看懂rdb文件(四)
182 0
|
1月前
|
设计模式 NoSQL 网络协议
大数据-48 Redis 通信协议原理RESP 事件处理机制原理 文件事件 时间事件 Reactor多路复用
大数据-48 Redis 通信协议原理RESP 事件处理机制原理 文件事件 时间事件 Reactor多路复用
37 2
|
3月前
|
缓存 NoSQL Linux
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
128 1
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
|
3月前
|
NoSQL Redis
Redis AOFRW主进程问题之更新manifest文件如何解决
Redis AOFRW主进程问题之更新manifest文件如何解决
|
3月前
|
存储 缓存 NoSQL
【Azure Redis 缓存 Azure Cache For Redis】如何设置让Azure Redis中的RDB文件暂留更久(如7天)
【Azure Redis 缓存 Azure Cache For Redis】如何设置让Azure Redis中的RDB文件暂留更久(如7天)
|
3月前
|
存储 NoSQL Redis
Redis 文件总大小问题之计算待加载AOF文件总大小如何解决
Redis 文件总大小问题之计算待加载AOF文件总大小如何解决
|
3月前
|
NoSQL 分布式数据库 Redis
Redis 进程问题之主进程更新manifest文件如何解决
Redis 进程问题之主进程更新manifest文件如何解决
|
3月前
|
NoSQL Redis
【Azure Redis】Redis导入备份文件(RDB)失败的原因
【Azure Redis】Redis导入备份文件(RDB)失败的原因
|
3月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
|
3月前
|
缓存 NoSQL 算法
【Azure Redis 缓存】Redis导出数据文件变小 / 在新的Redis复原后数据大小压缩近一倍问题分析
【Azure Redis 缓存】Redis导出数据文件变小 / 在新的Redis复原后数据大小压缩近一倍问题分析