MacOS使用brew安裝mongodb

1、使用brew安裝mongodb需要先更新brew
brew update

如果更新時抱如下錯誤:

yindengxu@xuyindengdeMacBook-Pro ~ % brew update
Error: 
  homebrew-core is a shallow clone.
To `brew update`, first run:
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!

根據提示執行如下命令:

git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow

運行結果如下圖所示:

yindengxu@xuyindengdeMacBook-Pro ~ % git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
remote: Enumerating objects: 566713, done.
remote: Counting objects: 100% (566670/566670), done.
remote: Compressing objects: 100% (200947/200947), done.
remote: Total 557950 (delta 359549), reused 552551 (delta 354154), pack-reused 0
接收對象中: 100% (557950/557950), 220.04 MiB | 3.83 MiB/s, 完成.
處理 delta 中: 100% (359549/359549), 完成 3868 個本地對象.
yindengxu@xuyindengdeMacBook-Pro ~ % brew update                                                                     
Updated 1 tap (homebrew/cask).
==> Updated Casks
purevpn
2、mongodb最新的安裝方法,之前舊的方法都無效了
brew tap mongodb/brew

運行結果如下:

yindengxu@xuyindengdeMacBook-Pro ~ % brew tap mongodb/brew                                                           
Updating Homebrew...
3、執行如下命令:
brew install mongodb-community

運行結果如下圖所示:

yindengxu@xuyindengdeMacBook-Pro ~ % brew install mongodb-community                 
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
==> Updated Casks
Updated 2 casks.

==> Installing mongodb-community from mongodb/brew
==> Downloading https://fastdl.mongodb.org/tools/db/mongodb-database-tools-macos-x86_64-100.2.1.zip
Already downloaded: /Users/yindengxu/Library/Caches/Homebrew/downloads/6e0a5e5b34d438cd760de60976f4aeedb1a9332b2d384323bd68070898d49c47--mongodb-database-tools-macos-x86_64-100.2.1.zip
==> Downloading https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-4.4.1.tgz
Already downloaded: /Users/yindengxu/Library/Caches/Homebrew/downloads/d46781539dca95a7ced7d9b466b13bd4cc0202f17a8bf4bd499a5e1a4a241262--mongodb-macos-x86_64-4.4.1.tgz
==> Installing dependencies for mongodb/brew/mongodb-community: mongodb-database-tools
==> Installing mongodb/brew/mongodb-community dependency: mongodb-database-tools
??  /usr/local/Cellar/mongodb-database-tools/100.2.1: 13 files, 173MB, built in 6 seconds
==> Installing mongodb/brew/mongodb-community
==> Caveats
To 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.4.1: 11 files, 136.8MB, built in 4 seconds
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/yindengxu/Library/Caches/Homebrew/aom--2.0.0.catalina.bottle.1.tar.gz... (5.5MB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/automake--1.16.2_1.catalina.bottle.tar.gz... (949.8KB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/dart--2.10.2.zip... (157.8MB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/git--2.28.0.catalina.bottle.tar.gz... (19.4MB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/gmp--6.2.0.catalina.bottle.tar.gz... (1022.7KB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/libass--0.14.0_1.catalina.bottle.1.tar.gz... (208.8KB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/libtiff--4.1.0.catalina.bottle.tar.gz... (1.1MB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/node--14.14.0.catalina.bottle.tar.gz... (16.8MB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/pcre2--10.35.catalina.bottle.tar.gz... (2.0MB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/sdl2--2.0.12_1.catalina.bottle.tar.gz... (1.3MB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/sqlite--3.33.0.catalina.bottle.tar.gz... (1.9MB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/utf8proc--2.5.0.catalina.bottle.tar.gz... (152.7KB)
Removing: /Users/yindengxu/Library/Caches/Homebrew/x264--r3011.catalina.bottle.1.tar.gz... (2.3MB)
Removing: /Users/yindengxu/Library/Logs/Homebrew/dart... (115B)
Removing: /Users/yindengxu/Library/Logs/Homebrew/node... (64B)
Pruned 0 symbolic links and 1 directories from /usr/local
==> Caveats
==> mongodb-community
To 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

如果出現下面所示錯誤:

yindengxu@xuyindengdeMacBook-Pro ~ % brew install mongodb-community                                                  
Updating Homebrew...
==> Installing mongodb-community from mongodb/brew
==> Downloading https://fastdl.mongodb.org/tools/db/mongodb-database-tools-macos-x86_64-100.2.1.zip
Already downloaded: /Users/yindengxu/Library/Caches/Homebrew/downloads/6e0a5e5b34d438cd760de60976f4aeedb1a9332b2d384323bd68070898d49c47--mongodb-database-tools-macos-x86_64-100.2.1.zip
==> Downloading https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-4.4.1.tgz
Already downloaded: /Users/yindengxu/Library/Caches/Homebrew/downloads/d46781539dca95a7ced7d9b466b13bd4cc0202f17a8bf4bd499a5e1a4a241262--mongodb-macos-x86_64-4.4.1.tgz
==> Installing dependencies for mongodb/brew/mongodb-community: mongodb-database-tools
==> Installing mongodb/brew/mongodb-community dependency: mongodb-database-tools
Error: Your CLT does not support macOS 11.
It is either outdated or was modified.
Please update your CLT or delete it if no updates are available.
Update them from Software Update in System Preferences or run:
  softwareupdate --all --install --force

If that doesn't show you an update run:
  sudo rm -rf /Library/Developer/CommandLineTools
  sudo xcode-select --install

Alternatively, manually download them from:
  https://developer.apple.com/download/more/.

Error: An exception occurred within a child process:
  SystemExit: exit

根據提示執行如下命令:

softwareupdate --all --install --force

運行結果如下:

yindengxu@xuyindengdeMacBook-Pro ~ % softwareupdate --all --install --force
Software Update Tool

Finding available software
No updates are available.

繼續執行如下命令:

sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install

需要輸入密碼,彈出安裝插件框,確定安裝,運行結果如下:

yindengxu@xuyindengdeMacBook-Pro ~ % sudo rm -rf /Library/Developer/CommandLineTools
  sudo xcode-select --install
Password:
xcode-select: note: install requested for command line developer tools

4、安裝之后,需要創建一個/data/db文件夾用來作為默認數據庫目錄。執行以下步驟:
sudo mkdir -p /data/db

因為Mac電腦默認是開啟安全模式的,不能在根目錄下面隨便創建刪除文件夾。所以我們創建的時候,會報這個錯誤mkdir: /data/db: Read-only file system,如下:

yindengxu@xuyindengdeMacBook-Pro ~ % sudo mkdir -p /data/db
mkdir: /data/db: Read-only file system

更改為在用戶更目錄下創建數據庫默認目錄

mkdir ~/data 

進入剛剛創建的目錄:

cd ~/data

查看目錄路徑,下面會用到:

pwd

運行結果:

/Users/yindengxu/data

指定MongoDB數據庫路徑,這里的路徑需要換成你自己電腦上面的路徑

sudo mongod --dbpath=/Users/yindengxu/data

5、執行如下命令:

sudo chown ‘username’ /data/db

6、mongodb啟動和停止

brew services start mongodb-community
brew services stop mongodb-community
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容