MacOS使用brew安装mongodb

2020-12-24  本文已影响0人  许银灯
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
上一篇下一篇

猜你喜欢

热点阅读