tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

【mongodb】Mac OSにmongodbをinstallするメモ

はじめに

久しぶりにMongoDBを触ろうと思いおもむろに
brew install mongodb
としたらエラー。

どうやらHomebrew Coreから消えたようです。
ということでinstallメモ

アジェンダ

  1. どんなエラーか
  2. brew tapからinstallする
  3. brew tapからinstall時のエラー

1. どんなエラーか

brew install だと以下のようなエラーが出ます。

$ brew install mongodb
Updating Homebrew...
Error: No available formula with the name "mongodb"
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get complete history run:
  git -C "$(brew --repo homebrew/core)" fetch --unshallow

Error: No previously deleted formula found.
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.

2. brew tapからinstallする

改めて公式を見返すと、installや使い方などはちゃんと記載がありました。

brew tapにて公式以外のリポジトリをフォーミュラとしてHomebrewに追加してからinstallを行います。

# tapをする
$ brew tap mongodb/brew
Updating Homebrew...
==> Tapping mongodb/brew
Cloning into '/usr/local/Homebrew/Library/Taps/mongodb/homebrew-brew'...

# installを行う
$ brew install mongodb-community
Updating Homebrew...

-- 省略  

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.0: 11 files, 136.7MB, built in 2 seconds
==> 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

3. brew tapからinstall時のエラー

brew install mongodb-communityでエラーが出る場合

brew install mongodb-communityで以下のようにエラーが出る場合、すなおにbrew install gccを行いましょう。

# mongodb-communityでインストール
$ 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.1.1.zip
######################################################################## 100.0%
==> Downloading https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-4.4.0.tgz
######################################################################## 100.0%
==> Installing dependencies for mongodb/brew/mongodb-community: mongodb-database-tools
==> Installing mongodb/brew/mongodb-community dependency: mongodb-database-tools
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Error: An exception occurred within a child process:
  CompilerSelectionError: mongodb/brew/mongodb-database-tools cannot be built with any available compilers.
Install GNU's GCC:
  brew install gcc`

brew install gccでエラーが出る場合

brew install gccで以下のようにエラーが出る場合、Command Line Toolsをインストール or インストールしなおす必要があるようです。

ぼくはinstallしていたのですがエラーが出たのでinstallしなおしました。
installはxcode-select --installで行います。

# gccをインストール
$ brew install gcc
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/gmp-6.2.0.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/2e6acd6e62d1b8ef0800061e113aea30a63f56b32b99c010234c0420fd6d3ecf?response-content-disposition=attachment%3Bfilename%3D%22gmp-6.2.0.catalin
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/isl-0.22.1.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/b5319e3bbbb36ef3536d841999b7497b3dce4bf9e07fb04f6b0db716e087896d?response-content-disposition=attachment%3Bfilename%3D%22isl-0.22.1.catali
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/mpfr-4.1.0.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/5fcf57834f58c18761c6c7b0eb961eb7f9fc54325b5361bf3a17c4dee6ebc08a?response-content-disposition=attachment%3Bfilename%3D%22mpfr-4.1.0.catali
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/libmpc-1.2.0.catalina.bottle.tar.gz
######################################################################## 100.0%
Warning: Building gcc from source:
  The bottle needs the Xcode CLT to be installed.
==> Downloading https://ftp.gnu.org/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.xz
######################################################################## 100.0%
Error: The following formula
  gcc
cannot be installed as binary package and must be built from source.
Install the Command Line Tools:
  xcode-select --install

インストールしなおした一連

Command Line Toolsはわりと時間がかかります。
15 ~ 30minほどみておくと良いでしょう。

# Command Line Toolsをインストール
$ xcode-select --install
xcode-select: note: install requested for command line developer tools

# gccをインストール
$ brew install gcc

# mmongodbをtapする
$ brew tap mongodb/brew

# mongodb-communityをインストール
$ brew install mongodb-community

4. mongodの起動

mongoの起動は、configファイルを指定してbackground processで起動します。
configファイルを見ればわかりますが、dbファイルは/usr/local/var/mongodbにあります。

# バックグラウンドで起動する
$ mongod --config /usr/local/etc/mongod.conf --fork

# 起動を確認
$ ps aux | grep mongo
tweeeety  5730   0.2  0.1  5531164  22884   ??  S     3:54AM   0:00.68 mongod --config /usr/local/etc/mongod.conf --fork
tweeeety  5744   0.0  0.0  4278540    724 s009  S+    3:54AM   0:00.00 grep mongo

# confを確認
$ cat /usr/local/etc/mongod.conf
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1

終わり

数年前はどうしてたっけ?と自分のブログを見返してみました。
ソースからinstallしてたので、まあ時代は変わるよなーと思いました。