对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

使用 Homebrew 运行 MongoDB 服务

MongoDB 提供了一个 Homebrew tap,允许您通过 brew 包经理安装 MongoDB Enterprise 和 MongoDB Community。如果您使用这些公式在 macOS 系统上安装了 MongoDB,您还可以通过 brew services 命令将 mongod 进程作为服务进行托管。

brew services <cmd> mongodb-enterprise[@VERSION]
命令
说明

info

显示 MongoDB 服务的状态。

restart

如果 MongoDB 服务正在运行,则停止该服务,再次启动它,然后重新注册它,以便在登录或启动时启动。

run

启动 MongoDB 服务。不会将其注册为在登录或启动时自动运行。

start

启动 MongoDB 服务并将其注册为在登录或启动时运行。

stop

停止 MongoDB 服务并将其从登录或启动时启动的服务注册表中删除。

重要提示:

要停止该进程但保留其在登录或启动时启动的注册,请使用 --keep 选项。

在通过 Homebrew 安装在 macOS 上的 MongoDB Enterprise 和 MongoDB Community 的自管理部署中,您可以通过 brew services 命令将 mongod 进程作为 Homebrew 服务进行管理。

有关所支持平台的详细信息,请参阅:

一些 Homebrew 服务命令(例如 start、stop 和 restart)会与一个注册表进行交互,该注册表通过 launchd 控制持久性服务。

使用用户级权限运行这些命令以更新每个用户的 launchd 代理。使用以下命令更新当前用户的注册表,并将其配置为在您登出时停止 MongoDB 服务,并在您重新登录时重新启动它:

brew services start mongodb-community@9.0

仅在单用户使用系统进行开发时,才使用此行为。在多用户环境中,如果每个用户都需要访问 MongoDB,此配置会阻止其他用户访问数据库。要配置注册表以使 MongoDB 在用户登出后保持运行,并在初创企业时而非登录时启动它,请使用 sudo 运行以下命令:

sudo brew services start mongodb-community@9.0

这会将注册表配置为在启动时启动 mongod,而不是等待特定用户登录。

Homebrew 允许您在单个系统上安装特定包的多个版本。如果您在未指定版本的情况下启动服务,Homebrew 将启动可用的最新版本。

brew services <cmd> mongodb-enterprise@9.0

在向 launchd 注册 MongoDB 服务时,请使用版本化公式语法。此语法可确保 Homebrew 继续使用指定版本启动 MongoDB 服务,直到您准备好过渡到下一个主要或次要版本。

brew services 将 mongod 日志消息定向到 Homebrew 前缀中的日志文件。默认日志文件为 $(brew --prefix)/var/log/mongodb/mongo.log。

以下示例管理 MongoDB Enterprise 9.0 的 Homebrew 服务。

要为当前会话启动 MongoDB 服务,请运行以下命令:

brew services run mongodb-enterprise@9.0

If you also want to start MongoDB for the current session and ensure that it restarts on login, see Start MongoDB on Login.

要显示 MongoDB 服务的状态,请运行以下命令:

brew services info mongodb-enterprise@9.0
mongodb-enterprise (sh.brew.mongodb-enterprise)
Running: true
Loaded: true
Schedulable: false
User: user.name
PID: 1845

要停止 MongoDB 服务并将其从初创企业注册表中删除,请运行以下命令:

brew services stop mongodb-enterprise@9.0

要重启 mongod 进程,请运行以下命令:

brew services restart mongodb-enterprise@9.0

此命令还会重新注册 Homebrew 服务,以便在登录时启动。

要启动 MongoDB 服务并将其注册到 Homebrew 以在登录时启动,请运行以下命令:

brew services start mongodb-enterprise@9.0

要在启动时启动 MongoDB 服务,请使用 sudo 运行上述命令。

To start MongoDB only for the current session and have it shut down when you log out or reboot, see Run MongoDB.

要显示 mongod 日志消息,请运行以下命令:

tail $(brew --prefix)/var/log/mongodb/mongo.log
{"t":{"$date":"2026-09-25T18:08:46.214+00:00"},"s":"I", "c":"STORAGE", "id":4615611, "ctx":"initandlisten", "msg":"MongoDB starting", "attr":{"pid":15432,"port":27017,"dbPath":"/usr/local/var/mongodb","architecture":"64-bit","host":"mac"}}
{"t":{"$date":"2026-09-25T18:08:46.214+00:00"},"s":"I", "c":"CONTROL", "id":51765, "ctx":"initandlisten", "msg":"Operating System", "attr":{"os":{"name":"macOS","version":"15.0"}}}
给本页内容打分