首页 > 数据库 >Redis学习笔记Ⅰ

Redis学习笔记Ⅰ

时间:2023-02-12 17:14:04浏览次数:57  
标签:AOF 持久 文件 Redis rdb redis 笔记 学习 RDB

Redis Ⅰ

Redis.conf详解

  • 单位注意事项:当需要内存大小时,可以指定

    # 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.
    
  • 包含:可以组合多个config文件

    ############################### 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.
    #
    # Notice 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 c:\path\to\other.conf
    
  • 网络

    bind 127.0.0.1 #绑定的ip
    port 6379 #端口号
    protected-mode #保护模式
    *#rotected-mode 是为了禁止公网访问redis cache,加强redis安全的。
    #它启用的条件,有两个:
    #1) 没有bind IP
    #2) 没有设置访问密码
    
  • 通用

    daemonize yes #守护进程
    pidfile /var/run/redis_6379.pid #如果以后台的方式运行就需要一个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
    # Specify the log file name. Also 'stdout' can be used to force
    # Redis to log on the standard output. 
    logfile ""
    # 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
    
  • 快照(SNAPSHOTTING):

    做持久化的时候会用到,在规定时间内执行了多少次操作就会持久化到文件 .rdb .aof

    因为是内存数据库,如果没有持久化,数据就会断电即失

    ################################ SNAPSHOTTING  ################################
    #
    # Save the DB on disk:
    #
    #   save <seconds> <changes>
    #
    #   Will save the DB if both the given number of seconds and the given
    #   number of write operations against the DB occurred.
    #
    #   In the example below the behaviour will be to save:
    #   after 900 sec (15 min) if at least 1 key changed
    #   after 300 sec (5 min) if at least 10 keys changed
    #   after 60 sec if at least 10000 keys changed
    #
    #   Note: you can disable saving completely by commenting out all "save" lines.
    #
    #   It is also possible to remove all the previously configured save
    #   points by adding a save directive with a single empty string argument
    #   like in the following example:
    #
    #   save ""
    #持久化时 自己定义
    save 900 1
    save 300 10
    save 60 10000
    
    
    #持久化失败是否会继续工作
    stop-writes-on-bgsave-error yes
    
    #是否压缩rdb文件 需要消耗一些cpu资源
    rdbcompression yes
    
    # The DB will be written inside this directory, with the filename specified
    # above using the 'dbfilename' configuration directive.
    # 
    # The Append Only File will also be created inside this directory.
    # 
    # Note that you must specify a directory here, not a file name.
    #RDB 文件的保存目录
    dir ./
    
  • 复制(REPLICATION)

    见下文主从复制

  • 安全 (SECURITY)

     requirepass 123456
     
     #在命令行设置密码
     config set requirepass "123456"
     
     # 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
     
    
  • 限制(LIMITS)

    maxclients 10000 #
    maxmemory <bytes>
    
    #MAXMEMORY POLICY: how Redis will select what to remove when maxmemory is reached. You can select among five behaviors:
    #内存到达上限之后的处理操作
    # volatile-lru -> remove the key with an expire set using an LRU algorithm 使用 LRU 算法删除设置了过期时间的key
    # allkeys-lru -> remove any key according to the LRU algorithm   根据 LRU 算法删除任意键
    # volatile-random -> remove a random key with an expire set   删除带有过期集的随机key
    # allkeys-random -> remove a random key, any key
    # volatile-ttl -> remove the key with the nearest expire time (minor TTL )删除过期时间最近的key(次要 TTL)
    # noeviction -> don't expire at all, just return an error on write operations  根本不会过期,只是在写操作时返回一个错误
    #The default is:
    maxmemory-policy noeviction
    
  • APPEND ONLY MODE (aof配置)

    appendonly no   #默认不开启 默认使用RDB方式持久化 大部分rdb够用了
    appendfilename "appendonly.aof" #持久化的文件名
    
    # appendfsync always # 每次修改都会 sync 消耗性能
    appendfsync everysec # 每一秒都会执行一次sync 可能会丢失这1秒的数据
    # appendfsync no # 不执行 sync, 这个时候操作系统自己同步数据,速度最快 
    

Redis持久化

Redis 持久化

解决因为是内存数据库断电即失的问题,提供一个数据的持久化保存的功能

RDB,简而言之,就是在不同的时间点,将 redis 存储的数据生成快照并存储到磁盘等介质上;

AOF,则是换了一个角度来实现持久化,那就是将 redis 执行过的所有写指令记录下来,在下次 redis 重新启动时,只要把这些写指令从前到后再重复执行一遍,就可以实现数据恢复了。

其实 RDB 和 AOF 两种方式也可以同时使用,在这种情况下,如果 redis 重启的话,则会优先采用 AOF 方式来进行数据恢复,这是因为 AOF 方式的数据恢复完整度更高。

RDB(Redis DataBase)

  • 是什么

    RDB 方式,是将 redis 某一时刻的数据持久化到磁盘中,是一种快照式的持久化方法。默认的持久化方式。

    redis 在进行数据持久化的过程中,会先将数据写入到一个临时文件中,待持久化过程都结束了,才会用这个临时文件替换上次持久化好的文件。正是这种特性,让我们可以随时来进行备份,因为快照文件总是完整可用的。

    优点:

    • 对于 RDB 方式,redis 会单独创建(fork)一个子进程来进行持久化,而主进程是不会进行任何 IO 操作的,这样就确保了 redis 极高的性能。

    • 如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那 RDB 方式要比 AOF 方式更加的高效

    缺点:

    • 虽然 RDB 有不少优点,但它的缺点也是不容忽视的。如果你对数据的完整性非常敏感,那么 RDB 方式就不太适合你,因为即使你每 5 分钟都持久化一次,当 redis 故障时,仍然会有近 5 分钟的数据丢失。
    • 需要一定的时间修改。
    • fork进程的时候会占用一定的内存空间。
  • 执行条件

    • save的规则满足的条件下 就会生成rdb文件

    • 执行flushall命令,也会触发rdb规则

    • 退出redis,也会产生rdb文件

      备份就自动生成一个dump.rdb

  • 如何恢复rdb文件

    • 只需要将rdb文件放到redis启动目录就行,redis启动的时候hi自动检查dump.rdb文件来恢复其中的数据。

    • 查看需要存在的位置:config get dir

      几乎它的默认配置就足够使用了

  • # Compress string objects using LZF when dump .rdb databases? 是否使用压缩(compress)
    # For default that's set to 'yes' 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
    
    # Since version 5 of RDB a CRC64 checksum is placed at the end of the file.  是否关闭CRC校验和
    # 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
    
    # The filename where to dump the DB 
    # 持久化文件名
    dbfilename dump.rdb
    
    # The working directory.
    #
    # The DB will be written inside this directory, with the filename specified
    # above using the 'dbfilename' configuration directive.
    # 
    # The Append Only File will also be created inside this directory.
    # 
    # Note that you must specify a directory here, not a file name.
    # 持久化文件目录
    dir ./
    
    

AOF(Append Only File)

  • 是什么

    AOF,英文是 Append Only File,即只允许追加不允许改写的文件

    AOF 方式是将执行过的写指令记录下来,在数据恢复时按照从前到后的顺序再将指令都执行一遍,就这么简单。

    我们通过配置 redis.conf 中的 appendonly yes 就可以打开 AOF 功能。如果有写操作(如 SET 等),redis 就会被追加到 AOF 文件的末尾。

    优点:

    • 默认的 AOF 持久化策略是每秒钟 fsync 一次(fsync 是指把缓存中的写指令记录到磁盘中),因为在这种情况下,redis 仍然可以保持很好的处理性能,即使 redis 故障,也只会丢失最近 1 秒钟的数据。

      # appendfsync always # 每次修改都会 sync 消耗性能
      appendfsync everysec # 每一秒都会执行一次sync 可能会丢失这1秒的数据
      # appendfsync no # 不执行 sync, 这个时候操作系统自己同步数据,速度最快 
      
    • 良好的修复能力。如果在追加日志时,恰好遇到磁盘空间满、inode 满或断电等情况导致日志写入不完整,也没有关系,redis 提供了 redis-check-aof 工具,可以用来进行日志修复。

      redis-check-aof --fix appendonly.aof

      因为采用了追加方式,如果不做任何处理的话,AOF 文件会变得越来越大,为此,redis 提供了 AOF 文件重写(rewrite)机制,即当 AOF 文件的大小超过所设定的阈值时,redis 就会启动 AOF 文件的内容压缩,只保留可以恢复数据的最小指令集。举个例子或许更形象,假如我们调用了 100 次 INCR 指令,在 AOF 文件中就要存储 100 条指令,但这明显是很低效的,完全可以把这 100 条指令合并成一条 SET 指令,这就是重写机制的原理。

      在进行 AOF 重写时,仍然是采用先写临时文件,全部完成后再替换的流程,所以断电、磁盘满等问题都不会影响 AOF 文件的可用性,这点大家可以放心。

    • AOF 方式的另一个好处,我们通过一个“场景再现”来说明。某同学在操作 redis 时,不小心执行了 FLUSHALL,导致 redis 内存中的数据全部被清空了,这是很悲剧的事情。不过这也不是世界末日,只要 redis 配置了 AOF 持久化方式,且 AOF 文件还没有被重写(rewrite),我们就可以用最快的速度暂停 redis 并编辑 AOF 文件,将最后一行的 FLUSHALL 命令删除,然后重启 redis,就可以恢复 redis 的所有数据到 FLUSHALL 之前的状态了。是不是很神奇,这就是 AOF 持久化方式的好处之一。但是如果 AOF 文件已经被重写了,那就无法通过这种方法来恢复数据了。

    缺点:

    • 在同样数据规模的情况下,AOF 文件要比 RDB 文件的体积大。
    • AOF 方式的恢复速度也要慢于 RDB 方式。
    • 运行效率也没有 RDB 好,所以默认使用 RDB 的。

    如果你直接执行 BGREWRITEAOF 命令,那么 redis 会生成一个全新的 AOF 文件,其中便包括了可以恢复现有数据的最少的命令集。

    如果运气比较差,AOF 文件出现了被写坏的情况,也不必过分担忧,redis 并不会贸然加载这个有问题的 AOF 文件,而是报错退出。这时可以通过以下步骤来修复出错的文件

    1. 备份被写坏的 AOF 文件\
    2. 运行 redis-check-aof –fix 进行修复\
    3. 用 diff -u 来看下两个文件的差异,确认问题点\
    4. 重启 redis,加载修复后的 AOF 文件

AOF重写

解决aof文件越来越大的问题

开始前,redis 会创建(fork)一个“重写子进程”,这个子进程会首先读取现有的 AOF 文件,并将其包含的指令进行分析压缩写入到一个临时文件中。

与此同时,主工作进程会将新接收到的写指令一边累积到内存缓冲区中,一边继续写入到原有的 AOF 文件中,这样做是保证原有的 AOF 文件的可用性,避免在重写过程中出现意外。

当“重写子进程”完成重写工作后,它会给父进程发一个信号,父进程收到信号后就会将内存中缓存的写指令追加到新 AOF 文件中。

当追加结束后,redis 就会用新 AOF 文件来代替旧 AOF 文件,之后再有新的写指令,就都会追加到新的 AOF 文件中了。

拓展

其实 RDB 和 AOF 两种方式也可以同时使用,在这种情况下,如果 redis 重启的话,则会优先采用 AOF 方式来进行数据恢复,这是因为 AOF 方式的数据恢复完整度更高。

如果你没有数据持久化的需求,也完全可以关闭 RDB 和 AOF 方式,这样的话,redis 将变成一个纯内存数据库,就像 memcache 一样。

对于我们应该选择 RDB 还是 AOF,官方的建议是两个同时使用。这样可以提供更可靠的持久化方案。

双选时,RDB可以作为备份,只需要在slave上每十五分钟保存一次即可。AOF可以保证丢失的数据不至于太大,但是默认的64M就会重写的机制带来了较大的IO,可以将其修改到5G以上,默认超过原大小100%大小重写可以改到适当的数值。

标签:AOF,持久,文件,Redis,rdb,redis,笔记,学习,RDB
From: https://www.cnblogs.com/yiochin/p/17114134.html

相关文章

  • Redis学习笔记Ⅱ
    RedisⅡRedis发布订阅官网很简介了已经。订阅进阶:消息队列Redis发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。Redis客户端可以......
  • JVM垃圾回收 笔记
    垃圾回收算法新生代中因为对象都是“朝生夕死的”,深入理解JVM虚拟机上说98%的对象存活率很低,适用于复制算法,复制算法比较适合用于存活率低的内存区域。它优化了标记/清除......
  • 嵌入式学习框架
    中大型的嵌入式开发是在linux平台上进行上层应用开发、linux层面的驱动等内核开发以及底层的硬件设计;通过linux上的设备驱动程序来驱动硬件。所以嵌入式开发可以细分为三类......
  • Redis(十五)——实现分布式锁
    1、基于set命令的分布式锁加锁:使用setnx进行加锁,该指令返回1时,加锁成功。解锁:使用del释放,以便其他线程可以继续获取锁存在问题:A线程获取锁后还没释放就挂了,死锁。解决......
  • Mybatis-尚硅谷-学习笔记
    https://blog.csdn.net/weixin_45581692/article/details/127508494?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167619048716800188527496%2522%252C%2522......
  • 学习python中的字符串操作
    字符串字符串的存储地址原理​ 在python中有一个字符串保留区,当中存放了声明的字符串,每一个字符串对应一个地址,变量名指向对应的地址,只要字符串相同,声明内容相同的字符......
  • Linux学习-day9
    第六章存储结构与管理硬盘6.1一切从“/”开始Linux系统中一切都是文件​Linux系统中的一切文件都是从“根”目录(/)开始的,并按照文件系统层次标准(FHS)采用倒树状结构来存放......
  • C语言学习:读写一行字符
    1#include<stdio.h>2#include<io_utils.h>3#include<errno.h>4#include<string.h>56voidReadFile1(){7FILE*file=fopen("CMakeLists.txt"......
  • centos7部署redis
    参考文档https://redis.io/docs/getting-started/installation/install-redis-from-source/https://blog.csdn.net/weixin_45550937/article/details/1240920771、安......
  • spring mvc笔记
    https://blog.csdn.net/weixin_45581692/article/details/127258639?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167618955116800184175434%2522%252C%2522......