redisRDB持久化中dir路径解析

dir ./ 指定生成rdb文件的路径 相对路径受到启动redis的操作路径的影响。

redis读取rdb文件:

[root@localhost my-conf]# ls               
redis01.conf
[root@localhost my-conf]# redis-server redis01.conf 
19528:C 22 Jun 2021 17:08:19.985 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
19528:C 22 Jun 2021 17:08:19.985 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=19528, just started
19528:C 22 Jun 2021 17:08:19.985 # Configuration loaded
[root@localhost my-conf]# redis-cli -p 6380
127.0.0.1:6380> keys *
(empty list or set)
127.0.0.1:6380> set k1 v1
OK
127.0.0.1:6380> set k2 v2
OK
127.0.0.1:6380> set k3 v3
OK
127.0.0.1:6380> bgsave
Background saving started
127.0.0.1:6380> exit
[root@localhost my-conf]# ls
dump6380.rdb  redis01.conf
[root@localhost my-conf]# redis-server redis01.conf 
22585:C 22 Jun 2021 17:10:43.841 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
22585:C 22 Jun 2021 17:10:43.841 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=22585, just started
22585:C 22 Jun 2021 17:10:43.841 # Configuration loaded
[root@localhost my-conf]# redis-cli -p 6380
127.0.0.1:6380> keys *
1) "k1"
2) "k3"
3) "k2"
127.0.0.1:6380> 

以上操作:

1. 查看初始配置文件的目录
2. 在当前文件目录中启动redis
3. 向redis当中插入数据
4. 进行持久化
5. 查看生成的rdb文件
6. 重新启动并连接redis
7. 可以看到流程一切正确,数据存在
[root@localhost redis-direct]# pwd
/root/link/redis-direct
[root@localhost redis-direct]# cd my-conf/
[root@localhost my-conf]# pwd
/root/link/redis-direct/my-conf
[root@localhost my-conf]# cd ..
[root@localhost redis-direct]# ls
my-conf  own-help.txt  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis.conf  redis-sentinel  redis-server
[root@localhost redis-direct]# redis-server my-conf/redis01.conf 
29350:C 22 Jun 2021 17:16:01.313 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29350:C 22 Jun 2021 17:16:01.313 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=29350, just started
29350:C 22 Jun 2021 17:16:01.313 # Configuration loaded
[root@localhost redis-direct]# redis-cli -p 6380
127.0.0.1:6380> keys *
(empty list or set)
127.0.0.1:6380> 

以上操作在redis的启动位置上进行了改变,由原来的在my-conf目录下启动使用命令

​ redis-server redis6380.conf

变为了

​ redis-server my-conf/redis6380.conf

的启动命令,但是读取的是同样的配置文件,命令二读取不到持久化后的rdb文件。

原因:

配置文件中的配置

​ dir ./

当redis在启动的时候会从当前启动目录读取配置文件,由于dump.rdb文件是在命令一启动的时候生成的,也就是说当redis在命令一启动的时候读取rdb文件的路径为/root/link/redis-direct/my-conf,而命令2是在/root/link/redis-direct的目录下启动的,所以读取配置文件的位置为/root/link/redis-direct,没有rdb文件,启动自然就读取不到信息。

则会有两种假设情况

​ 1.在命令二启动后,重新生成rdb文件的位置应该在/root/link/redis-direct,命令一的配置文件并不会受影响

[root@localhost redis-direct]# clear
[root@localhost redis-direct]# ls
my-conf  own-help.txt  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis.conf  redis-sentinel  redis-server
[root@localhost redis-direct]# ls
my-conf  own-help.txt  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis.conf  redis-sentinel  redis-server
[root@localhost redis-direct]# redis-server my-conf/redis01.conf 
43653:C 22 Jun 2021 17:27:15.591 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
43653:C 22 Jun 2021 17:27:15.591 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=43653, just started
43653:C 22 Jun 2021 17:27:15.591 # Configuration loaded
[root@localhost redis-direct]# redis-cli -p 6380
127.0.0.1:6380> keys *
(empty list or set)
127.0.0.1:6380> set k4 v5
OK
127.0.0.1:6380> set k5 v6
OK
127.0.0.1:6380> bgsave
Background saving started
127.0.0.1:6380> exit
[root@localhost redis-direct]# ls
dump6380.rdb  my-conf  own-help.txt  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis.conf  redis-sentinel  redis-server
[root@localhost redis-direct]# redis-server my-conf/redis01.conf 
45534:C 22 Jun 2021 17:28:44.042 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
45534:C 22 Jun 2021 17:28:44.042 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=45534, just started
45534:C 22 Jun 2021 17:28:44.042 # Configuration loaded
[root@localhost redis-direct]# redis-cli -p 6380
127.0.0.1:6380> keys *
1) "k4"
2) "k5"

​ 2.将命令一所产生的rdb文件拷贝到/root/link/redis-direct目录下,那么在命令二启动的时候才会读取到命令一的数据

[root@localhost redis-direct]# redis-server my-conf/redis01.conf 
49999:C 22 Jun 2021 17:32:13.052 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
49999:C 22 Jun 2021 17:32:13.052 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=49999, just started
49999:C 22 Jun 2021 17:32:13.052 # Configuration loaded
[root@localhost redis-direct]# redis-cli -p 6380
127.0.0.1:6380> keys *
1) "k1"
2) "k2"
3) "k3"
127.0.0.1:6380> 

总结:

​ dir 配置路径如果是相对路径./XXX的话,也要注意启动的方式,可以使用绝对路径来进行持久化文件的保存

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐