Try   HackMD

用 Docker 玩 Mongo

tags: Docker MongoDb

前置步驟

有安裝docker環境即可

實作步驟

一、建立Mongodb

1. 拉取MongoDb镜像

預設版本為最新版,以下指令可以取得最新版的MongoDb鏡像

docker pull mongo

運行結果

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2. 運行MongoDB 的映像檔

在運行之前我們可以事先建立容器要對應的實體路徑

  1. 範例是使用Windows系統故路徑為: C:\temp\MonoDbData。
  2. 27017為MongoDb預設port。
  3. 啟用MongoDb預設可以不使用帳號就可以使用,如果需要帳號流程可參考擴充內容。
docker run --name mongo4 -v C:\temp\MonoDbData:/data/db -d -p 27017:27017 --rm mongo

參數解釋:

  • name:容器名稱
  • -v:實體位置對應容器內部位置
  • -d:背景執行
  • -p:實體port對應容器內部port
  • -rm:結束後立即移除

運行結果如下

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

此運行方式其MongoDb無帳號密碼管控連線時不需帶入帳密,下圖為Robo 3T測試結果

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

其對應的實體路徑下也會有檔案產生,之後操作DB的資料也就不怕docker掛掉而遺失了

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

3. 停止容器

關掉 mongodb 資料庫輸入以下指令即可,其"mongo4"可以替換成任何容器名稱

docker stop mongo4

二、基本檢視和操作

  1. 進入容器內
docker exec -it mongo4 bash

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 進入mongo指令操作
mongo

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

MongoDb相關CRUD操作就靠各為自己學習了,可以至底部參考資料參考,或是使用工具進行操作都是可行的,或是可以參考 MongoDb 快速指令查詢 裡面有基本操作

這邊稍微列一下可以操作的方式

# 連入DB(預設port 27017) mongo # 顯示DB show dbs # 切換dbname(注意大小寫) use dbname # 顯示集合 show collections # 資料查詢 db.system.version.find() # 指令,離開資料庫或容器 exit

確認資料庫狀況

docker exec mongo4 mongo --eval "print(version())"

擴充內容

啟用權限認證

在腳本後方加上mongod --auth 命令使MongoDb啟動時可以限定一定要使用帳密登入

docker run --name mongo4 -p 27017:27017 --rm mongo mongod --auth

docker-compose腳本

該腳本有針對權限限制和一些容量做設定,可能依據不同需求在下去修改。

version: '3.7' services: mongo: container_name: mongo hostname: mongo image: mongo:latest volumes: - /data/mongo/db:/data/db - /data/mongo/conf:/data/configdb environment: - MONGO_INITDB_ROOT_USERNAME=admin - MONGO_INITDB_ROOT_PASSWORD=123456 logging: driver: 'json-file' options: max-file: '10' max-size: '1024m' ports: - "27017:27017" networks: - mongo-net restart: always command: ["mongod", "--oplogSize=1024", "--wiredTigerCacheSizeGB=1", "--auth", "--noscripting"] networks: mongo-net: driver: bridge name: mongo-net

結論

這邊有時做一個[Node的範例]裡面是用Docker-compose的方式運行,不熟的可以前往快速建立MongoDB複習一下。



相關參考來源:

Day 15 - 二周目 - 用 Docker 玩轉 MongoDB