mongodb 如何设置 mongod --dbpath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21448268/
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
how to set mongod --dbpath
提问by vesperae
very new to mongodb and databases in general. whenever i run mongo
i receive this error message:
对 mongodb 和一般的数据库来说非常新。每当我运行时,mongo
我都会收到此错误消息:
MongoDB shell version: 2.4.9
connecting to: test
Thu Jan 30 13:03:33.170 Error: couldn't connect to server 127.0.0.1:27017
at src/mongo/shell/mongo.js:145
exception: connect failed
running mongod
i see this:
运行mongod
我看到这个:
Thu Jan 30 13:13:36.588 [initandlisten] MongoDB starting : pid=29408 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=Kimis-MacBook-Air-2.local
Thu Jan 30 13:13:36.588 [initandlisten]
Thu Jan 30 13:13:36.588 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Thu Jan 30 13:13:36.588 [initandlisten] db version v2.4.9
Thu Jan 30 13:13:36.588 [initandlisten] git version: nogitversion
Thu Jan 30 13:13:36.588 [initandlisten] build info: Darwin minilionvm.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Thu Jan 30 13:13:36.588 [initandlisten] allocator: tcmalloc
Thu Jan 30 13:13:36.588 [initandlisten] options: { bind_ip: "127.0.0.1", config: "/usr/local/etc/mongod.conf", dbpath: "/usr/local/var/mongodb" }
Thu Jan 30 13:13:36.591 [initandlisten] journal dir=/usr/local/var/mongodb/journal
Thu Jan 30 13:13:36.591 [initandlisten] recover begin
Thu Jan 30 13:13:36.591 [initandlisten] recover lsn: 108155770
Thu Jan 30 13:13:36.591 [initandlisten] recover /usr/local/var/mongodb/journal/j._0
Thu Jan 30 13:13:36.591 [initandlisten] journal file version number mismatch got:4147 expected:4149. if you have just upgraded, recover with old version of mongod, terminate cleanly, then upgrade.
Thu Jan 30 13:13:36.592 [initandlisten] dbexception during recovery: 13536 journal version number mismatch 16711
Thu Jan 30 13:13:36.592 [initandlisten] exception in initAndListen: 13536 journal version number mismatch 16711, terminating
Thu Jan 30 13:13:36.592 dbexit:
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to close listening sockets...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to flush diaglog...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to close sockets...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: waiting for fs preallocator...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: lock for final commit...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: final commit...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: closing all files...
Thu Jan 30 13:13:36.592 [initandlisten] closeAllFiles() finished
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: removing fs lock...
Thu Jan 30 13:13:36.592 dbexit: really exiting now
when i manually set my mongo dpath to mongod --dbpath /data/db
(as should be the default on installation) and keep it running in terminal everything runs fine. but once i close it, everything breaks again. my question is:
当我手动将 mongo dpath 设置为mongod --dbpath /data/db
(应该是安装时的默认值)并使其在终端中运行时,一切正常。但是一旦我关闭它,一切都会再次中断。我的问题是:
- why is my dbpath set to
/usr/local/var/mongodb
? - how do i fix this error so mongo works on my machine?
- 为什么我的 dbpath 设置为
/usr/local/var/mongodb
? - 我如何解决这个错误,以便 mongo 在我的机器上工作?
i'm assuming i either need to permanently set the dbpath to /data/db
or reconfigure something so it works with the dbpath as /usr/local/var/mongodb
我假设我需要将 dbpath 永久设置为/data/db
或重新配置某些内容,以便它与 dbpath 一起使用/usr/local/var/mongodb
i fairly new to unix commands as well so not entirely sure how to fix this error.
我对 unix 命令也很陌生,所以不完全确定如何解决这个错误。
thank you for any suggestions!
谢谢你的任何建议!
so, digging through the mongodb documentation i read this:
所以,通过 mongodb 文档挖掘我读到这个:
"Unless specified, mongod will look for data files in the default /data/db
directory. (Windows systems use the \data\db directory.) If you installed using a package management system. Check the /etc/mongodb.conf
file provided by your packages to see the configuration of the dbpath
."
“除非指定,mongod 将在默认/data/db
目录中查找数据文件。(Windows 系统使用 \data\db 目录。)如果您使用包管理系统安装。检查/etc/mongodb.conf
您的包提供的文件以查看dbpath
. ”
when i took a look at the files within /etc/
there is no mongodb.conf
file... however, i do see /usr/local/etc/mongod.conf
... so if i understand this correctly, i should create a file called /etc/mongodb.conf
and set the dbpath to /data/db
.
当我查看其中的文件时/etc/
,没有mongodb.conf
文件...但是,我确实看到了/usr/local/etc/mongod.conf
...所以如果我理解正确,我应该创建一个名为的文件 /etc/mongodb.conf
并将 dbpath 设置为/data/db
.
do i need to delete the stuff within /usr/local/
as well?
我也需要删除里面的东西/usr/local/
吗?
i think this has something to do with how my PATH are setup... could someone explain to me how to fix this in unix so i won't have this problem?
我认为这与我的 PATH 设置方式有关...有人可以向我解释如何在 unix 中解决这个问题,这样我就不会遇到这个问题吗?
thanks again!
再次感谢!
采纳答案by Neil Lunn
First you will have a config file in /etc/mongodb.conf
, therefore this sounds like a homebrew install which will use some more standardized paths. The whole /data/db/
thing is referenced in a lot of manual install documentation.
首先,您将有一个配置文件/etc/mongodb.conf
,因此这听起来像是自制软件安装,它将使用一些更标准化的路径。整个/data/db/
事情在很多手动安装文档中都有引用。
So basically from your log the server is not running, it's shutting down, so there is nothing for the shell to connect to. Seems like you have had some unclean shutdowns/restarts which has led to the inconsistency.
所以基本上从你的日志来看,服务器没有运行,它正在关闭,所以 shell 没有什么可以连接的。似乎您有一些不正常的关机/重启导致不一致。
Clear the files in the journal /usr/local/var/mongodb/journal/
on your config.
清除/usr/local/var/mongodb/journal/
配置中日志中的文件。
Also:
还:
sudo rm /var/lib/mongodb/mongod.lock
Just in case, even though that part looks clean. And then restart.
以防万一,即使那部分看起来很干净。然后重启。
回答by Sacha Nacar
Have only tried this on Mac:
只在 Mac 上试过这个:
- Create a data directory in the root folder of your app
- cd into your wherever you placed your mongo directory when you installed it
run this command:
mongod --dbpath ~/path/to/your/app/data
- 在您的应用程序的根文件夹中创建一个数据目录
- cd 到您安装 mongo 目录的任何位置
运行这个命令:
mongod --dbpath ~/path/to/your/app/data
You should be good to go!
你应该很高兴去!
回答by Andrey
You can set dbPath in the mongodb.conf file:
您可以在 mongodb.conf 文件中设置 dbPath:
storage:
dbPath: "/path/to/your/database/data/db"
It's a YAML-based configuration file format (since Mongodb 2.6 version), so pay attention no tabs only spaces, and space after ": "
它是一种基于 YAML 的配置文件格式(从 Mongodb 2.6 版本开始),所以注意没有制表符只有空格,以及“:”之后的空格
usually this file located in the *nix systems here: /etc/mongodb.conf
通常这个文件位于 *nix 系统中:/etc/mongodb.conf
So then just run
那么就运行
$ mongod -f /etc/mongodb.conf
And mongod process will start...
然后 mongod 进程将开始......
(on the Windows something like)
(在 Windows 上类似)
> C:\MongoDB\bin\mongod.exe -f C:\MongoDB\mongod.conf
回答by Gareth Thomas
For me it must have:
对我来说它必须有:
mongod --dbpath=/whatever/data/path
回答by aaditya
mongod --port portnumber --dbpath /path_to_your_folder
By default portnumber is 27017 and path is /var/lib/mongodb
默认端口号为 27017,路径为 /var/lib/mongodb
You can set your own port number and path where you want to keep all your database.
您可以设置自己的端口号和路径,以保存所有数据库。
回答by ABarb
You could also configure mongod to run on start up so that it is automatically running on start up and the dbpath is set upon configuration. To do this try:
您还可以将 mongod 配置为在启动时运行,以便它在启动时自动运行,并在配置时设置 dbpath。要做到这一点,请尝试:
mongod --smallfiles --config /etc/mongod.conf
mongod --smallfiles --config /etc/mongod.conf
The --smallfiles
tag is there in case you get an error with size. It is, of course, optional. Doing this should solve your problem while also automating your mongodb setup.
该--smallfiles
标签是没有的情况下,你会得到一个错误与大小。当然,它是可选的。这样做应该可以解决您的问题,同时还可以自动化您的 mongodb 设置。
回答by ABarb
Create a directory db in home, inside db another directory data
在home中创建一个目录db,在db里面创建另一个目录data
cd
mkdir db
cd db
mkdir data
then type this command--
然后输入这个命令——
mongod --dbpath ~/db/data
回答by phifa
sudo mongod --dbpath ~/data
This made it work for me.
这使它对我有用。
回答by Ted
Scenario:MongoDB(version v4.0.9).
场景:MongoDB(版本 v4.0.9)。
- Set custom folder(with name: myCustomDatabases), where to store databases.
- In custom folder(with name: myCustomDatabases), have to create database(with name: newDb).
- 设置自定义文件夹(名称:myCustomDatabases),存储数据库的位置。
- 在自定义文件夹(名称:myCustomDatabases)中,必须创建数据库(名称:newDb)。
Resolve:
解决:
Create custom folder(with name: myCustomDatabases):
D:>md myCustomDatabases
Run 'mongod --dbpath' with path to custom folder(with name: myCustomDatabases):
mongod --dbpath "D:\myCustomDatabases"
From another'cmd' run 'mongo':
D:>mongo
3.1. Show all databases, stored in custom folder(with name: myCustomDatabases):
>show dbs admin 0.000GB config 0.000GB local 0.000GB
3.2. Use database with name newDb:
> use newDb switched to db newDb
3.3. Show all databases, stored in custom folder(with name: myCustomDatabases):
>show dbs admin 0.000GB config 0.000GB local 0.000GB
!!! Noticed, that newDbis NOTin the list !!!
3.4. Have to create a collection with a document, which will create the database newDb.
> db.Cats.insert({name: 'Leo'}) WriteResult({ "nInserted" : 1 })
The insert({name: 'Leo'})operation creates: the database newDBand the collection Cats, because they do not exist.
3.5. Now the new created database newDbwill be displayed in the list.
> show dbs admin 0.000GB config 0.000GB local 0.000GB newDb 0.000GB
3.6. Now in custom folder D:\myCustomDatabases, have database newDb.
创建自定义文件夹(名称:myCustomDatabases):
D:>md myCustomDatabases
使用自定义文件夹的路径运行“mongod --dbpath” (名称:myCustomDatabases):
mongod --dbpath "D:\myCustomDatabases"
从另一个“cmd”运行“mongo”:
D:>mongo
3.1. 显示存储在自定义文件夹中的所有数据库(名称:myCustomDatabases):
>show dbs admin 0.000GB config 0.000GB local 0.000GB
3.2. 使用名为newDb 的数据库:
> use newDb switched to db newDb
3.3. 显示存储在自定义文件夹中的所有数据库(名称:myCustomDatabases):
>show dbs admin 0.000GB config 0.000GB local 0.000GB
!!!注意到,这NEWDB是不是在列表中!
3.4. 必须使用文档创建一个集合,这将创建数据库newDb。
> db.Cats.insert({name: 'Leo'}) WriteResult({ "nInserted" : 1 })
该插件({名称:“狮子”})操作创建:数据库NEWDB和收集猫,因为它们不存在。
3.5. 现在新创建的数据库newDb将显示在列表中。
> show dbs admin 0.000GB config 0.000GB local 0.000GB newDb 0.000GB
3.6. 现在在自定义文件夹D:\myCustomDatabases 中,有数据库newDb。
回答by Mezlini
very simple:
很简单:
sudo chown mongodb:mongodb /var/lib/mongodb/mongod.lock