【記錄】mac下安裝MongoDB

環境

操作系統:macOS Mojave Version 10.14.2

安裝

➜  ~ sudo brew install mongodb
Password:
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.

這個報錯其實是說無需使用sudo

➜  ~ brew install mongodb
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
bettercap    bit          fonttools    hcloud       inetutils    opendetex
==> Renamed Formulae
resin-cli -> balena-cli

==> Installing dependencies for mongodb: python@2
==> Installing mongodb dependency: python@2
==> Downloading https://homebrew.bintray.com/bottles/[email protected]_2.mojave.bo
######################################################################## 100.0%
==> Pouring [email protected]_2.mojave.bottle.tar.gz
==> /usr/local/Cellar/python@2/2.7.15_2/bin/python -s setup.py --no-user-cfg ins
==> /usr/local/Cellar/python@2/2.7.15_2/bin/python -s setup.py --no-user-cfg ins
==> /usr/local/Cellar/python@2/2.7.15_2/bin/python -s setup.py --no-user-cfg ins
==> Caveats
Pip and setuptools have been installed. To update them
  pip install --upgrade pip setuptools

You can install Python packages with
  pip install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
🍺  /usr/local/Cellar/python@2/2.7.15_2: 4,701 files, 82.7MB
==> Installing mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-4.0.3_1.mojave.bott
######################################################################## 100.0%
==> Pouring mongodb-4.0.3_1.mojave.bottle.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/4.0.3_1: 18 files, 258.1MB
==> Caveats
==> python@2
Pip and setuptools have been installed. To update them
  pip install --upgrade pip setuptools

You can install Python packages with
  pip install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> mongodb
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

這樣就說明安裝成功了,可以看出它被安裝在/usr/local/Cellar/mongodb/4.0.3_1路徑下,實際上通過brew命令下載的文件都存在/usr/local/Cellar/文件夾下

配置

配置mongo/bin 的環境變量,有利於每次啓動不用重複寫路徑,具體步驟參照參考資料

1.首先使用brew安裝的mongodb文件夾默認存放在/usr/local/Cellar下,同時也會在/usr/loacl/etc下生成一個mongod.conf配置文件
2.如果是新mac,你的~(根目錄下)還沒有.bash_profile文件,那麼就要新建一個
3.在~下執行touch .bash_profile
4.編輯.bash_porfile文件,執行sudo vim .bash_profile,內容如下:
export MONGO_PATH=/usr/local/Cellar/mongodb/4.0.3_1
export PATH=$PATH:$MONGO_PATH/bin
5.保存退出 :wq
6.執行 source .bash_profile
7.查看是否成功 執行mongod

然後在mongodb/4.0.3_1文件夾下創建data,logs文件夾,同時也創建一份mongo.conf,其內容如下:

#數據庫路徑
dbpath=/usr/local/Cellar/mongodb/4.0.3_1/data
#日誌輸出路徑
logpath=/usr/local/Cellar/mongodb/4.0.3_1/logs/mongo.log
#錯誤日誌採用追加模式
logappend=true
#啓用日誌文件默認啓用
journal=true
#過濾無效日誌
quiet=true
#默認端口號
port=27017

但根據參考資料的步驟執行mongod會報exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating這個錯,最後我用了下面這個命令就OK了

mongod --config /usr/local/Cellar/mongodb/4.0.3_1/mongo.conf

同時再開一個新的終端執行mongo命令就成功啦

➜  ~ mongo
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("7f18c673-0639-4f6a-bad9-9526bf036e2d") }
MongoDB server version: 4.0.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten]
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten]
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

簡單執行了一些命令

> use blog
switched to db blog
> db.createCollection("users")
2019-02-07T11:24:52.551+0800 I NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
2019-02-07T11:24:52.553+0800 I NETWORK  [js] reconnect 127.0.0.1:27017 ok
{ "ok" : 1 }
> db.users.insert({user_name:"admin",user_password:"123456"})
WriteResult({ "nInserted" : 1 })
>

總結

  1. touch命令用於創建文件夾,可參考博文
  2. source命令用於通知當前shell讀入路徑爲filename的文件並依次執行文件中的所有語句,可參考博文
  3. 目前我的執行方法是mongod --config /usr/local/Cellar/mongodb/4.0.3_1/mongo.conf有點長,不知道有什麼方法可以簡化?

參考資料

1.https://blog.csdn.net/sinat_36193631/article/details/81261221

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章