更改 MongoDB 数据存储目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5961145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing MongoDB data store directory
提问by Mark Gill
Until now I have not been specifying a MongoDB data directory and have had only one 30 GB primary partition.
到目前为止,我还没有指定 MongoDB 数据目录,并且只有一个 30 GB 的主分区。
I just ran out of space and added a new hard disk. How can I transfer my data (that is apparently in /var/lib/mongodb/
) and configure MongoDB so that everything runs off of the new disk without affecting my existing installation?
我刚刚用完了空间并添加了一个新硬盘。如何传输我的数据(显然在 中/var/lib/mongodb/
)并配置 MongoDB,以便所有内容都从新磁盘运行,而不会影响我现有的安装?
回答by Brendan W. McAdams
The short answer is that the --dbpath
parameter in MongoDB will allow you to control what directory MongoDB reads and writes it's data from.
简短的回答是--dbpath
MongoDB中的参数将允许您控制 MongoDB 从哪个目录读取和写入数据。
mongod --dbpath /usr/local/mongodb-data
mongod --dbpath /usr/local/mongodb-data
Would start mongodb and put the files in /usr/local/mongodb-data
.
将启动 mongodb 并将文件放入/usr/local/mongodb-data
.
Depending on your distribution and MongoDB installation, you can also configure the mongod.conf
file to do this automatically:
根据您的发行版和 MongoDB 安装,您还可以配置mongod.conf
文件以自动执行此操作:
# Store data in /usr/local/var/mongodb instead of the default /data/db
dbpath = /usr/local/var/mongodb
The official 10gen Linux packages (Ubuntu/Debianor CentOS/Fedora) ship with a basic configuration file which is placed in /etc/mongodb.conf
, and the MongoDB service reads this when it starts up. You could make your change here.
官方的第 10 代 Linux 软件包(Ubuntu/Debian或CentOS/Fedora)附带一个基本配置文件,该文件放置在 中/etc/mongodb.conf
,MongoDB 服务在启动时读取该文件。你可以在这里进行更改。
回答by DmitrySandalov
Resolved it in 2 minutes downtime :)
Just move your folder, add symlink, then tune permissions.
在 2 分钟的停机时间内解决了它:)
只需移动您的文件夹,添加符号链接,然后调整权限。
sudo service mongod stop
sudo mv mongodb /new/disk/mongodb/
sudo ln -s /new/disk/mongodb/ /var/lib/mongodb
sudo chown mongodb:mongodb /new/disk/mongodb/
sudo service mongod start
# test if mongodb user can access new location:
sudo -u mongodb -s cd /new/disk/mongodb/
# resolve other permissions issues if necessary
sudo usermod -a -G <newdisk_grp> mongodb
回答by Yogesh Sanchihar
The following command will work for you, if you want to change default path. Just type this in bin directory of mongodb.
如果您想更改默认路径,以下命令将适用于您。只需在mongodb的bin目录中输入即可。
mongod --dbpath=yourdirectory\data\db
In case you want to move existing data too, then just copy all the folders from existing data\db directory to new directory before you execute the command.
如果您也想移动现有数据,则只需在执行命令之前将现有 data\db 目录中的所有文件夹复制到新目录。
And also stop existing mongodb services which are running.
并停止正在运行的现有 mongodb 服务。
回答by ivanbtrujillo
Create a file called mongod.cfg in MongoDB folder if you dont have it. In my case: C:\Users\ivanbtrujillo\MongoDB
如果没有,请在 MongoDB 文件夹中创建一个名为 mongod.cfg 的文件。就我而言:C:\Users\ivanbtrujillo\MongoDB
Then, edit mongod.cfg with notepad and add a line with the following (our custom dbpath):
然后,使用记事本编辑 mongod.cfg 并添加以下行(我们的自定义 dbpath):
dbpath=C:\Users\ivanbtrujillo\MongoDB\data\db
In this file you should especify the logpath too. My mongod.cfg file is:
在此文件中,您也应该指定日志路径。我的 mongod.cfg 文件是:
logpath=C:\Users\ivanbtrujillo\MongoDB\log\mongo.log
dbpath=C:\Users\ivanbtrujillo\MongoDB\data\db
If you uses mongoDB as a windows service, you have to change this key and especify the mongod.cfg file.
如果您使用 mongoDB 作为 Windows 服务,则必须更改此键并指定 mongod.cfg 文件。
To install mongodb as a windows service run this command:
要将 mongodb 安装为 Windows 服务,请运行以下命令:
**"C:\Users\ivanbtrujillo\MongoDB\bin\mongod.exe" --config "C:\Users\ivanbtrujillo\MongoDB\mongod.cfg" –install**
Open regedit.exe and go to the following route:
打开regedit.exe,进入如下路径:
HKEYLOCALMACHINE\SYSTEM\CurrentControlSet\services\MongoDB
MongoDB service does not work, we have to edit the ImagePath key, delete its content and put the following:
MongoDB 服务不起作用,我们必须编辑 ImagePath 键,删除其内容并输入以下内容:
**"C:\Users\ivanbtrujillo\MongoDB\bin\mongod.exe" --config "C:\Users\ivanbtrujillo\MongoDB\mongod.cfg"
--logpath="C:\Users\ivanbtrujillo\MongoDB\log\mongo.log" –service**
We indicates to mongo it's config file and its logpath.
我们向 mongo 指示它的配置文件和它的日志路径。
Then when you init the mongodb service, it works.
然后当您初始化 mongodb 服务时,它就可以工作了。
Here is a full tutorial to install mongoDB in windows: http://ivanbtrujillo.herokuapp.com/2014/07/24/installing-mongodb-as-a-service-windows/
这是在 Windows 中安装 mongoDB 的完整教程:http://ivanbtrujillo.herokuapp.com/2014/07/24/installing-mongodb-as-a-service-windows/
Hope it helps,
希望能帮助到你,
回答by lobster1234
Copy the contents of /var/lib/mongodb
to /data/db
. The files you should be looking for should have names like your_db_name.ns
and your_dbname.n
where n
is a number starting with 0. If you do not see such files under /var/lib/mongodb
, search for them on your filesystem.
将内容复制/var/lib/mongodb
到/data/db
. 你应该寻找的文件应该有名称,如 your_db_name.ns
和your_dbname.n
其中n
一个号码与0开始。如果你没有看到在这样的文件/var/lib/mongodb
,搜索它们在你的文件系统。
Once copied over, use --dbpath=/data/db
when starting MongoDB via the mongod
command.
复制后,--dbpath=/data/db
在通过mongod
命令启动 MongoDB 时使用。
回答by drinchev
Here is what I did, hope it is helpful to anyone else :
这是我所做的,希望对其他人有帮助:
Steps:
脚步:
- Stop your servicesthat are using mongodb
- Stop mongod- my way of doing this was with my rc file
/etc/rc.d/rc.mongod stop
, if you use something else, like systemd you should check your documentation how to do that - Create a new directoryon the fresh harddisk -
mkdir /mnt/database
- Make sure that mongodb has privileges to read / write from that directory( usually
chown mongodb:mongodb -R /mnt/database/mongodb
) - thanks @DanailGabenski. - Copy the data folder of your mongodb to the new location-
cp -R /var/lib/mongodb/ /mnt/database/
- Remove the old database folder-
rm -rf /var/lib/mongodb/
- Create symbolic link to the new database folder-
ln -s /mnt/database/mongodb /var/lib/mongodb
- Start mongod-
/etc/rc.d/rc.mongod start
- Check the logof your mongod and do some sanity checking ( try
mongo
to connect to your database to see if everything is all right ) - Start your servicesthat you stopped in point 1
- 停止使用 mongodb 的服务
- 停止 mongod- 我的方法是使用我的 rc 文件
/etc/rc.d/rc.mongod stop
,如果你使用其他东西,比如 systemd,你应该检查你的文档如何做到这一点 - 在新硬盘上创建一个新目录-
mkdir /mnt/database
- 确保 mongodb 具有从该目录(通常
chown mongodb:mongodb -R /mnt/database/mongodb
)读取/写入的权限- 感谢 @DanailGabenski。 - 将 mongodb 的数据文件夹复制到新位置-
cp -R /var/lib/mongodb/ /mnt/database/
- 删除旧的数据库文件夹-
rm -rf /var/lib/mongodb/
- 创建指向新数据库文件夹的符号链接-
ln -s /mnt/database/mongodb /var/lib/mongodb
- 启动mongod-
/etc/rc.d/rc.mongod start
- 检查您的 mongod的日志并进行一些完整性检查(尝试
mongo
连接到您的数据库以查看是否一切正常) - 启动您在第 1 点停止的服务
There is no need to tell that you should be careful when you do this, especialy with rm -rf
but I think this is the best way to do it.
没有必要告诉你这样做时应该小心,尤其是,rm -rf
但我认为这是最好的方法。
You should never try to copy database dir while mongod is running, because there might be services that write / read from it which will change the content of your database.
你不应该在 mongod 运行时尝试复制数据库目录,因为可能有服务从它那里写入/读取,这会改变你的数据库的内容。
回答by Roberto
If installed via apt-get
in Ubuntu 12.04, don't forget to chown -R mongodb:nogroup /path/to/new/directory
. Also, change the configuration at /etc/mongodb.conf
.
如果通过apt-get
Ubuntu 12.04安装,请不要忘记chown -R mongodb:nogroup /path/to/new/directory
. 此外,更改配置/etc/mongodb.conf
。
As a reminder, the mongodb-10gen
package is now started via upstart, so the config script is in /etc/init/mongodb.conf
提醒一下,这个mongodb-10gen
包现在是通过 upstart 启动的,所以配置脚本在/etc/init/mongodb.conf
I just went through this, hope googlers find it useful :)
我刚刚经历了这个,希望谷歌员工觉得它有用:)
回答by I P
For me, the user was mongod
instead of mongodb
对我来说,用户是mongod
而不是mongodb
sudo chown mongod:mongod /newlocation
sudo chown mongod:mongod /newlocation
You can see the logs for errors if the service fails:
如果服务失败,您可以查看错误日志:
/var/log/mongodb/mongod.log
/var/log/mongodb/mongod.log
回答by FGiorlando
In debian/ubuntu, you'll need to edit the /etc/init.d/mongodb script. Really, this file should be pulling the settings from /etc/mongodb.conf but it doesn't seem to pull the default directory (probably a bug)
在 debian/ubuntu 中,您需要编辑 /etc/init.d/mongodb 脚本。真的,这个文件应该从 /etc/mongodb.conf 中提取设置,但它似乎没有提取默认目录(可能是一个错误)
This is a bit of a hack, but adding these to the script made it start correctly:
这有点麻烦,但是将这些添加到脚本中可以使其正确启动:
add:
添加:
DBDIR=/database/mongodb
change:
改变:
DAEMON_OPTS=${DAEMON_OPTS:-"--unixSocketPrefix=$RUNDIR --config $CONF run"}
to:
到:
DAEMON_OPTS=${DAEMON_OPTS:-"--unixSocketPrefix=$RUNDIR --dbpath $DBDIR --config $CONF run"}