赞
踩
《未完待续,持续更新》
Redis configuration https://redis.io/docs/management/config/
配置示例 Redis configuration file example https://redis.io/docs/management/config-file/
常用命令
config get
config set
config rewrite
bind 127.0.0.1 -::1
保护模式,默认为 yes
protected-mode yes
端口,默认为6379
port 6379
tcp-backlog 511
客户端空闲N秒后关闭连接,为0则禁用此功能,也就是不关闭连接。
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
TCP keepalive,tcp连接保活时长,默认维持300s的长连接。
tcp-keepalive 300
socket标记,默认为0代表不需要标记。
# The default value is 0, which implies no marking is required.
# socket-mark-id 0
是否后台启动,默认为no,建议设置为yes
daemonize no
当开启后,会写入/var/run/redis.pid
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# requires "expect stop" in your upstart job config
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# on startup, and updating Redis status on a regular
# basis.
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# supervised auto
指定 pidfile 的存放目录
pidfile /var/run/redis_6379.pid
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
日志级别:生产环境考虑 notice或者warning级别。
logfile ""
日志文件名和存放路径
开启系统日志
# To enable logging to the system logger, just set 'syslog-enabled' to yes,
# and optionally update the other syslog parameters to suit your needs.
# syslog-enabled no
指定系统日志标识
# Specify the syslog identity.
# syslog-ident redis
指定系统日志级别
# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
# syslog-facility local0
是否开启崩溃日志
# To disable the built in crash log, which will possibly produce cleaner core
# dumps when they are needed, uncomment the following:
#
# crash-log-enabled no
# To disable the fast memory check that's run as part of the crash log, which
# will possibly let redis terminate sooner, uncomment the following:
#
# crash-memcheck-enabled no
设置数据库的大小,默认16,可用范围0-15
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16
总是展示logo,默认no
# 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 and syslog logging is
# disabled. 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 no
# By default, Redis modifies the process title (as seen in 'top' and 'ps') to
# provide some runtime information. It is possible to disable this and leave
# the process name as executed by setting the following to no.
set-proc-title yes
进程标题模板
# When changing the process title, Redis uses the following template to construct # the modified title. # # Template variables are specified in curly brackets. The following variables are # supported: # # {title} Name of process as executed if parent, or type of child process. # {listen-addr} Bind address or '*' followed by TCP or TLS port listening on, or # Unix socket if only that's available. # {server-mode} Special mode, i.e. "[sentinel]" or "[cluster]". # {port} TCP port listening on, or 0. # {tls-port} TLS port listening on, or 0. # {unixsocket} Unix domain socket listening on, or "". # {config-file} Name of configuration file used. # proc-title-template "{title} {listen-addr} {server-mode}"
禁用RDB
save ""
默认配置
# save 3600 1 300 100 60 10000
当bgsave出现错误的时候停止写入,默认为yes,设置为no关闭此功能。
stop-writes-on-bgsave-error yes
rdb压缩,
# Compress string objects using LZF when dump .rdb databases?
# By default compression is enabled as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes
默认yes,开启压缩,会将string对象按照LZF算法进行压缩。
设置为no,优点:会节约一些cpu,缺点:如果有可以压缩的内容会让结果集变大。
# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes
默认yes代表开启,用百分之10的性能换来RDB抗损坏能力。
设置为no,优点:性能提升,缺点:降低文件的抗损坏能力。
# Enables or disables full sanitization checks for ziplist and listpack etc when
# loading an RDB or RESTORE payload. This reduces the chances of a assertion or
# crash later on while processing commands.
# Options:
# no - Never perform full sanitization
# yes - Always perform full sanitization
# clients - Perform full sanitization only for user connections.
# Excludes: RDB files, RESTORE commands received from the master
# connection, and client connections which have the
# skip-sanitize-payload ACL flag.
# The default should be 'clients' but since it currently affects cluster
# resharding via MIGRATE, it is temporarily set to 'no' by default.
#
# sanitize-dump-payload no
RDB文件存放名称
# The filename where to dump the DB
dbfilename dump.rdb
字面意思:RDB删除同步文件 no,
rdb-del-sync-files no
AOF和RDB的工作目录,持久化会在目录下生成对应的文件。
dir ./
从机配置主机的ip和端口
# replicaof <masterip> <masterport>
replicaof 192.168.1.1 6379
当开启主从复制的时候,配置主机的requirepass密码
# masterauth <master-password>
# masteruser <username>
当从机失去和主机的连接时或者复制的过程中有两种不同的策略。
# 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 error
# "MASTERDOWN Link with MASTER is down and replica-serve-stale-data is set to 'no'"
# to all data access commands, excluding commands such as:
# INFO, REPLICAOF, AUTH, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE,
# UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST,
# HOST and LATENCY.
replica-serve-stale-data yes
副本是否只读
replica-read-only yes
``
## repl-diskless-sync yes
是否开启无盘复制(网络复制)
```java
repl-diskless-sync yes
当repl-diskless-sync yes时,可以指定延迟
repl-diskless-sync-delay 5
当repl-diskless-sync yes时,可以指定最大副本数量
repl-diskless-sync-max-replicas 0
# 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.
# replica-announce-ip 5.5.5.5
# replica-announce-port 1234
tracking-table-max-keys 1000000
# Using an external ACL file
#
# Instead of configuring users here in this file, it is possible to use
# a stand-alone file just listing users. The two methods cannot be mixed:
# if you configure users here and at the same time you activate the external
# ACL file, the server will refuse to start.
#
# The format of the external ACL user file is exactly the same as the
# format that is used inside redis.conf to describe users.
#
# aclfile /etc/redis/users.acl
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatible with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
# requirepass foobared
# New users are initialized with restrictive permissions by default, via the
# equivalent of this ACL rule 'off resetkeys -@all'. Starting with Redis 6.2, it
# is possible to manage access to Pub/Sub channels with ACL rules as well. The
# default Pub/Sub channels permission if new users is controlled by the
# acl-pubsub-default configuration directive, which accepts one of these values:
#
# allchannels: grants access to all Pub/Sub channels
# resetchannels: revokes access to all Pub/Sub channels
#
# From Redis 7.0, acl-pubsub-default defaults to 'resetchannels' permission.
#
# acl-pubsub-default resetchannels
可以做什么:禁止flushdb等危险的命令。
# Command renaming (DEPRECATED). # # ------------------------------------------------------------------------ # WARNING: avoid using this option if possible. Instead use ACLs to remove # commands from the default user, and put them only in some admin user you # create for administrative purposes. # ------------------------------------------------------------------------ # # It is possible to change the name of dangerous commands in a shared # environment. For instance the CONFIG command may be renamed into something # hard to guess so that it will still be available for internal-use tools # but not available for general clients. # # Example: # # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 # # It is also possible to completely kill a command by renaming it into # an empty string: # # rename-command CONFIG "" # # Please note that changing the name of commands that are logged into the # AOF file or transmitted to replicas may cause problems.
常见Redis危险命令重命名
rename-command keys ""
rename-command flushdb ""
rename-command flushall ""
# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
# IMPORTANT: When Redis Cluster is used, the max number of connections is also
# shared with the cluster bus: every node in the cluster will use two
# connections, one incoming and another outgoing. It is important to size the
# limit accordingly in case of very large clusters.
#
# maxclients 10000
默认值:10000,代表最大支持1w个连接。
127.0.0.1:6379> CONFIG GET maxclients
1) "maxclients"
2) "10000"
127.0.0.1:6379>
案例1:验证maxclients 1
maxclients 1
[root@localhost ~]# redis-cli -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
AUTH failed: ERR max number of clients reached
127.0.0.1:6379>
当配置最大客户端为1时,redis-cli第二个客户端无法连接redis。
# Set a memory usage limit to the specified amount of bytes. # When the memory limit is reached Redis will try to remove keys # according to the eviction policy selected (see maxmemory-policy). # # If Redis can't remove keys according to the policy, or if the policy is # set to 'noeviction', Redis will start to reply with errors to commands # that would use more memory, like SET, LPUSH, and so on, and will continue # to reply to read-only commands like GET. # # This option is usually useful when using Redis as an LRU or LFU cache, or to # set a hard memory limit for an instance (using the 'noeviction' policy). # # WARNING: If you have replicas attached to an instance with maxmemory on, # the size of the output buffers needed to feed the replicas are subtracted # from the used memory count, so that network problems / resyncs will # not trigger a loop where keys are evicted, and in turn the output # buffer of replicas is full with DELs of keys evicted triggering the deletion # of more keys, and so forth until the database is completely emptied. # # In short... if you have replicas attached it is suggested that you set a lower # limit for maxmemory so that there is some free RAM on the system for replica # output buffers (but this is not needed if the policy is 'noeviction'). # # maxmemory <bytes>
将内存设置为1byte会发生什么?
127.0.0.1:6379> set k1 v1
(error) OOM command not allowed when used memory > 'maxmemory'.
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "1"
127.0.0.1:6379>
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # is reached. You can select one from the following behaviors: # # volatile-lru -> Evict using approximated LRU, only keys with an expire set. # allkeys-lru -> Evict any key using approximated LRU. # volatile-lfu -> Evict using approximated LFU, only keys with an expire set. # allkeys-lfu -> Evict any key using approximated LFU. # volatile-random -> Remove a random key having an expire set. # 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, when there are no suitable keys for # eviction, Redis will return an error on write operations that require # more memory. These are usually commands that create new keys, add data or # modify existing keys. A few examples are: SET, INCR, HSET, LPUSH, SUNIONSTORE, # SORT (due to the STORE argument), and EXEC (if the transaction includes any # command that requires memory). # # The default is: # # maxmemory-policy noeviction
默认策略:noeviction(just return an error on write operations)
127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"
127.0.0.1:6379>
# 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. By default Redis will check five keys and pick the one that was
# used least 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
设置为10更接近真实的LRU,设置为3更快但会损失一些精确度。
# Eviction processing is designed to function well with the default setting.
# If there is an unusually large amount of write traffic, this value may need to
# be increased. Decreasing this value may reduce latency at the risk of
# eviction processing effectiveness
# 0 = minimum latency, 10 = default, 100 = process without regard to latency
#
# maxmemory-eviction-tenacity 10
从节点忽略最大内存限制
# 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
# Redis reclaims expired keys in two ways: upon access when those keys are # found to be expired, and also in background, in what is called the # "active expire key". The key space is slowly and interactively scanned # looking for expired keys to reclaim, so that it is possible to free memory # of keys that are expired and will never be accessed again in a short time. # # The default effort of the expire cycle will try to avoid having more than # ten percent of expired keys still in memory, and will try to avoid consuming # more than 25% of total memory and to add latency to the system. However # it is possible to increase the expire "effort" that is normally set to # "1", to a greater value, up to the value "10". At its maximum value the # system will use more CPU, longer cycles (and technically may introduce # more latency), and will tolerate less already expired keys still present # in the system. It's a tradeoff between memory, CPU and latency. # # active-expire-effort 1
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
# cluster-enabled yes
开启集群模式取消注释
Tips:重要参数,务必配置
# Every cluster node has a cluster configuration file.
# This file is not intended to be edited by hand.
# It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have overlapping cluster configuration file names.
# cluster-config-file nodes-6379.conf
集群中的每一个节点需要一个唯一名称的配置文件,集群节点会负责创建和修改工作。
Tips:重要参数,务必配置
# Cluster node timeout is the amount of milliseconds a node must be unreachable for it to be considered in failure state.
# Most other internal time limits are a multiple of the node timeout.
# cluster-node-timeout 15000
当通讯超过该时间的时候,集群会认为节点不可用。
# The cluster port is the port that the cluster bus will listen for inbound connections on.
# When set to the default value, 0, it will be bound to the command port + 10000.
# Setting this value requires you to specify the cluster bus port when executing cluster meet.
# cluster-port 0
集群通讯端口,默认情况是命令端口+10000,也就是16379,可以指定一个固定端口。
要保证集群节点的该端口均可访问,否则会影响集群的通讯等。
# A replica of a failing master will avoid to start a failover if its data looks too old. # There is no simple way for a replica to actually have an exact measure of its "data age", so the following two checks are performed: 多个副本去参与failover(故障转移,选举)offset 越大也就是数据最全的rank(等级)越高,更容易成主节点。 # 1) If there are multiple replicas able to failover, they exchange messages in order to try to give an advantage to the replica with the best replication offset (more data from the master processed). # Replicas will try to get their rank by offset, and apply to the start of the failover a delay proportional to their rank. 每一个replica(从节点,副本)会计算最后一次和主节点通讯的时间。 # 2) Every single replica computes the time of the last interaction with its master. # This can be the last ping or command received (if the master is still in the "connected" state), or the time that elapsed since the disconnection with the master (if the replication link is currently down). 如果最后一次通讯间隔太长,replica不会进行故障转移。 # If the last interaction is too old, the replica will not try to failover at all. 这一点可以用户来控制,也就是通过配置文件的形式 # The point "2" can be tuned by user. 如果上一次通讯时间间隔 大于 这个公式计算出来的时间,副本节点不会进行 failover(故障转移) # Specifically a replica will not perform the failover if, since the last interaction with the master, the time elapsed is greater than: # (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period 如果这三个参数分别是ABC三个值,当时间大于(A*B+C)的时候,从节点不会参与选举。 # So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor is 10, and assuming a default repl-ping-replica-period of 10 seconds, the replica will not try to failover if it was not able to talk with the master for longer than 310 seconds. cluster-replica-validity-factor 这个参数过大会导致有很多旧数据的从节点参与故障转移。太小可能会阻止集群选举一个副本 # A large cluster-replica-validity-factor may allow replicas with too old data to failover a master, while a too small value may prevent the cluster from being able to elect a replica at all. 为了maximum availability(最大的可用性),可以将 cluster-replica-validity-factor设置为0,这意味着replicas(从节点们)会忽略与主节点通信的延迟参加选failover,也就是只要可以跟master通讯,就认为你有资格参加选举。 # For maximum availability, it is possible to set the cluster-replica-validity-factor to a value of 0, which means, that replicas will always try to failover the master regardless of the last time they interacted with the master. # (However they'll always try to apply a delay proportional to their offset rank). # Zero is the only value able to guarantee that when all the partitions heal the cluster will always be able to continue. 上面公式中的B,高可用推荐设置为0 # cluster-replica-validity-factor 10
repl-ping-replica-period(公式中的C)master 间隔 10 向 replicas 发送 PINGs
# Master send PINGs to its replicas 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
总结:判定一个从节点是否参加选举的公式是(A*B+C),为了高可用推荐将B设置为0,这样只要replica可以与集群通讯就认为有资格进行故障转移(选举成为主节点)。
集群副本能够迁移到孤立的主机,即没有工作副本的主机。 # Cluster replicas are able to migrate to orphaned masters, that are masters that are left without working replicas. 这提高了集群抵抗故障的能力,否则,如果没有有效的副本,孤立的主节点就不能在故障发生时进行故障转移。 # This improves the cluster ability to resist to failures as otherwise an orphaned master can't be failed over in case of failure if it has no working replicas. 只有当旧主机上至少还有给定数量的其他工作副本时,副本才会迁移到孤立主机。 # Replicas migrate to orphaned masters only if there are still at least a given number of other working replicas for their old master. 这个数字称为migration barrier(迁移阻碍) # This number is the "migration barrier". 参数为1意味着只有当主服务器至少有一个其他工作副本时,副本服务器才会迁移,依此类推。 # A migration barrier of 1 means that a replica will migrate only if there is at least 1 other working replica for its master and so forth. 它通常反映了集群中每个主服务器所需的副本数量。 #It usually reflects the number of replicas you want for every master in your cluster. 默认是1(只有他们的主节点至少包含一个从节点的时候才会发生迁移) # Default is 1 (replicas migrate only if their masters remain with at least one replica). 如果想要禁止迁移,可以把这个参数设为很大的值,或者将 cluster-allow-replica-migration 变为no # To disable migration just set it to a very large value or set cluster-allow-replica-migration to 'no'. 可以在调试中设置为0,不要再生产中设置为0,这是很危险的 # A value of 0 can be set but is useful only for debugging and dangerous in production. 默认为1 # cluster-migration-barrier 1
cluster-migration-barrier 这个参数控制集群 replica 的迁移限制,如果迁移后小于 N 值,会导致 replica 迁移失败。
问题:这个过程是自动的还是手动的?
关闭这个选项允许自动化较低的集群配置,也就是说默认自动化较高
# Turning off this option allows to use less automatic cluster configuration.
它既禁止迁移到孤立的主服务器,也禁止从空的主服务器迁移。
# It both disables migration to orphaned masters and migration from masters that became empty.
默认为 yes ,允许自动migrations(迁移)
# Default is 'yes' (allow automatic migrations).
# cluster-allow-replica-migration yes
# cluster-require-full-coverage yes
# cluster-replica-no-failover no
默认为 no,
如果设置为 yes,代表该节点如果为replica则永远不会failover(故障转移)成为master节点,仍然可以手动进行 failover
# cluster-allow-reads-when-down no
# cluster-allow-pubsubshard-when-down yes
# cluster-link-sendbuf-limit 0
# cluster-announce-hostname ""
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。