有了这篇文章redis配置文件就再也不怕了!!!--------------redis配置文件常用配置详解

redis 配置文件常用配置详解

1、内存大小单位units

# 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. 在redis中,内存大小是可以任意指定的,当你需要的时候可以对其进行更改,而且redis中只有bytes单位,没有bit。
  2. redis中对英文大小写是不敏感的。

2.配置文件导入使用的 INCLUDES (包含)

################################## INCLUDES ###################################

# Include one or more other config files here.  This is useful if you
# have a standard template that goes to all Redis servers but also need
# to customize a few per-server settings.  Include files can include
# other files, so use this wisely.
#
# Note that option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf

解释:
include包含几点含义:

1.首先在开始说明了Include用在什么场合?

这个可以配置一个或多个其他配置文件。如果你有一个适用于所有Redis服务器的标准模板,但也需要定制一些每个服务器的设置,可以使用该配置进行文件的配置,以减少工作量。

2.include的注意事项
  • include 不会被来自admin 和redis sentinel 使用 命令 “CONFIG REWRITE” 进行改写
  • 由于Redis总是使用最后的处理行作为配置指令的值,你最好把include放在这个文件的开头,以避免在运行时覆盖配置更改。

3.模块 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

解释:

MODULES的作用及注意

Modules是用来配置导入的模块,它可以导入多个模块,我们需要关心的是 导入Moudle的格式

4.网络配置 NETWORK(重要)

4.1 bind详解
################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
# Each address can be prefixed by "-", which means that redis will not fail to
# start if the address is not available. Being not available only refers to
# addresses that does not correspond to any network interfece. Addresses that
# are already in use will always fail, and unsupported protocols will always BE
# silently skipped.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses
# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6
# bind * -::*                     # like the default, all available interfaces
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 -::1
1.bind 的作用是什么?

bind的中文意思是绑定的意思。同理bind在配置文件中的作用就是去绑定可以访问本redis的ip,若不写bind,就意味着所有的Ip都可以访问,本redis服务器,这是不安全的,有风险的。

2.bind 如何使用?

在你开发的时候你对bind进行配置ip以方便你可以访问,可以绑定一个或者多个,如: bind 192.168.1.100 10.0.0.1,这个指能绑定两个ip,会允许这俩个都可以访问; 对于bind 127.0.0.1 -::1 首先这里加了- ,这里的意思是如果该地址没有启动,那么redis就会启动失败。对于::1来说,这是指回环(自己发自己接,它的含义为是压缩格式IPV6回送地址0:0:0:0:0:0:0:1。它等效于IPV4地址127.0.0.1。)所以是本地的ipv4 和ipv6 都可以访问到;对于bind * -:😗 ,意味所有的ip都可以访问。

4.2 保护模式 protected-mode
# 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
用来干嘛的?

当将protected-mode 设为yes,且你没有设置bind ,也没有设置密码,redis的服务只能被你本机访问(ipv4、ipv6)和本机上的进程通信

怎么用?

开启 protected-mode yes
关闭 protected-mode no

4.3 端口号 prot
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

用于设置redis 的端口号,默认的为6379 ;一个浪漫的端口号

4.4 tcp-backlog
# TCP listen() backlog.
#
# In high requests-per-second environments you need a high backlog in order
# to avoid slow clients connection issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511
1. 这个参数的作用?

官方意思:是为了应对高并发的环境下,你需要将该参数设置的较高,以避免客户端连接缓慢的问题。
其实就是高并发量下,提高用户访问速度

2.该参数的工作原理?

一个客户端连接在完成TCP的3次握手之前首先进入到未连接队列,完成握手之后正式建立连接,进入已连接队列,交付给应用程序处理。应用程序调用accept()函数从已连接队列取出连接进行处理。应用层在调用listen()函数时指定的backlog是已连接队列大小,如果大于somaxconn将被设为somaxconn。所以在redis中的tcp-backlog参数大小的设置就决定了客户端连接速度的问题。

3.如何修改该参数?

因为该参数的值大小受到Linux环境的影响, Linux 内核会默默地将其截断为 /proc/sys/net/core/somaxconn 的值,因此请确保同时提高 somaxconn 和 tcp_max_syn_backlog 的值以获得所需的效果。
修改方法:

  1. 执行命令: vi etc/sysctl.conf
  2. 在文件里面添加 net.core.somaxconn= 1024(指你想要的值)
    3.保存退出 然后执行sysctl -p 然后可以正常启动
4.因该参数出现的问题,及解决方法?

(这几篇博客写的非常好,分析问题的角度和方法,保存在我的印象笔记里了,收获颇丰)
1.
redis connect timeout问题排查
redis connect timeout

2.Linux 启动redis 报错:WARNING: The TCP backlog setting of 511 cannot be enforced

3.redis优化
4.这里的解决方法都是对值该参数值进行修改

4.5 Unix socket:用于侦听

# Unix socket.
#
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /run/redis.sock
# unixsocketperm 700
  1. 作用说明:
    指定用于侦听传入连接的 Unix socket 路径。
    没有默认值,因此若未指定该选项,则 Redis 不会侦听 Unux Socket。
4.6 timeout:连接空闲超时

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

作用:客户端闲置 N 秒后关闭连接,设为 0 则禁用该机制。

tcp-keepalive: 保活连接

# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Force network equipment in the middle to consider the connection to be
#    alive.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 300

说明:如果该选项配置不为 0,则 Redis 将周期性使用 SO_KEEPALIVE 向 客户端发送 TCP ACK 包,有两个用途:

  • 对端状态探测。
  • 保持网络连接的活动状态。
    在 Linux 上,指定的值(以秒为单位)是用于发送 ACK 的时间间隔。注意,关闭连接需要两倍的时间。在其它内核上,期限取决于内核配置。默认300,一般设置为60。

5.通用设置:GENERAL

5.1 daemonize:设置后台
#################################GENERAL#################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no

说明:
一般情况下,redis是前台显示,不能在后台运行,如果需要将其设置为后台进程就需要将默认的no,改为yes。这时就能后台运行了。

5.2 pidfile:进程文件
# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
#
# Note that on modern Linux systems "/run/redis.pid" is more conforming
# and should be used instead.
pidfile /var/run/redis_6379.pid

注意

  • 这是存放pid文件的位置,每个实例都会产生一个不同的Pid文件。
  • 若不是守护进程运行,不会创建pid文件,守护进程会自动创建 /var/run/redis.pid
  • 创建一个pid文件是最好的努力:如果Redis无法创建它,没有什么不好的事情发生,服务器将启动并正常运行。
5.3 loglevel: 日志级别
# 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
  • 指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为notice
  • 四个级别根据使用阶段来选择,生产环境选择notice 或者warning
日志记录4个级别的区别
  1. debug(很多信息,对开发/测试有用)
  2. verbose(许多很少有用的信息,但不像调试级别那样混乱)
  3. notice(适度冗长,您可能在生产中想要什么)
  4. warning (只记录非常重要/关键的消息)
5.4 logfile: 日志名称

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""

来指定日志名称

5.5 database: 数据库数量
# 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

说明:用于设置redis中的数据库的数量,一般默认数据库的数量为16 ,默认使用的数据库Id为0,可以使用SELECT 命令 连接指定数据库id ,这里的abid 范围为:0~abid-1

6.SECURITY:安全

# 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 compatable with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
# requirepass foobared

访问密码的查看、设置和取消
在命令中设置密码,只是临时的。重启redis服务器,密码就还原了。
永久设置,需要再配置文件中进行设置。
(这一部分,我看注解看的有点懵,等深入学习一段时间后,再回来补充)

7.CLIENTS:客户端

7.1 maxclients:最大客户端量
################################### CLIENTS ####################################

# 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

这里直接把官方的翻译下:

  1. 设置最大同时连接客户端数。 **默认情况下,此限制设置为 10000 个客户端**,但是如果 Redis 服务器无法配置进程文件限制以允许指定的限制,则允许客户端的最大数量设置为当前文件限制减去 32(因为 Redis 保留了一个 很少有文件描述符供内部使用)。
    
  2.  一**旦达到限制,Redis 将关闭所有新连接,并发送错误“已达到最大客户端数”**。
    
  3.  重要:使用Redis Cluster时,最大连接数也与集群总线共享:**集群中的每个节点将使用两个连接**,一个传入,另一个传出。 在非常大的集群的情况下,相应地调整限制的大小很重要。
    

8.MEMORY MANAGEMENT:内存管理

8.1 maxmemory:最大内存

############################## MEMORY MANAGEMENT ################################
# 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>


说明:

  1. 将内存使用限制设置为指定的字节数。
  2. 当达到内存限制时,Redis 将尝试根据选择的驱逐策略删除键(这里的驱逐策略指: maxmemory-policy)。
  3. 如果 Redis 无法根据策略删除键值,或者如果策略是设置为 ‘noeviction’,Redis 将开始回复错误命令。这将使用更多内存,如 SET、LPUSH 等,并将继续回复只读命令,如 GET。
  4. 当使用 Redis 作为 LRU 或 LFU 缓存时,此选项通常很有用,或者为实例设置硬内存限制(使用“noeviction”策略)。
设置方式为:

maxmemory

8.2 MAXMEMORY POLICY

# 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


说明:

  1. MAXMEMORY 策略:当达到 maxmemory 时,Redis 将如何选择要删除的内容。您可以从以下行为中选择一种:
  • volatile-lru -> 使用近似 LRU 驱逐,只有设置了过期时间的键。
  • allkeys-lru -> 使用近似 LRU 驱逐任何键。
  • volatile-lfu -> 使用近似 LFU 驱逐,只有设置了过期时间的键。
  • allkeys-lfu -> 使用近似 LFU 驱逐任何键。
  • volatile-random -> 删除设置了过期时间的随机密钥。
  • allkeys-random -> 删除一个随机密钥,任何密钥。
  • volatile-ttl -> 删除过期时间最近的键(次 TTL)
  • noeviction -> 不要驱逐任何东西,只是在写操作时返回一个错误。
    LRU 表示最近最少使用 LFU 表示最不常用 LRU、LFU 和 volatile-ttl 都是使用近似随机算法实现的。
    2.当策略中没有合适方法进行回收时,Redis 将在需要更多内存的写操作时返回错误。这些通常是创建新密钥、添加数据或修改现有密钥的命令。一些示例是:SET、INCR、HSET、LPUSH、SUNIONSTORE、SORT(由于 STORE 参数)和 EXEC(如果事务包括任何需要内存的命令)。
    3.默认的设置为:maxmemory-policy noeviction
8.3 maxmemory-samples
# 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

说明:

1. 这个参数是用来干嘛的?

LRU、LFU 和最小 TTL 算法不是精确算法而是近似算法(为了节省内存),因此您可以对其进行调整以提高速度或准确性。 默认情况下,Redis 将检查五个键并选择最近最少使用的键,您可以使用以下配置指令更改样本大小。

2.参数值配置指导?
  1. 默认值 5 会产生足够好的结果。 10 非常接近真实的 LRU,但花费更多的 CPU。 3 更快,但不是很准确。
3.配置的默认值?

maxmemory-samples 5

(这是redis 6 的配置文件,大概常用的就那么多,还有一些因为没有真正使用redis,等以后有问题和新的感触再加上。觉得不错就点个赞,万分感谢~)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下次一定少写BUG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值