文章目录
本文参考官网通过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.