最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】设置redis的rdb文件的路径和文件名

redis crifan 7839浏览 0评论

之前已经实现了基本的redis+celery去实现异步任务和周期任务了。

但是发现默认的redis会在当前项目根目录下生成

dump.rdb

文件,觉得不妥,想要:

指定redis的rdb文件的名字和位置

放到此处的runtime下面:

redis dump.rdb location

redis dump.rdb / saving small files – Stack Overflow

<code>[root@xx-general-01 robotDemo]# ll
total 104
drwxr-xr-x 5 root root  4096 May 10 14:57 ai
-rw-r--r-- 1 root root 38986 May 24 11:57 app.py
-rw-r--r-- 1 root root     6 May 24 17:35 celerybeat.pid
-rw-r--r-- 1 root root  2064 May 24 17:34 config.py
-rw-r--r-- 1 root root   403 May 25 14:35 dump.rdb
-rw-r--r-- 1 root root  2005 Apr 24 16:36 gunicorn_config.py
drwxr-xr-x 2 root root  4096 May 24 17:30 logs
-rw-r--r-- 1 root root   303 May 11 17:22 Pipfile
-rw-r--r-- 1 root root 12454 May 11 17:24 Pipfile.lock
drwxr-xr-x 2 root root  4096 May 24 17:35 __pycache__
-rw-r--r-- 1 root root  2102 May  2 16:10 README.md
drwxr-xr-x 2 root root  4096 May 24 17:30 runtime
-rw-r--r-- 1 root root  2340 May 24 17:19 supervisord_server.conf
drwxr-xr-x 3 root root  4096 May 14 16:17 tmp
[root@xx-general-01 robotDemo]# pwd
/xx/robotDemo
[root@xx-general-01 robotDemo]#
[root@xx-general-01 robotDemo]#
[root@xx-general-01 robotDemo]# ps -e aux | grep redis
root     25025  0.0  0.0 142916  8016 ?        Sl   May24   1:12 /usr/bin/redis-server *:6379
root     26589  0.0  0.0 112664   972 pts/0    S+   15:15   0:00 grep --color=auto redis
[root@xx-general-01 robotDemo]# systemctl status redis
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           └─limit.conf
   Active: inactive (dead)
[root@xx-general-01 robotDemo]# systemctl status redis-server
Unit redis-server.service could not be found.
</code>

直接运行不行, 要cli进去后才能看到:

<code>[root@xx-general-01 robotDemo]# redis-cli CONFIG GET *
(error) ERR Wrong number of arguments for CONFIG GET
[root@xx-general-01 robotDemo]# pipenv shell
Warning: Your Pipfile requires python_version 3.6, but you are using 3.4.5 (/root/.local/share/v/r/bin/python).
  $ pipenv check will surely fail.
Spawning environment shell (/bin/bash). Use 'exit' to leave.
[root@xx-general-01 robotDemo]# . /root/.local/share/virtualenvs/robotDemo-dwdcgdaG/bin/activate
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# redis-cli CONFIG GET *
(error) ERR Wrong number of arguments for CONFIG GET
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# redis-cli --version
redis-cli 3.2.10
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# redis-cli
127.0.0.1:6379&gt; CONFIG GET *
  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) ""
...
</code>

看到是:

dbfilename=dump.rdb

<code>(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# echo "CONFIG GET *" | redis-cli | grep -e "dir" -e "dbfilename" -A1
dbfilename
dump.rdb
--
dir
/xx/robotDemo
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]#
</code>

redis – Where does dump.rdb belong? – Stack Overflow

还是要去找找redis.conf在哪,然后去修改配置文件才对

Ping Redis: How to get the rdb location, and config file location in redis.

参考:

linux – How do I load the dump.rdb file into redis? – Server Fault

去找到redis的conf后,去修改其中的dir

redis conf file

‎download.redis.io/redis-stable/redis.conf

Sample redis configuration

redis.conf 配置详解 – CSDN博客

redis conf file location

Redis Quick Start – Redis

默认位置好像是:

/etc/redis.conf

Start redis-server with config file – Stack Overflow

或者是:

/etc/redis/redis.conf

Where is the data directory in Redis? – Stack Overflow

<code>[root@xx-general-01 robotDemo]# ll /etc/re
redhat-lsb/          redhat-release       redis.conf           redis-sentinel.conf  resolv.conf          
[root@xx-general-01 robotDemo]# ll /etc/redis.conf
-rw-r----- 1 redis root 46729 Aug 18  2017 /etc/redis.conf
</code>

看到此处的:

vim /etc/redis.conf

中是:

<code>dir /var/lib/redis
</code>

好像是:

此处服务器中,redis运行的数据库,并没有放到当前项目的文件夹下,而是默认放到了:

/var/lib/redis

所以去确认看看

结果并没有,而

/var/lib/redis/

也是空的:

<code>[root@xx-general-01 robotDemo]# ll
total 104
drwxr-xr-x 5 root root  4096 May 10 14:57 ai
-rw-r--r-- 1 root root 38986 May 24 11:57 app.py
-rw-r--r-- 1 root root     6 May 24 17:35 celerybeat.pid
-rw-r--r-- 1 root root  2064 May 24 17:34 config.py
-rw-r--r-- 1 root root   403 May 25 16:35 dump.rdb
-rw-r--r-- 1 root root  2005 Apr 24 16:36 gunicorn_config.py
drwxr-xr-x 2 root root  4096 May 24 17:30 logs
-rw-r--r-- 1 root root   303 May 11 17:22 Pipfile
-rw-r--r-- 1 root root 12454 May 11 17:24 Pipfile.lock
drwxr-xr-x 2 root root  4096 May 24 17:35 __pycache__
-rw-r--r-- 1 root root  2102 May  2 16:10 README.md
drwxr-xr-x 2 root root  4096 May 24 17:30 runtime
-rw-r--r-- 1 root root  2340 May 24 17:19 supervisord_server.conf
drwxr-xr-x 3 root root  4096 May 14 16:17 tmp
[root@xx-general-01 robotDemo]# ll /var/lib/redis/
total 0
</code>

而再去看到:

<code>236 # The filename where to dump the DB
237 dbfilename dump.rdb
238  
239 # The working directory.
240 #
241 # The DB will be written inside this directory, with the filename specified
242 # above using the 'dbfilename' configuration directive.
243 #
244 # The Append Only File will also be created inside this directory.
245 #
246 # Note that you must specify a directory here, not a file name.
247 dir /var/lib/redis   
</code>

感觉是:

用supervisor去管理redis后:

<code>[program:redis]
directory=/xxx/robotDemo
command=/usr/bin/redis-server
</code>

使得working directory变成了当前项目根目录了:

/xxx/robotDemo

从而导致,在当前文件夹下生成了dump.rdb

所以问题好像转为:

redis在被supervisor管理后,通过directory切换了工作目录的问题了

如何在这种情况下,设置redis的工作目录

supervisor manage redis working directory

working directory redis inside supervisor

Setting up Nginx, Gunicorn, Celery, Redis, Supervisor, and Postgres with Django to run your Python…

Install and configure Redis, Supervisor and Worker for Faveo on Ubuntu 16.04 · ladybirdweb/faveo-helpdesk Wiki

install redis with supervisord

 

那是否可以通过此处运行redis-server时,在命令行中指定dir参数?

redis-server set dir

Change redis db location – Stack Overflow

Redis `dir` path changing. – Google Groups

redis-server command line pass parameter dir

Unable to set configuration parameter “save” through the command line. · Issue #2366 · antirez/redis

<code>redis-server  --dir /tmp
</code>

好像可以在命令行中指定dir参数的

去试试

之前也看过help:

<code>[root@xx-general-01 robotDemo]# redis-server --version
Redis server v=3.2.10 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=c8b45a0ec7dc67c6
[root@xx-general-01 robotDemo]# redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory &lt;megabytes&gt;

Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --slaveof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel
</code>

中的:

<code>./redis-server - (read config from stdin)
</code>

但是不确定如何传递参数

去试试

<code>command=/usr/bin/redis-server /etc/redis.conf --dir /root/naturling_20180101/web/server/robotDemo/runtime
</code>

结果,

(由于更新了supervisor的配置,所以要先去:

<code>supervisorctl reload
supervisorctl start all
</code>

就可以了:

<code>[root@xx-general-01 robotDemo]# pwd
/xxxserver/robotDemo
[root@xx-general-01 robotDemo]# ll
total 100
drwxr-xr-x 5 root root  4096 May 10 14:57 ai
-rw-r--r-- 1 root root 38986 May 24 11:57 app.py
-rw-r--r-- 1 root root     6 May 25 17:36 celerybeat.pid
-rw-r--r-- 1 root root  2064 May 24 17:34 config.py
-rw-r--r-- 1 root root  2005 Apr 24 16:36 gunicorn_config.py
drwxr-xr-x 2 root root  4096 May 24 17:30 logs
-rw-r--r-- 1 root root   303 May 11 17:22 Pipfile
-rw-r--r-- 1 root root 12454 May 11 17:24 Pipfile.lock
drwxr-xr-x 2 root root  4096 May 24 17:35 __pycache__
-rw-r--r-- 1 root root  2102 May  2 16:10 README.md
drwxr-xr-x 2 root root  4096 May 25 17:36 runtime
-rw-r--r-- 1 root root  2447 May 25 17:34 supervisord_server.conf
drwxr-xr-x 3 root root  4096 May 14 16:17 tmp
[root@xx-general-01 robotDemo]# ll runtime/
total 20
-rw-r--r-- 1 root root 16384 May 25 17:36 celerybeat-schedule
-rw-r--r-- 1 root root   184 May 25 17:36 dump.rdb
[root@xx-general-01 robotDemo]#
</code>

【总结】

此处,是supervisor去管理redis的进程的,之前配置是:

<code>[program:redis]
directory=/root/naturling_20180101/web/server/robotDemo
command=/usr/bin/redis-server
</code>

对应的redis的配置是:

/etc/redis.conf

<code>236 # The filename where to dump the DB
237 dbfilename dump.rdb
238  
239 # The working directory.
240 #
241 # The DB will be written inside this directory, with the filename specified
242 # above using the 'dbfilename' configuration directive.
243 #
244 # The Append Only File will also be created inside this directory.
245 #
246 # Note that you must specify a directory here, not a file name.
247 dir /var/lib/redis  
</code>

而此处dump.rdb却存在项目的根目录下了:

/xxxserver/robotDemo

而原因:

可能是:

由于用supervisor去管理,且redis启动之前,切换到了

directory设置的,当前项目跟目录了:

/xxx/robotDemo

导致此处

之前redis设置的:

<code>dir /var/lib/redis
</code>

的路径没有用了。

而此处想要更改dump.rdb的路径的话,最终是:

通过命令行的方式给redis-server去传递dir参数,具体写法是:

<code>command=/usr/bin/redis-server /etc/redis.conf --dir /xxx/robotDemo/runtime
</code>

即可达到自己的目的:

把redis的dump.rdb,放到当前项目下的runtime目录中。

转载请注明:在路上 » 【已解决】设置redis的rdb文件的路径和文件名

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.167 seconds, using 22.05MB memory