1. 程式人生 > >MongoDB 下載_安裝_配置 及 啟動與連線

MongoDB 下載_安裝_配置 及 啟動與連線

本文導讀

  • 本文承接《 MongoDB 理論簡介》,熟悉 MongoDB 理論之後,本文介紹 MongoDB  在 Windows 系統中的 下載與安裝配置。

MongoDB 下載

  • Commutity Server:社群版本,免費的版本,學習或者一般的專案用這種就足夠了。
  • Enterprise Server :商業版本,收費的版本。
  • 以社群版本為例,選擇自己對應的作業系統 “ Windows”、"Linux" 等,然後選擇系統版本
  • 可以點選 " DOWNLOAD(msi)"  按鈕直接下載 安裝版的 .msi 檔案,msi 檔案是一種安裝檔案,點選按鈕之後就會進行下載,注意如果網速慢,則可能要等一段時間,如果還是不行建議翻牆!
  • 也可以點選 "ALL Version Binaries" 連結,此時不僅可以下載所有的歷史版本,而且還可以下載 解壓版的軟體,即下載後解壓即可使用,無需安裝,推薦方式,

  • 如上所示,可以下載任意的歷史版本,*.zip 是解壓版本,*.msi 是安裝版本。
  • 推薦使用 解壓版 即可,因為安裝版本安裝完成後目錄結構與解壓版是一樣的(我猜的)。

目錄介紹

  • 以上面 Windows 系統的 MongoDB 4.0.2 版本為例進行說明:
mongo.exe 客戶端程式,連線 MongoDB
mongod.exe 服務端程式,啟動 MongoDB
mongodump.exe 備份程式
mongoexport.exe 資料匯出程式
mongofiles.exe GridFS工具,內建的分散式檔案系統
mongoimport.exe 資料匯入程式
mongorestore.exe 資料恢復程式
mongos.exe 資料分片程式,支援資料的橫向擴充套件
mongostat.exe 監視程式

環境變數 配置

  • 為了能在 cmd 命令列中方便操作,需要將 MongoDB 的 bin 目錄配置系統環境變數中的 path 變數中,如同之前配置 Java JDK 一樣
  • 將上面準備好的 D:\MongoDB\mongodb-win32-x86_64-2008plus-ssl-4.0.2-rc0\bin 追加到 Path 環境變數中

MongoDB 操作

啟動 MongoDB 資料庫

  • 啟動 MongoDB 資料庫之前,要手動建立一個存放 MongoDB 資料檔案的目錄,如 D:\MongoDB\Data

  • 在命令列執行 mongod --dbpath=D:\MongoDB\Data 啟動 MongoDB 資料庫,bin 目錄下的 mongod.exe 程式就是用於啟動 MongoDB 的。
  • --dbpath 後面接 MongoDB 資料庫檔案儲存目錄,注意這個物理目錄就是一個 MongoDB 資料庫
  • 如下所示 MongoDB 資料庫 啟動 成功!
Microsoft Windows [版本 10.0.17134.112]
(c) 2018 Microsoft Corporation。保留所有權利。

C:\Users\Administrator.SC-201707281232>mongod --dbpath=D:\MongoDB\Data
2018-09-10T11:29:57.470+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-09-10T11:29:57.913+0800 I CONTROL  [initandlisten] MongoDB starting : pid=13500 port=27017 dbpath=D:\MongoDB\Data 64-bit host=SC-201707281232
2018-09-10T11:29:57.913+0800 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] db version v4.0.2-rc0
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] modules: none
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten] build environment:
2018-09-10T11:29:57.914+0800 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2018-09-10T11:29:57.915+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-09-10T11:29:57.915+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-09-10T11:29:57.915+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "D:\MongoDB\Data" } }
2018-09-10T11:29:57.916+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3552M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-09-10T11:29:58.505+0800 I STORAGE  [initandlisten] WiredTiger message [1536550198:505724][13500:140730388790352], txn-recover: Set global recovery timestamp: 0
2018-09-10T11:29:58.681+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-09-10T11:29:59.159+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.164+0800 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: ab94bc6f-4cf3-4bb7-9a64-51f27a13405a
2018-09-10T11:29:59.549+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
2018-09-10T11:29:59.557+0800 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: ffb7e260-d107-425d-bbc9-a7cbab38b91f
2018-09-10T11:30:00.428+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'D:/MongoDB/Data/diagnostic.data'
2018-09-10T11:30:00.434+0800 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: e32fd3e6-6b27-496a-b519-41add4bd68c7
2018-09-10T11:30:00.434+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-09-10T11:30:01.018+0800 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2018-09-10T11:30:01.018+0800 I INDEX    [LogicalSessionCacheRefresh]     building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018-09-10T11:30:01.075+0800 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs
2018-09-10T11:30:01.075+0800 I COMMAND  [LogicalSessionCacheRefresh] command config.$cmd command: createIndexes { createIndexes: "system.sessions", indexes: [ { key: { lastUse: 1 }, name: "lsidTTLIndex", expireAfterSeconds: 1800 } ], $db: "config" } numYields:0 reslen:114 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { W: 1 } }, Collection: { acquireCount: { w: 1 } } } protocol:op_msg 641ms

  • 如上所示,cmd 命令列中啟動了 MongoDB 資料庫之後,如果關閉了此 cmd 面板,則意味著 MongoDB 資料庫也關閉了
  • 如同 Mysql 啟動之後會預設使用 3306 埠一樣,MongoDB 的預設埠為 27017
  • 如下所示 使用 windows 自帶的 netstat 與 tasklist 命令 輕鬆就可查埠和程序來。
C:\Users\Administrator.SC-201707281232>netstat -ano |findstr "27017"
  TCP    127.0.0.1:27017        0.0.0.0:0              LISTENING       13500
  TCP    127.0.0.1:27017        127.0.0.1:1809         ESTABLISHED     13500
C:\Users\Administrator.SC-201707281232>tasklist |findstr 13500
mongod.exe                   13500 Console                    1     39,540 K
C:\Users\Administrator.SC-201707281232>
  • 自定義埠啟動:mongod --dbpath=D:\MongoDB\Mater --port 27018,(使用 --port 進行指定)
  • 使用安全認證啟動:mongod --dbpath=D:\MongoDB\Mater --auth,(使用 --auth  引數,可以參考《MongoDB 安全與認證》)
  • 以主節點啟動:mongod --dbpath D:\MongoDB\Mater --port 10000 --master,(主從複製可以參考《 MongoDB 主從複製(主從叢集 )》)
  • 以從節點啟動:mongod --dbpath D:\MongoDB\Slave --port 10001 --slave --source localhost:10000

連線 MongoDB 資料庫

  • 新開一個 cmd 視窗,在命令列中可以使用命令 “ mongo ” 連線到 MongoDB 伺服器,bin 目錄下的 mongo.exe 就是連線 MongoDB 的客戶端程式。
  • mongo :預設連線本地(localhost) 的 27017 埠的 MongoDB 資料庫
  • mongo localhost:27017:指定連線本地 27017 埠的 MongoDB 資料庫,預設連線的是 test 資料庫
  • mongo ip:port:連線到遠端資料庫時,指定 ip 與 埠即可
C:\Users\Administrator.SC-201707281232>mongo
MongoDB shell version v4.0.2-rc0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2-rc0
Server has startup warnings:
2018-09-10T11:29:59.159+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-09-10T11:29:59.162+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()
---
>
C:\Users\Administrator.SC-201707281232>mongo localhost:27017
MongoDB shell version v4.0.2-rc0
connecting to: mongodb://localhost:27017/test
MongoDB server version: 4.0.2-rc0
Server has startup warnings:
2018-09-10T11:29:59.159+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten]
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-09-10T11:29:59.160+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-09-10T11:29:59.161+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-09-10T11:29:59.162+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-09-10T11:29:59.162+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()
---
>
  • 輸入 "exit" 可以退出連線。

關閉 MongoDB 資料庫

  • 關閉資料庫:先使用 use admin 切換資料庫,然後輸出 db.shutdownServer() 關閉資料庫例項
  • 當然也可以簡單粗暴的直接關閉 資料庫的 cmd 視窗
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2018-09-11T16:41:37.684+0800 I NETWORK  [js] trying reconnect to localhost:27017 failed
2018-09-11T16:41:38.751+0800 I NETWORK  [js] reconnect localhost:27017 failed failed