在 linux/OS X 上启动 mongod 服务的正确方法是什么?

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

What is the correct way to start a mongod service on linux / OS X?

linuxmongodbserviceinitialization

提问by Alex C

I've installed mongodb and have been able to run it, work with it, do simple DB read / write type stuff. Now I'm trying to set up my Mac to run mongod as a service.

我已经安装了 mongodb 并且能够运行它,使用它,做简单的 DB 读/写类型的东西。现在我正在尝试设置我的 Mac 以将 mongod 作为服务运行。

I get "Command not found" in response to:

我收到“找不到命令”以回应:

 init mongod start

In response to:

回应:

~: service mongod start
service: This command still works, but it is deprecated. Please use launchctl(8) instead.
service: failed to start the 'mongod' service

And if I try:

如果我尝试:

~: launchctl start mongod
launchctl start error: No such process

So obviously I'm blundering around a bit. Next step seems to be typing in random characters until something works. The command which doeswork is: mongod --quiet &I'm not sure, maybe that is the way you're supposed to do it? Maybe I should just take off 'quiet mode' and add > /logs/mongo.logto the end of the command line?

所以很明显我有点胡思乱想。下一步似乎是输入随机字符,直到某些东西起作用为止。该命令的工作是:mongod --quiet &我不知道,也许是你的方式该怎么办呢?也许我应该取消“安静模式”并添加> /logs/mongo.log到命令行的末尾?

I'm building a development environment on a Mac with the intention of doing the same thing on a linux server. I'm just not sure of the Bash commands. All the other searches I do with trying to pull up the answer give me advice for windows machines.

我正在 Mac 上构建一个开发环境,目的是在 linux 服务器上做同样的事情。我只是不确定 Bash 命令。我试图找出答案的所有其他搜索都为我提供了有关 Windows 机器的建议。

Perhaps someone knows the linux version of the commands?

也许有人知道命令的 linux 版本?

Thanks very much

非常感谢

采纳答案by Brendan W. McAdams

With recent builds of mongodb community edition, this is straightforward.

使用最近构建的 mongodb 社区版,这很简单。

When you install via brew, it tells you what exactly to do. There is no need to create a new launch control file.

当您通过 brew 安装时,它会告诉您究竟要做什么。无需创建新的启动控制文件。

$ brew install mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.0.6.yosemite.bottle.tar.gz ### 100.0%
==> Pouring mongodb-3.0.6.yosemite.bottle.tar.gz
==> Caveats
To have launchd start mongodb at login:
  ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
Then to load mongodb now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Or, if you don't want/need launchctl, you can just run:
  mongod --config /usr/local/etc/mongod.conf
==> Summary
  /usr/local/Cellar/mongodb/3.0.6: 17 files, 159M

回答by Scott

I did a bit of looking around on the Mac side. You may want to use the installer hereas it looks like it does all the setup for you to automatically launch on Mac OS. The only downside is it looks like it's using a pretty old mongo version.

我在 Mac 方面做了一些环顾四周。您可能希望在此处使用安装程序因为它似乎完成了所有设置,以便您在 Mac OS 上自动启动。唯一的缺点是它看起来像是使用了一个非常旧的 mongo 版本。

This link herealso explains the setup to get mongo automatically launching as a background service on the Mac.

这里的这个链接还解释了在 Mac 上让 mongo 作为后台服务自动启动的设置。

回答by Mario Alemi

Edit: you should now use brew services start mongodb, as in Gergo's answer...

编辑:您现在应该使用brew services start mongodb,如 Gergo 的回答...

When you install/upgrade mongodb, brew will tell you what to do:

当你安装/升级 mongodb 时,brew 会告诉你该怎么做:

To have launchd start mongodb at login:

要在登录时启动 mongodb:

    ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents

Then to load mongodb now:

然后现在加载mongodb:

    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

Or, if you don't want/need launchctl, you can just run:

或者,如果您不想要/不需要launchctl,您可以运行:

    mongod

It works perfectly.

它完美地工作。

回答by koffster

If you feel like having a simple gui to fix this (as I do), then I can recommend the mongodb pref-pane. Description: https://www.mongodb.com/blog/post/macosx-preferences-pane-for-mongodb

如果你想要一个简单的 gui 来解决这个问题(就像我一样),那么我可以推荐 mongodb pref-pane。说明:https: //www.mongodb.com/blog/post/macosx-preferences-pane-for-mongodb

On github: https://github.com/remysaissy/mongodb-macosx-prefspane

在 github 上:https: //github.com/remysaissy/mongodb-macosx-prefspane

回答by Gergo Erdosi

Homebrew's servicestap integrates formulas with the launchctlmanager. Adding it is easy:

Homebrew 的services水龙头将公式与launchctl管理器集成在一起。添加它很容易:

brew tap homebrew/services

You can then launch MongoDB with this command (this will also start mongodb on boot):

然后,您可以使用此命令启动 MongoDB(这也将在启动时启动 mongodb):

brew services start mongodb

You can also use stopor restart:

您还可以使用stoprestart

brew services stop mongodb
brew services restart mongodb

回答by Rejeev Divakaran

mongod --dbpath [path_to_data_directory]

mongod --dbpath [path_to_data_directory]

回答by Ardi Bello

Just installed MongoDB via Homebrew. At the end of the installation console, you can see an output as follows:

刚刚通过 Homebrew 安装了 MongoDB。在安装控制台的最后,您可以看到如下输出:

To start mongodb:

启动mongodb:

brew services start mongodb

Or, if you don't want/need a background service you can just run:

或者,如果您不想要/不需要后台服务,您可以运行:

mongod --config /usr/local/etc/mongod.conf

So, brew services start mongodb, managed to run MongoDB as a service for me.

因此,brew services 启动 mongodb,设法为我运行 MongoDB 作为服务。

回答by MehdiPRG

First Step

第一步

install mongodb in your linux machine with

在你的 linux 机器上安装 mongodb

apt install mongodb-client && apt install mongodb-server

apt 安装 mongodb-client && apt 安装 mongodb-server

second step is

第二步是

change the database path instead of your system default path if you want.
so do the following steps and change it for yourself.

如果需要,更改数据库路径而不是系统默认路径。
因此,请执行以下步骤并自行更改。

mongod --directoryperdb --dbpath /var/lib/mongodb/data/db --logpath /var/lib/mongodb/log/mongodb.log --logappend --rest

and in your windows machine do it just like that just put an --install flag. you have to get a successful message.

并在您的 Windows 机器中这样做,只需放置一个 --install 标志即可。你必须得到一个成功的消息。

Best Regards...

此致...

回答by NOTiFY

On macOS 10.13.6 with MongoDB 4.0

在 macOS 10.13.6 和 MongoDB 4.0 上

I was unable to connect to localhost from the mongo shell

我无法从 mongo shell 连接到 localhost

I started MongoDB with:

我开始使用 MongoDB:

mongod --config /usr/local/etc/mongod.conf

mongod --config /usr/local/etc/mongod.conf

I found that the 'mongod.conf' had:

我发现'mongod.conf'有:

bindIp: 127.0.0.1

绑定IP:127.0.0.1

Change my JavaScript connection from localhostto 127.0.0.1and it worked fine.

将我的 JavaScript 连接从localhost更改为127.0.0.1,它运行良好。

The same was occurring with MongoDB Compass too.

MongoDB Compass 也发生了同样的情况。

回答by briami

mongodwasn't working to start the daemon for me but after I ran the following, it started working:

mongod没有为我启动守护程序,但在我运行以下命令后,它开始工作:

'mongod --fork --logpath /var/log/mongodb.log'

(from here: https://docs.mongodb.com/manual/tutorial/manage-mongodb-processes/)

(从这里:https: //docs.mongodb.com/manual/tutorial/manage-mongodb-processes/