使用Homebrew安装MongoDB

152

我对MongoDB比较新,正在尝试使用Homebrew在我的Mac上安装MongoDB,但是我遇到了以下错误:

Error: No available formula with the name "mongodb" 
==> Searching for a previously deleted formula (in the last 
month)...
Warning: homebrew/core is shallow clone. To get complete history 
run:
  git -C "$(brew --repo homebrew/core)" fetch --unshallow

Error: No previously deleted formula found.
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.

我运行了brew update,然后运行brew install mongodb


1
这并不是真正的错误,你正在尝试安装一个不存在的软件包(公式)。你有没有搜索解决方案?官方文档中有正确的命令行。 - bfontaine
1
似乎 $ brew install mongodb-community@5.0 目前还无法通过 brew 安装。请尝试 $ brew install mongodb-community@4.4。同时确保在启动和停止 mongodb 时指定正确的版本。 - Pritam Patil
9个回答

448

公式mongodb已从Homebrew核心中删除。请查看来自homebrew-core的pr-43770

给我们的用户:如果您因为mongodb停止工作而来到这里,我们已经将其从Homebrew核心公式中移除,因为它已迁移到非开源许可证下。

幸运的是,mongodb团队正在维护自定义Homebrew tap。您可以从新的tap卸载旧的mongodb并重新安装新的mongodb。

# If you still have the old mongodb installed from homebrew-core
brew services stop mongodb
brew uninstall homebrew/core/mongodb

# Use the migrated distribution from custom tap
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-community

查看mongodb/homebrew-brew获取更多信息。


1
它只能通过 sudo brew services start mongodb-community 工作。如果没有 sudo,我的应用程序永远无法连接到数据库,因为mongo默认使用 /data/db 路径,而不管其配置如何。 - Matthias Hagemann
2
@MattHagemann 你可能把事情搞砸了。默认的数据库位置是 /usr/lobal/var/mongodb,可以通过 brew cat mongodb-comunnity 来确认。不要使用带有 sudo 的 brew,这会改变软件包文件的所有者,并给你带来更多麻烦。我曾经在这里解释过 sudo brew 的坏处:https://dev59.com/BLfna4cB1Zd3GeqPkwEJ#58487534。 - Simba
@Simba 谢谢你的警告。我已经修复了所有权,并且可以在不使用 sudo 的情况下启动服务。原来 dbPath 内部的 WiredTiger.turtle(和其他文件)仍然属于 root 所有,并且应该属于我的 ID。 - Matthias Hagemann
1
brew install mongodb-community is changed to brew install mongodb-community@4.4 - user13622589

23
关于macOS Big SurHomebrew,mongodb文档表示:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ 您应该安装支持macOS 10.13或更高版本的MongoDB 4.4社区版,因此这些步骤将有所帮助。
解决方法1: 如果您之前安装了旧版本的公式,则可能会遇到ChecksumMismatchError错误,请执行以下操作以修复:
删除已下载的.tgz存档文件。
brew untap mongodb/brew && brew tap mongodb/brew

重新调整公式。

  brew install mongodb-community@4.4 

解决方案2:

如果您没有安装任何版本的公式。

1、从https://brew.sh/#install安装Xcode命令行工具和Homebrew。

xcode-select --install

2、点击MongoDB Homebrew Tap:

brew tap mongodb/brew

3、在macOS终端中验证安装先决条件:

brew tap | grep mongodb

4、安装 MongoDB

brew install mongodb-community@4.4

注意:安装包含:
• mongod服务器,
• mongos分片集群查询路由器,
• mongo shell
请参考此截图:

enter image description here

最后,要将MongoDB(即mongod进程)作为macOS服务运行,请执行以下操作:

brew services start mongodb-community@4.4

截图:

enter image description here


1
它可以运行,但尝试安装 mongodb-community@5.0 时无法运行。 - Viraj Singh
安装后如何访问Mongo shell? - duhaime

16

首先安装 MongoDB

brew tap mongodb/brew

其次,使用以下命令进行安装。mangodb已成功安装。
brew install mongodb-community@4.0 

您将获得输出

==> CaveatsTo have launchd start mongodb/brew/mongodb-community now and restart at login:
  brew services start mongodb/brew/mongodb-community
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf
==> Summary
  /usr/local/Cellar/mongodb-community/4.2.2: 21 files, 274.5MB, built in 2 minutes 46 seconds
brew services start mongodb/brew/mongodb-community
==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-commu

8

请在您的终端中尝试此代码:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 

接着:

brew tap mongodb/brew  

最后:

brew install mongodb-community@4.0

2
您可能还需要将 mongo 可执行文件添加到您的路径中或将其添加为别名,例如 export PATH=$PATH:/usr/local/Cellar/mongodb-community@4.0/4.0.13/bin/alias mongo=/usr/local/Cellar/mongodb-community@4.0/4.0.13/bin/mongo - Everett
1
那么如何运行Mongo守护进程?我的意思是类似于“mongod”的命令? - Hajar Elkoumikhi

1
只需开始

brew tap mongodb/brew


1
~ % brew services run mongodb-community
/usr/local/opt/mongodb-community/homebrew.mxcl.mongodb-community.plist: service already loaded

Error: Failure while executing
/bin/launchctl bootstrap gui/501 /usr/local/opt/mongodb-community/homebrew.mxcl.mongodb-community.plist
exited with 17.

1
你的回答可以通过添加更多支持性信息得到改进。请编辑以添加进一步细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0

在我的MacOS Catalina上,它不能正常工作。

安装后(https://zellwk.com/blog/install-mongodb/),我不得不添加对“/tmp/mongodb-27017.sock”的权限。

sudo chown -R `id -un` /tmp/mongodb-27017.sock

而且命令“mongod”似乎忽略了“/usr/local/etc”中的配置文件(mongod.conf),因此我必须始终使用dbpath参数启动它。

mongod --dbpath /usr/local/var/mongodb

即使在配置文件中指定了相同的路径。

对我来说,使用配置文件和 "mongod" 也不起作用。

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

或者

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

我通过使用brew services启动mongodb解决了所有这些问题。这样做没有任何问题,并且从正确的配置文件中获取参数。

brew services run mongodb-community

0
如果您需要Mongo Dump和其他一些内容,请考虑使用这个:
brew tap mongodb/brew
brew install mongodb-database-tools

这给了我:
mongodump     mongoexport   mongofiles    mongoimport   mongorestore  mongostat     mongotop

下面

/usr/local/Cellar/mongodb-database-tools/100.7.0/bin/

并且已经创建了符号链接到

/usr/local/bin

-1

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