【mongodb 基础2】Install MongoDB Community Edition on macOS

本文详细介绍了如何在macOS上通过Homebrew安装MongoDB6.0社区版,包括安装Xcode命令行工具、Homebrew,以及MongoDB的下载、更新和启动。此外,还提到了可能遇到的问题及解决方案,如设置安全目录,以及MongoDB的运行、停止、验证和连接方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文参考官网通过Homebrew安装MongoDB
 
Use this tutorial to install MongoDB 6.0 Community Edition on macOS using the third-party Homebrew package manager.

 

一. 安装准备

Install Xcode Command-Line Tools

Install the Xcode command-line tools by running the following command in your macOS Terminal:

xcode-select --install

 

Install Homebrew

macOS does not include the Homebrew brew package by default.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

brew官网:https://brew.sh/#install

 

二. Installing MongoDB 6.0 Community Edition

1. 下载MongoDB Homebrew 组件包

Tap the MongoDB Homebrew Tap to download the official Homebrew formula for MongoDB and the Database Tools,

brew tap mongodb/brew

 

2. 更新组件包

To update Homebrew and all existing formulae:

brew update

 

3. 安装MongoDBTo install MongoDB

brew install mongodb-community@6.0

 

可能出现的问题

Error: Command failed with exit 128: git

执行:brew -v

lianggao@LiangdeMacBook-Pro ~ % brew -v
Homebrew 4.0.6
fatal: detected dubious ownership in repository at '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'
To add an exception for this directory, call:

        git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
Homebrew/homebrew-core (no Git repository)
fatal: detected dubious ownership in repository at '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'
To add an exception for this directory, call:

        git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask
Homebrew/homebrew-cask (no Git repository)

根据提示添加:

lianggao@LiangdeMacBook-Pro ~ % git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core  
lianggao@LiangdeMacBook-Pro ~ % git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask

 

三. 安装后包含的组件

The installation includes the following binaries:

  • The mongod server
  • The mongos sharded cluster query router
  • The MongoDB Shell, mongosh

 
创建的目录和文件

安装位置:
check where brew has installed these files and directories:

 % brew --prefix                     
/usr/local

In addition, the installation creates the following files and directories at the location specified below, depending on your Apple hardware:

在这里插入图片描述
 
 

四. Run&stop MongoDB

两种方式运行mongodb

You can run MongoDB as a macOS service using brew, or you can run MongoDB manually as a background process.

1. 作为macOS服务的方式运行mongodb

It is recommended to run MongoDB as a macOS service, as doing so sets the correct system ulimit values automatically (see ulimit settings for more information).
 
To run MongoDB (i.e. the mongod process) as a macOS service, run:

brew services start mongodb-community@6.0

。。。
==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-commu

 

2. stop mongod

To stop a mongod running as a macOS service, use the following command as needed:

brew services stop mongodb-community@6.0

 

3. verify that MongoDB is running

To verify that MongoDB is running, perform one of the following: If
you started MongoDB as a macOS service:

brew services list

4. the log file

You can also view the log file to see the current status of your mongod process:

/usr/local/var/log/mongodb/mongo.log.

 
 

五. Connect and Use MongoDB

1. Connect MongoDB

To begin using MongoDB, connect mongosh to the running instance.

lianggao@LiangdeMacBook-Pro ~ % mongosh

lianggao@LiangdeMacBook-Pro ~ % mongosh
Current Mongosh Log ID:        641168ac9149fea4d2578ae1
Connecting to:                mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.0
Using MongoDB:                6.0.5
Using Mongosh:                1.8.0

。。。

test> 

 

2. other tools

including data backup and import/export tools like mongoimport and mongodump as well as monitoring tools like mongotop.

% mongotop

It should start up, connect to your running mongod, and start reporting usage statistics.

3. default setting

mongoDB默认绑定的ip是127.0.0.1,也就是说所有的请求只能来自本地一个节点。

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.

通过设置ip来允许与外界的通讯

This value can be configured either:

  • in the MongoDB configuration file with bindIp,
  • via the command-line argument --bind_ip
     

For more information on configuring bindIp, see IP Binding.

### 安装 MongoDB 对于希望在 MacOS 上安装 MongoDB 的用户来说,可以通过 Homebrew 来简化这一过程。首先确保已经安装了 Homebrew,之后可以在终端执行如下命令来安装 MongoDB Community Edition: ```bash brew tap mongodb/brew brew install mongodb-community@6.0 ``` 完成上述步骤后,为了启动并使 MongoDB 随系统自动启动,可以使用以下命令[^1]: ```bash brew services start mongodb-community@6.0 ``` 若不打算让其随系统启动而是仅临时运行,则可依据处理器架构不同采取相应措施。针对 Intel 架构的 Mac 设备应采用此指令: ```bash mongod --config /usr/local/etc/mongod.conf --fork ``` 而对于搭载 Apple Silicon (M1/M2) 芯片的新款 Mac 则需调整路径为: ```bash mongod --config /opt/homebrew/etc/mongod.conf --fork ``` 这将允许 `mongod` 进程作为守护程序持续工作。 ### 使用可视化管理工具 除了官方提供的 MongoDB Compass 之外还有其他选项可用于连接和管理 MongoDB 数据库实例。例如 Navicat Premium 提供了一个强大的平台支持多种类型的数据库之间的数据迁移功能[^4];然而需要注意的是并非所有的特性都对 MongoDB 用户开放。 对于那些寻求完全免费解决方案的人来说,MongoDB 自家出品的 **Compass** 是一个不错的选择,它不仅易于上手而且具备足够的性能满足日常开发需求。可以从官方网站获取最新版本。 一旦选择了合适的客户端应用,在初次打开时通常会被引导通过指定主机名/IP地址及端口号建立新的连接。默认情况下本地部署的服务监听于 `localhost:27017` 。此时应当能够顺利浏览现有数据库列表——新环境中可能只会看到少量预置条目如 admin, config 和 local[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

roman_日积跬步-终至千里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值