zoukankan      html  css  js  c++  java
  • 峰Redis学习(8)Redis 持久化AOF方式

    第三节:Redis 的持久化之AOF 方式      

    AOF方式:将以日志,记录每一个操作
     

    优势:安全性相对RDB方式高很多;

    劣势:效率相对RDB方式低很多;

    1)AOF方式需要配置:

    # Please check http://redis.io/topics/persistence for more information.
    
    appendonly no
    
    # The name of the append only file (default: "appendonly.aof")
    
    appendfilename "appendonly.aof"

    appendonly no默认关闭aof方式 我们修改成yes 就开启

    下面那个是默认的aof文件名

    # If unsure, use "everysec".
    
    appendfsync always
    # appendfsync everysec
    # appendfsync no

    这里是三种同步策略:

    always 是 只要发生修改,立即同步 (推荐实用 安全性最高)

    everysec 是 每秒同步一次

    no是不同步 

    我们修改成always

    2)保存退出,重新启动redis,然后随便加几个key

    [root@bogon redis]# ll
    总用量 52
    drwxr-xr-x. 2 root root   134 3月  31 20:51 bin
    -rw-r--r--. 1 root root   102 4月   1 11:44 dump.rdb
    -rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf
    [root@bogon redis]# bin/redis-server redis.conf 
    [root@bogon redis]# bin/redis-cli
    127.0.0.1:6379> keys *
    (empty list or set)
    127.0.0.1:6379> set test1 111
    OK
    127.0.0.1:6379> set test 222
    OK
    127.0.0.1:6379> shutdown save
    not connected> exit
    [root@bogon redis]# ll
    总用量 56
    -rw-r--r--. 1 root root    88 4月   1 11:49 appendonly.aof
    drwxr-xr-x. 2 root root   134 3月  31 20:51 bin
    -rw-r--r--. 1 root root   100 4月   1 11:49 dump.rdb
    -rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf
    [root@bogon redis]# rm -rf dump.rdb 
    [root@bogon redis]# ll
    总用量 52
    -rw-r--r--. 1 root root    88 4月   1 11:49 appendonly.aof
    drwxr-xr-x. 2 root root   134 3月  31 20:51 bin
    -rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf

    这里就有一个appendonly.aof文件;

    3)将appendonly.aof剪切到/root/目录下,启动后,发现数据没了:

    [root@bogon redis]# ll
    总用量 52
    -rw-r--r--. 1 root root    88 4月   1 11:49 appendonly.aof
    drwxr-xr-x. 2 root root   134 3月  31 20:51 bin
    -rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf
    [root@bogon redis]# mv appendonly.aof /root/
    [root@bogon redis]# cd /root/
    [root@bogon ~]# ll
    总用量 1528
    -rw-------. 1 root root    1269 3月  31 23:56 anaconda-ks.cfg
    -rw-r--r--. 1 root root      88 4月   1 11:49 appendonly.aof
    drwxrwxr-x. 6 root root    4096 7月  28 2017 redis-3.2.10
    -rw-r--r--. 1 root root 1550261 7月  29 2017 redis-3.2.10.tar.gz
    [root@bogon ~]# cd /usr/local/redis/
    [root@bogon redis]# bin/redis-server redis.conf 
    [root@bogon redis]# bin/redis-cli
    127.0.0.1:6379> keys *
    (empty list or set)

    4)aof方式恢复数据:将/root/appendonly.aof拷贝至/usr/local/redis/下面,再重新启动redis:

    127.0.0.1:6379> keys *
    (empty list or set)
    127.0.0.1:6379> shutdown
    not connected> exit
    [root@bogon redis]# ll
    总用量 52
    -rw-r--r--. 1 root root     0 4月   1 11:52 appendonly.aof
    drwxr-xr-x. 2 root root   134 3月  31 20:51 bin
    -rw-r--r--. 1 root root    77 4月   1 11:54 dump.rdb
    -rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf
    [root@bogon redis]# rm -rf appendonly.aof 
    [root@bogon redis]# cd /root/
    [root@bogon ~]# ll
    总用量 1528
    -rw-------. 1 root root    1269 3月  31 23:56 anaconda-ks.cfg
    -rw-r--r--. 1 root root      88 4月   1 11:49 appendonly.aof
    drwxrwxr-x. 6 root root    4096 7月  28 2017 redis-3.2.10
    -rw-r--r--. 1 root root 1550261 7月  29 2017 redis-3.2.10.tar.gz
    [root@bogon ~]# cp appendonly.aof /usr/local/redis/
    [root@bogon ~]# cd /usr/local/redis/
    [root@bogon redis]# ll
    总用量 56
    -rw-r--r--. 1 root root    88 4月   1 11:55 appendonly.aof
    drwxr-xr-x. 2 root root   134 3月  31 20:51 bin
    -rw-r--r--. 1 root root    77 4月   1 11:54 dump.rdb
    -rw-r--r--. 1 root root 46690 4月   1 11:47 redis.conf
    [root@bogon redis]# bin/redis-server redis.conf 
    [root@bogon redis]# bin/redis-cli
    127.0.0.1:6379> keys *
    1) "test"
    2) "test1"

    我们发现 又有数据了

    小结: 我们平时可以把aof文件定期备份 然后需要的时候 拷贝到redis下 重启即可;

  • 相关阅读:
    P2590 [ZJOI2008]树的统计(树链剖分)
    【算法】线性排序
    【LeetCode每天一题】Median of Two Sorted Arrays(两数组中的中位数)
    【算法】归并排序
    【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
    【算法】快排
    【LeetCode每天一题】Add Two Numbers(两链表相加)
    【LeetCode每天一题】Two Sum(两数之和)
    【算法】选择排序
    【算法】插入排序
  • 原文地址:https://www.cnblogs.com/tenWood/p/8686148.html
Copyright © 2011-2022 走看看