大家好,今天分享一下Redis 数据库aof持久化的操作

AOF(Append Only File ):将我们的命令都记录下来,恢复的时候就把这个文件再执行一遍

redis rightpop 丢消息 redis appendonly no_数据持久化

aof 保存的是appendonly.aof 文件

[root@localhost redistest]# vim redis.conf

redis rightpop 丢消息 redis appendonly no_数据持久化_02


它的配置文件在这里

注意这个
(默认是关闭)

redis rightpop 丢消息 redis appendonly no_数据持久化_03

这里也要注意一下

redis rightpop 丢消息 redis appendonly no_数据持久化_04

把这个改成yes就可以使用aof了

redis rightpop 丢消息 redis appendonly no_AOF_05

这个时候可以看见有appendonly.aof这个文件

redis rightpop 丢消息 redis appendonly no_AOF_06

重启数据库

127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@localhost bin]#

再次进入数据库

[root@localhost bin]# redis-server redistest/redis.conf 
7526:C 23 Mar 2022 12:39:19.261 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7526:C 23 Mar 2022 12:39:19.261 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=7526, just started
7526:C 23 Mar 2022 12:39:19.261 # Configuration loaded
[root@localhost bin]# redis-cli  -p 6379

创建一些数据

127.0.0.1:6379> set name zhan
OK
127.0.0.1:6379> set age 1
OK
127.0.0.1:6379> set like dianshiju
OK
127.0.0.1:6379>

查看这个文件的内容:(这就是我们刚刚创建的文件)

[root@localhost bin]# cat appendonly.aof 
*2
$6
SELECT
$1
0
*3
$3
set
$4
name
$4
zhan
*3
$3
set
$3
age
$1
1
*3
$3
set
$4
like
$9
dianshiju

我们在下面给它做一些人为性的破坏

关掉数据库

127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@localhost bin]#

人为改动这个文件

[root@localhost bin]# vim appendonly.aof

redis rightpop 丢消息 redis appendonly no_数据库_07


这是我们随便添加的内容

运行数据库

[root@localhost bin]# redis-server redistest/redis.conf 
7762:C 23 Mar 2022 12:54:10.888 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7762:C 23 Mar 2022 12:54:10.888 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=7762, just started
7762:C 23 Mar 2022 12:54:10.888 # Configuration loaded
[root@localhost bin]#

会发现我们的数据库已经连接不上去了

[root@localhost bin]# redis-cli  -p 6379
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>

所以当appendonly.aof 有问题,我们是无法连接数据库的

这个时候,我们在bin 目录下

redis rightpop 丢消息 redis appendonly no_redis rightpop 丢消息_08


看见redis-check-aof这个文件,它是用来检查或恢复我们的appendonly.aof 这个文件的

使用redis-check-aof对appendonly.aof 进行修复

[root@localhost bin]# redis-check-aof  --fix appendonly.aof 
0x              70: Expected \r\n, got: 3230
AOF analyzed: size=152, ok_up_to=85, diff=67
This will shrink the AOF from 152 bytes, with 67 bytes, to 85 bytes
Continue? [y/N]: y
Successfully truncated AOF
[root@localhost bin]#

这样就算修复完成了

查看appendonly.aof 这个文件的内容

发现刚刚的改动已经没有了

[root@localhost bin]# cat appendonly.aof 
*2
$6
SELECT
$1
0
*3
$3
set
$4
name
$4
zhan
*3
$3
set
$3
age
$1
1
[root@localhost bin]#

重新启动数据库

[root@localhost bin]# redis-server redistest/redis.conf 
7972:C 23 Mar 2022 13:09:22.904 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7972:C 23 Mar 2022 13:09:22.904 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=7972, just started
7972:C 23 Mar 2022 13:09:22.904 # Configuration loaded
[root@localhost bin]#

我们已经成功连接数据库

[root@localhost bin]# redis-cli -p 6379
127.0.0.1:6379>

aof 数据持久化的优点:

1.每一次的数据修改,都会同步,这样的话,数据的完整性会好很多

缺点:
1.从数据文件的角度上看,aof文件的大小远远大于rdb,所以它的速度也会相对较慢

这里有一个重写的配置

redis rightpop 丢消息 redis appendonly no_redis_09


它的意思就是说,如果aof文件大于64mb,redis就会fork一个新的进程将我们的文件进行重写

aof 就是使用文件的无限追加,这就会导致文件越来越大

好了,有关于Redis 数据库aof持久化的操作就到这里了,谢谢大家