如何在Mac OS X上启动MongoDB服务?

59

我已经在我的 Mac 上安装了 MongoDB,但是该进程当前未运行。 如何启动 MongoDB 服务,以便我可以开始使用命令?


1
这取决于您如何安装它。您是如何安装的? - Sergio Tulentsev
请在终端中尝试运行 mongod - w3debugger
11个回答

78
如果你在macOS上使用Homebrew安装MongoDB,可以在终端输入以下命令。对于大多数情况来说这应该就能解决问题了。
$ brew services start mongodb

6
你可以在/usr/local/etc/mongod.conf中调整配置。 - Pierre-Alexis de Solminihac
我会使用sudo来启动服务。我运行了上述命令,但应用程序无法连接到我的数据库。使用sudo,一切正常,并且服务在后台运行。 - Matthias Hagemann
7
如果你安装了社区版,可能需要使用 brew services start mongodb-community 命令来启动 MongoDB。 - yco

39

请在终端中尝试以下步骤:

which mongod

这将输出你的mongod路径,但如果它不在$PATH中,命令输出将为空。所以你需要找到可执行文件:


这将输出您的mongod路径,但如果它未在$PATH中,则命令输出将为空。因此,您需要查找可执行文件:
find / -name 'mongod'
在此命令的输出中,您将看到许多行,其中之一将类似于bin/mongod,例如/usr/local/mongodb/bin/mongod。在这种情况下,请获取整个绝对路径并执行以下操作:
echo "PATH=/usr/local/mongodb/bin/:$PATH" >> ~/.bash_profile
. ~/.bash_profile

然后再尝试一次:

mongod --dbpath /your/path

25

只需执行brew services start mongodb-community。示例:

$ brew services list
Name              Status  User Plist
mongodb-community stopped      
$ brew services start mongodb-community
==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)
当然,这取决于您的安装方式。这是典型的Homebrew安装方式。

20

现在是2021年,所以在Mac上启动mongodb:

使用brew

  • brew services start mongodb-community
    • 针对通过brew install mongodb-community安装的MongoDB

类似地:

指定版本

  • brew services start mongodb-community@4.2
    • 针对通过brew services install mongodb-community@4.2安装的MongoDB

仅运行而不需要开机自启

start意味着启动设置开机自启

只需运行当前版本,请使用run

  • brew services run mongodb-community

检查状态

启动后,请使用以下命令检查状态:

  • brew services

手动操作

对于mongodb旧版本,命令为mongod

  • 直接运行:mongodb
  • 使用配置文件运行:mongod --config /usr/local/etc/mongod.conf
    • 默认配置文件路径为:/usr/local/etc/mongod.conf
  • 仅传递一些参数运行:
    • 使用数据库路径:mongod --dbpath /data
      • /data 是常见的mongodb数据库路径

5

进入一个你想让mongodb存储所有数据库文件的文件夹并运行以下命令:

mongod --dbpath=.


我尝试在命令终端中使用mongod,但它显示“-bash:mongod:找不到命令”,你有任何想法为什么会出现这种情况吗? - nshetty
@nshetty:是的,因为mongod不在您的$PATH中。 - Sergio Tulentsev

4

要启动mongodb,您只需要在终端上运行“mongod”命令。


我尝试在命令终端中使用mongod,但它显示“-bash: mongod: command not found”。对此非常陌生,如果这是非常基础的问题,请原谅。 - nshetty

4

使用Homebrew安装和运行MongoDB

打开终端应用程序并输入

brew update

更新Homebrew后

brew install mongodb-community@4.0

下载Mongo后,创建“db”目录。这是Mongo数据文件存放的位置。 您可以通过运行以下命令在默认位置创建目录

sudo mkdir -p /data/db

确保/data/db目录具有正确的权限,运行以下命令

sudo chown -R id -un /data/db

运行Mongo守护进程,在一个终端窗口中运行

brew services start mongodb-community

这将启动Mongo服务器。 运行Mongo shell,使用Mongo守护进程在一个终端中运行mongo。这将运行Mongo shell,一种访问MongoDB中的数据的应用程序。 要退出Mongo shell,请运行quit() 要停止Mongo守护进程,请按Ctrl-C键

启动服务:

brew services start mongo

https://treehouse.github.io/installation-guides/mac/mongo-mac.html


3

对于mongodb-community@4.2

启动请运行brew services start mongodb-community@4.2

停止请运行brew services stop mongodb-community@4.2

但是如果您不知道具体的版本号,可以省略指定版本号。

来源:链接


0

使用这行代码可以解决问题。

在mongo bin路径下,在终端中执行此行代码:

export PATH=<mongodb-install-directory>/bin:$PATH

<mongodb-install-directory> 请替换为您的路径,例如:

export PATH=/Application/Mongo/bin:$PATH

更好的方法:https://medium.com/@saurabhkumar_4718/install-mongodb-without-homebrew-on-mac-os-2a98b68ab09c - Philip Enc

0

使用以下命令检查已安装的服务:

brew services list

也可以直接使用以下方式进行检查:

which mongod

获取已安装服务的名称并通过名称启动服务:
brew services start <service_name>

例如:

如果服务名称是mongodb

brew services start mongodb

如果已安装社区版
brew services start mongodb-community

针对特定版本:

如果已安装mongodb-community@4.4

brew services start mongodb-community@4.4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接