当前位置:   article > 正文

【Redis】Redis配置文件详解(很全)_redis 配置文件

redis 配置文件

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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 包含其它配置文件的信息 include path

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

    # include /path/to/local.conf
    # include /path/to/other.conf
    
    • 1
    • 2

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
    
    • 1
    • 2
    • 3

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

    root@VM-4-5-ubuntu:~# ifconfig 
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.4.5  netmask 255.255.252.0  broadcast 10.0.7.255
            inet6 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 0
            TX packets 280878  bytes 43033240 (43.0 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::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 0
            TX packets 35168  bytes 2582220 (2.5 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

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

  • 保护模式 protected-mode

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

    protected-mode yes
    
    • 1
  • 端口号 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
    
    • 1
    • 2
    • 3
  • 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
    
    • 1
  • **是否超时无操作关闭连接 timeout **

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

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

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

    tcp-keepalive 300
    
    • 1

GENERAL 通用配置

  • **启动方式 daemonize **

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

    daemonize yes 
    
    • 1
  • 进程pid文件 pidfile

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

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

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

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

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

    databases 16
    
    • 1
  • 启动是否显示logo

    always-show-logo yes
    
    • 1

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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

  • 配置ACL

    ACL:访问控制列表。

    有两种方法配置ACL:

    • 在命令行通过ACL命令进行配置
    • 在Redis配置文件中开始,可以直接在redis.conf中配置,也可以通过外部aclfile配置。aclfile path

    配置语法:user <username> ... acl rules ...,例如 user worker +@list +@connection ~jobs:* on >ffa9203c493aa99

    redis默认有一个default用户。如果default具有nopass规则(就是说没有配置密码),那么新连接将立即作为default用户登录,无需通过AUTH命令提供任何密码。否则,连接会在未验证状态下启动,并需要AUTH验证才能开始工作。


    描述用户可以做的操作的ACL规则如下:

    • 启用或禁用用户(已经建立的连接不会生效)
      • on 启用用户,该用户可以验证身份登陆。
      • off 禁用用户,该用户不允许验证身份登陆。
    • 允许/禁止用户执行某些命令
      • +<command> 允许用户执行command指示的命令✅
      • -<command> 禁止用户执行command指示的命令声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签