Mac OS X 上安装 MongoDB

阿里云产品限时红包,最高 ¥1888 元,立即领取

参考:Install MongoDB on OS X

Mac OS X 的系统版本:EI Capitan (10.11.1)

这里采用 HomeBrew 来安装 MongoDB。

1. 升级 HomeBrew 到最新

brew update

2. 完成以后,通过以下命令安装

brew install mongodb

正常情况下按照上面步骤就可以完成安装,但是我这里遇到了一些问题。

1
2
3
4
5
6
7
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink .
/usr/local/opt is not writable.

You can try again using:
brew link scons

问题的原因就是 /usr/local/opt 目录不可写,正常情况下,在目前的 Mac OS X EI Capitan 上 /usr/local 是可写的,但其他的应用是可以修改其读写权限的。

1
2
3
4
5
6
7
8
Error: The /usr/local directory is not writable.
Even if this directory was writable when you installed Homebrew, other
software may change permissions on this directory. Some versions of the
"InstantOn" component of Airfoil are known to do this.

You should probably change the ownership and permissions of /usr/local
back to your user account.
sudo chown -R $(whoami):admin /usr/local

按照提示修改一下问题,重新 brew install mongodb 即可。如提示需要 brew link, 则按照提示操作。

3. 安装完成最终会出现如下提示来以不同方式开启 MongoDB。根据你的需要操作即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
lfys-MacBook-Pro: lfy$ brew install mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.2.0.el_capitan.bottle.tar.gz
Already downloaded: /Library/Caches/Homebrew/mongodb-3.2.0.el_capitan.bottle.tar.gz
==> Pouring mongodb-3.2.0.el_capitan.bottle.tar.gz
==> Caveats
To have launchd start mongodb at login:
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
Then to load mongodb now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Or, if you don't want/need launchctl, you can just run:
mongod --config /usr/local/etc/mongod.conf
==> Summary
🍺 /usr/local/Cellar/mongodb/3.2.0: 17 files, 208M

这里我执行了下面的命令:

1
mongod --config /usr/local/etc/mongod.conf

4. 在新的终端窗口中输入 mongo 就可以进行 MongoDB 的操作了。