Mac 安裝與配置mongodb

樑鳳波發表於2018-04-19

安裝 mongodb

install 之前,iTerm2 下用 brew 檢視已安裝軟體、搜尋 mongodb:

brew list
brew search mongodb

安裝 mongodb :

brew install mongodb

此處需要稍等一段時間,成功後會輸出以下即說明安裝成功:

$ brew install mongodb
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.4.0.sierra.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-3.4.0.sierra.bottle.1.tar.gz
==> Caveats
To have launchd start mongodb now and restart at login:
  brew services start mongodb
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/3.4.0: 17 files, 261.4M

/usr/local/Cellar/mongodb這個就是MongoDB的路徑

進入環境配置

$ cd ~
$ vim .bash_profile

我們需要把它放進環境裡:

export MONGO_PATH=/usr/local/Cellar/mongodb
export PATH=$PATH:$MONGO_PATH/bin

儲存好,就啟動 mongodb

新建一個 iTerm2 視窗,執行 mongod 嘗試啟動 mongodb 但會失敗 exiting:

bob@BobdeMacBook-Air:~$mongod
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] MongoDB starting : pid=6961 port=27017 dbpath=/data/db 64-bit host=BobdeMacBook-Air.local
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] db version v3.6.3
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] allocator: system
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] modules: none
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] build environment:
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] options: {}
2018-04-11T22:55:22.413+0800 I STORAGE  [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating
2018-04-11T22:55:22.413+0800 I CONTROL  [initandlisten] now exiting
2018-04-11T22:55:22.413+0800 I CONTROL  [initandlisten] shutting down with code:100
bob@BobdeMacBook-Air:~$

啟動 mongodb 之前,要先新建一個mongodb預設的資料寫入目錄:

$ mkdir -p /data/db
mkdir: /data/db: Permission denied (沒有許可權拒絕訪問)

// sudo 並輸入密碼,重新新建目錄
$ sudo mkdir -p /data/db
Password:

給剛才新建的資料庫目錄賦予許可權:

sudo chown -R guojc /data

此時,執行 mongod 啟動 mongodb 服務

2018-04-11T23:08:19.325+0800 I CONTROL  [initandlisten] MongoDB starting : pid=7681 port=27017 dbpath=/data/db 64-bit host=BobdeMacBook-Air.local
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] db version v3.6.3
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] allocator: system
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] modules: none
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] build environment:
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] options: {}
2018-04-11T23:08:19.328+0800 I STORAGE  [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] now exiting
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] shutting down with code:100

現在是成功了,等待連線,可以Command + T 新建視窗開啟

一般是可以的,如果還是exception: connect failed

當mongodb service 沒有執行的時候會發生這樣的錯誤

嘗試了新建一個/data/db 目錄,但不奏效

最後找到了如下方法解決

然後 在bash 輸入

brew services start mongodb

再輸入mongo進入資料庫

mongo

成功連線了:

bob@BobdeMacBook-Air:~$mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Server has startup warnings:
2018-04-11T22:59:01.723+0800 I CONTROL  [initandlisten]
2018-04-11T22:59:01.723+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T22:59:01.723+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T22:59:01.723+0800 I CONTROL  [initandlisten]
>
>

還可以測試:

> show dbs
admin  0.000GB
local  0.000GB
help

相關文章