注意
MongoDB Atlas
MongoDB Atlas is a hosted MongoDB service option in the cloud which requires no installation overhead and offers a free cluster to get started.
Overview
使用本教程,利用下载的.tgz tarball,即可在 macOS 上手动安装 MongoDB 7.0 Community Edition。
MongoDB 版本
本教程安装的是 MongoDB 7.0 Community Edition。想要安装不同版本的 MongoDB Community Edition,请移步本页面左上角的版本下拉菜单,选择该版本对应的文档进行参照。
安装方法
虽然您可以按照本文档所述使用下载的 .tgz Tarball 手动安装MongoDB ,但我们建议您尽可能在系统上使用 brew包管理器。使用包管理器会自动安装所需的所有依赖项,提供示例mongod.conf文件,并简化未来的升级和维护任务。
➤ 有关说明,请参阅使用 brew 软件包管理器安装 MongoDB。
Considerations
MongoDB Shell、 mongosh
使用 .tgz 包安装服务器时,您需要按照 mongosh 安装说明单独下载和安装 mongosh。
平台支持
MongoDB 7.0Community Edition 支持 macOS 14 或更高版本,并且需要 Haswell 或更新的 CPU 架构。
有关更多信息,请参阅平台支持。
生产说明
在生产环境中部署MongoDB之前,请参阅《自管理部署的生产说明》文档,了解性能注意事项和配置建议。
安装 MongoDB Community Edition
要从 .tgz 上手动安装 MongoDB Community Edition,请选择与您的 Mac 处理器对应的标签页并完成以下步骤:
确保二进制文件位于 PATH 环境变量中列出的目录下。
MongoDB 二进制文件位于 tarball 的 bin/ 目录中。您可以执行以下任一操作:
将二进制文件复制到
PATH变量中列出的目录中,例如/usr/local/bin。将/path/to/the/mongodb-directory/替换为您的安装目录。sudo cp /path/to/the/mongodb-directory/bin/* /usr/local/bin/ 创建指向
PATH变量中所列目录的二进制文件的符号链接,例如/usr/local/bin。将/path/to/the/mongodb-directory/替换为您的安装目录。sudo ln -s /path/to/the/mongodb-directory/bin/* /usr/local/bin/
确保二进制文件位于 PATH 环境变量中列出的目录下。
MongoDB 二进制文件位于 tarball 的 bin/ 目录中。您可以执行以下任一操作:
将二进制文件复制到
PATH变量中列出的目录中,例如/usr/local/bin。将/path/to/the/mongodb-directory/替换为您的安装目录。sudo cp /path/to/the/mongodb-directory/bin/* /usr/local/bin/ 创建指向
PATH变量中所列目录的二进制文件的符号链接,例如/usr/local/bin。将/path/to/the/mongodb-directory/替换为您的安装目录。sudo ln -s /path/to/the/mongodb-directory/bin/* /usr/local/bin/
运行 MongoDB Community Edition
- ulimit 注意事项
大多数类 Unix 操作系统都会限制进程可以使用的系统资源。这些限制可能会对 MongoDB 操作产生负面影响,应该进行调整。请参阅UNIX ulimit 自管理部署设置,了解适用于您平台的推荐设置。
注意
如果打开文件数的
ulimit值低于64000,MongoDB 会生成初创企业警告。
步骤
请按照以下步骤运行 MongoDB Community Edition。 这些说明假设您使用的是默认设置。
创建数据目录。
首次启动 MongoDB 之前,必须创建 mongod 进程写入数据的目录。
例如,要创建 ~/data/db 目录:
sudo mkdir -p ~/data/db
运行 MongoDB。
要运行 MongoDB,请在系统提示符下运行 mongod 进程,提供上面两个参数 dbpath 和 logpath,以及用于在后台运行 mongod 的 fork 参数。或者,您也可以选择将 dbpath、logpath、fork 以及其他许多参数的值存储在配置文件中。
mongod使用命令行参数运行
在系统提示符下运行 mongod 进程,直接在命令行上提供三个必要参数:
mongod --dbpath ~/data/db --logpath ~/data/log/mongodb/mongo.log --fork
使用配置文件运行 mongod
在系统提示符下运行 mongod 进程,提供包含 config 参数的配置文件的路径:
mongod --config /usr/local/etc/mongod.conf
macOS 可能会阻止 mongod 在安装后运行。如果在启动 mongod 时出现安全错误,表明无法识别或验证开发者的身份,请执行以下操作以授予 mongod 的运行权限:
打开系统偏好设置
选择“安全和隐私”窗格。
在通用标签页下,单击
mongod相关消息右侧的按钮,该按钮标有 Open Anyway 或 Allow Anyway,具体取决于您的 macOS 版本。
开始使用 MongoDB。
Start a mongosh session on the same host machine as the mongod. You can run mongosh without any command-line options to connect to a mongod that is running on your localhost with the default port of 27017:
mongosh
For more information on connecting using mongosh, such as to connect to a mongod instance running on a different host and/or port, see the mongosh documentation.
为了帮助您开始使用 MongoDB,MongoDB 提供了各种驱动程序版本的入门指南。有关可用版本,请参阅 MongoDB 入门。
更多信息
默认绑定本地主机
By default, MongoDB launches with bindIp set to 127.0.0.1, which binds to the localhost network interface. This means that the mongod can only accept connections from clients that are running on the same machine. Remote clients will not be able to connect to the mongod, and the mongod will not be able to initialize a replica set unless this value is set to a valid network interface which is accessible from the remote clients.
该值可通过以下任一方式配置:
警告
将实例绑定到可公开访问的 IP 地址之前,必须保护集群免遭未经授权的访问。有关安全建议的完整列表,请参阅自管理部署的安全清单。至少应考虑启用身份验证和强化网络基础设施。
有关配置bindIp的详细信息,请参阅自托管部署中的IP绑定。