文章目录

  • Redis配置文件
    • Units
    • Network 网络相关
    • GENERAL 通用配置
    • SNAPSHOTTING 快照相关
    • SECURITY 安全相关
    • CLIENTS 客户端配置
    • MEMORY MANAGEMENT 内存管理
    • LAZY FREEING 懒惰删除
    • THREADED I/O
    • KERNEL OOM CONTROL 设置OOM时终止哪些进程
    • APPEND ONLY MODE AOF持久化配置
    • LUA SCRIPTING-LUA脚本相关
    • REDIS CLUSTER 集群配置
    • CLUSTER DOCKER/NAT support
    • SLOW LOG 慢日志
    • LATENCY MONITOR 延迟监控
    • EVENT NOTIFICATION 事件通知
    • GOPHER SERVER Gopher协议
    • ADVANCED CONFIG 高级设置
    • ACTIVE DEFRAGMENTATION 碎片整理

Redis配置文件

redis版本: 6.0.16

Units

  • 配置数据单位换算关系

    ##################  该部分用于指定存储单位的大小换算关系,不区分大小写,只支持bytes,不支持bits
    # 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.
    
  • 包含其它配置文件的信息 include path

    对于公共部分配置,可以按以下方式配置引入

    # include /path/to/local.conf
    # include /path/to/other.conf
    

Network 网络相关

  • bind IP1 [IP2 …]

    这项配置绑定的IP并不是远程访问的客户端的IP地址,而是本机的IP地址。

    # bind 192.168.1.100 10.0.0.1
    # bind 127.0.0.1 ::1
    bind 127.0.0.1
    

    本机的IP地址是和网卡(network interfaces)绑定在一起的,配置这项后,Redis只会接收来自指定网卡的数据包。比如我的主机有以下网卡:

    root@VM-4-5-ubuntu:~# ifconfig
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.0.4.5  netmask 255.255.252.0  broadcast 10.0.7.255inet6 fe80::5054:ff:fe0b:843  prefixlen 64  scopeid 0x20<link>ether 52:54:00:0b:08:43  txqueuelen 1000  (Ethernet)RX packets 283943  bytes 28027507 (28.0 MB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 280878  bytes 43033240 (43.0 MB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::1  prefixlen 128  scopeid 0x10<host>loop  txqueuelen 1000  (Local Loopback)RX packets 35168  bytes 2582220 (2.5 MB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 35168  bytes 2582220 (2.5 MB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    

    如果我想要让Redis可以远程连接的话,就需要让Redis监听eht0这块网卡,也就是要加上配置bind 127.0.0.1 10.0.4.5,这样既可以本地访问,也能够远程访问。(主要bind只能有一行配置,如果有多个网卡要监听,就配置多个ip,用空格隔开,否者只有配置的最后一个bind生效)。

  • 保护模式 protected-mode

    从注释信息就可以看到,如果protected-modeyes的话,如果没有指定bind或者没有指定密码,那么只能本地访问。

    protected-mode yes
    
  • 端口号 port

    配置Redis监听的端口号,默认6379。

    # 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
    
  • TCP半连接队列长度配置 tcp-backlog

    在进行TCP/IP连接时,内核会维护两个队列

    • syns queue用于保存已收到sync但没有接收到ack的TCP半连接请求。由/proc/sys/net/ipv4/tcp_max_syn_backlog指定,我的系统(Ubuntu20.04)上是1024。
    • accept queue,用于保存已经建立的连接,也就是全连接。由/proc/sys/net/core/somaxconn指定。

    根据配置里的注释,需要同时提高somaxconntcp_max_syn_backlog的值来确保生效。

    tcp-backlog 511
    
  • **是否超时无操作关闭连接 timeout **

    客户端经过多少时间(单位秒)没有操作就关闭连接,0代表永不关闭。

    timeout 0
    
  • TCP连接保活策略 tcp-keepalive

    TCP连接保活策略,可以通过tcp-keepalive配置项来进行设置,单位为秒,假如设置为60秒,则server端会每60秒向连接空闲的客户端发起一次ACK请求,以检查客户端是否已经挂掉,对于无响应的客户端则会关闭其连接。如果设置为0,则不会进行保活检测。

    tcp-keepalive 300
    

GENERAL 通用配置

  • **启动方式 daemonize **

    是否以守护(后台)进程的方式启动,默认no。

    daemonize yes
    
  • 进程pid文件 pidfile

    redis启动后会把pid写入到pidfile指定的文件中。

    pidfile /var/run/redis_6379.pid
    
  • 日志相关 loglevel logfile

    loglevel用于配置日志打印机别,默认notice

    • debug:能设置的最高的日志级别,打印所有信息,包括debug信息。
    • verbose:打印除了debug日志之外的所有日志。
    • notice:打印除了debugverbose级别的所有日志。
    • warning:仅打印非常重要的信息。
    loglevel noticelogfile ""
    
  • 指定数据库的数量 databse

    redis默认有16个数据库,编号从0开始。

    databases 16
    
  • 启动是否显示logo

    always-show-logo yes
    

SNAPSHOTTING 快照相关

SECURITY 安全相关

################################## SECURITY #################################### Warning: since Redis is pretty fast, an outside user can try up to
# 1 million passwords per second against a modern box. This means that you
# should use very strong passwords, otherwise they will be very easy to break.
# Note that because the password is really a shared secret between the client
# and the server, and should not be memorized by any human, the password
# can be easily a long string from /dev/urandom or whatever, so by using a
# long and unguessable password no brute force attack will be possible.

大致意思就是redis很快,所以被破解密码时,性能也很好,如果你的密码太渣渣了,那么可能很快就被破解了,因此尽量使用长且不容易被猜到的密码作为redis的访问密码。