node.js MongoDB 不工作。“错误:dbpath (/data/db) 不存在。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24599119/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:37:21  来源:igfitidea点击:

MongoDB not working. "ERROR: dbpath (/data/db) does not exist."

node.jsangularjsmongodbexpress

提问by user3810163

I'm getting the following error when I try to run "mongod" in the terminal. I've tried uninstalling, reinstalling, and restarting the machine. Any suggestions on how to get it working would be amazing.

当我尝试在终端中运行“mongod”时出现以下错误。我试过卸载、重新安装和重新启动机器。关于如何让它工作的任何建议都会很棒。

ERROR:

错误:

dbpath (/data/db) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo

Side note: Node also stopped working on my machine around the same time that I got this error.

旁注:在我收到此错误的同时,Node 也停止在我的机器上工作。

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: failed to connect to [localhost:27017]

Any help would be much appreciated!

任何帮助将非常感激!

回答by lpappone

This should work to ensure that the directory is set up in the right place so that Mongo can find it:

这应该可以确保目录设置在正确的位置,以便 Mongo 可以找到它:

sudo mkdir -p /data/db/

sudo mkdir -p /data/db/

sudo chown `id -u` /data/db

sudo chown `id -u` /data/db

回答by Muhammad Ali

You need to create the directory on root /data/db or set any other path with the following command :

您需要在根目录 /data/db 上创建目录或使用以下命令设置任何其他路径:

mongod --dbpath /srv/mongodb/

See the example link

查看示例链接

回答by gabrielhws

I solved the problem with :

我解决了这个问题:

sudo mongod --dbpath=/var/lib/mongodband then mongo to access the mongodb Shell.

sudo mongod --dbpath=/var/lib/mongodb然后 mongo 访问 mongodb Shell。

回答by Raugaral

Change the user of the new data directory:

更改新数据目录的用户:

chown mongodb [rute_directory]

chown mongodb [rute_directory]

And try another time to start the mongo service

再试一次启动mongo服务

service mongod start

服务 mongod 启动

I solve the same problem with this.

我用这个解决了同样的问题。

回答by Poikilos

Daemons (usually ending with d) are normally started as services. Starting the service (daemon) will allow mongodb to work as designed (without permission changes if integrates well with your distro). I start it using the service named mongodb instead of starting mongod directly--on distro with systemd enable on startup then run like:

守护进程(通常以 d 结尾)通常作为服务启动。启动服务(守护进程)将允许 mongodb 按设计工作(如果与您的发行版集成良好,则无需更改权限)。我使用名为 mongodb 的服务启动它,而不是直接启动 mongod——在启动时启用 systemd 的发行版,然后运行如下:

sudo systemctl enable mongodb    
sudo systemctl start mongodb

or, on distro with upstart (if you have /etc/init) or init (if you have /etc/init.d) ( https://www.tecmint.com/systemd-replaces-init-in-linux/) instead run:

或者,与新贵发行版(如果你的/ etc / INIT)或init(如果你有/etc/init.d中)(https://www.tecmint.com/systemd-replaces-init-in-linux/)而是运行:

sudo service mongodb enable
sudo service mongodb start

If you have a distro with rc ("run commands") such as Gentoo (settings in /etc/init.d) (https://forums.gentoo.org/viewtopic-t-854138-start-0.html) run:

如果您有一个带有 rc(“运行命令”)的发行版,例如 Gentoo(/etc/init.d 中的设置)(https://forums.gentoo.org/viewtopic-t-854138-start-0.html)运行:

rc-update add mongodb default 
/etc/init.d/mongodb start 

In a distro/version of FreeBSD which still has rc (check whether your version switched to systemd, otherwise see below):

在仍然有 rc 的 FreeBSD 发行版/版本中(检查您的版本是否切换到 systemd,否则请参见下文):

  • add the following line to /etc/rc.conf:

    mongod_enable="YES"

  • then:

    sudo service mongod start

  • 将以下行添加到 /etc/rc.conf:

    mongod_enable="是"

  • 然后:

    sudo service mongod start

After starting the service, an unpriveleged user can use mongo, and each user will have separate data.

启动服务后,非特权用户可以使用mongo,每个用户将拥有单独的数据。