1. 程式人生 > 實用技巧 >MongoDB教程7-MongoDB的安裝與測試

MongoDB教程7-MongoDB的安裝與測試

MongoDB提供了可用於32位和64位系統的預編譯二進位制包,你可以從MongoDB官網下載安裝,MongoDB預編譯二進位制包下載地址:

https://www.mongodb.com/try/download/community

本節以 Windows 為例,具體安裝步驟可參考官網手冊。

1) 本節採用的 MongoDB 版本為 4.2.8,安裝環境為 Windows 64 位系統。安裝步驟如下。

下載zip包後,解壓安裝包,並安裝它。

安裝過程中,你可以通過點選 "Custom(自定義)" 按鈕來設定你的安裝目錄。

下一步安裝"install mongoDB compass"不勾選(當然你也可以選擇安裝它,可能需要更久的安裝時間),MongoDB Compass 是一個圖形介面管理工具,我們可以在後面自己到官網下載安裝,下載地址:

https://www.mongodb.com/download-center/compass

注意:實際中將 MongoDB 安裝在了目錄"D:\Program Files (x86)\MongoDB\Server\4.2"下

安裝完畢後先不要啟動,MongoDB將資料目錄儲存在 db 目錄下。但是這個資料目錄不會主動建立,我們在安裝完成後需要建立它, 在 MongoDB 安裝目錄(D:\Program Files (x86)\MongoDB\Server\4.2)下建立 db 目錄用於儲存資料,建立 log 目錄用於儲存日誌檔案,建立完畢後的介面如圖所示。

2) 與傳統資料庫一樣,MongoDB 需要先開啟服務端,再開啟客戶端,啟動步驟如下。

① 配置MongoDB伺服器。

在命令視窗切換到 D:\Program Files (x86)\MongoDB\Server\4.2\bin 目錄,執行 mongod.exe 命令,同時指定資料庫db和 log 日誌檔案的路徑:

mongod.exe -dbpath "D:\Program Files (x86)\MongoDB\Server\4.2\db" -logpath "D:\Program Files (x86)\MongoDB\Server\4.2\log\mon.log"

檢視 db 和 log 目錄,會發現 MongoDB 自動建立了執行所需的檔案,這種方式啟動 MongoDB 為前臺啟動,命令列視窗不能關閉。

②啟動 MongoDB 客戶端。

在"D:\Program Files (x86)\MongoDB\Server\4.2\bin"下另開一個命令視窗,執行 mongo 命令進入 MongoDB 的 Shell 互動介面,如圖所示。

$ mongo.exe
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("059743c7-f2a2-43ab-bcc1-27dfe6eef79a") }
MongoDB server version: 4.2.8
Server has startup warnings:
2020-09-02T14:04:35.400+0800 I  CONTROL  [initandlisten]
2020-09-02T14:04:35.400+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-09-02T14:04:35.400+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-09-02T14:04:35.401+0800 I  CONTROL  [initandlisten]
2020-09-02T14:04:35.401+0800 I  CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2020-09-02T14:04:35.401+0800 I  CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2020-09-02T14:04:35.401+0800 I  CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2020-09-02T14:04:35.401+0800 I  CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2020-09-02T14:04:35.401+0800 I  CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2020-09-02T14:04:35.401+0800 I  CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2020-09-02T14:04:35.417+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()
---

>
View Code

3) 使用者可將 MongoDB 服務設定為開機自啟,方法如下。

① 配置 MongoDB 服務開機自啟。

使用管理員身份進入命令視窗,切換到D:\Program Files (x86)\MongoDB\Server\4.2\bin 目錄,執行以下命令將 MongoDB 服務新增到系統服務中:

mongod.exe -dbpath "D:\Program Files (x86)\MongoDB\Server\4.2\db" -logpath "D:\Program Files (x86)\MongoDB\Server\4.2\log\mon.log" --install --serviceName "MongoDB"

② 開啟 MongoDB 服務。

使用 net start 命令即可完成服務開機自啟動設定,需要注意的是,一定要使用管理員身份開啟 CMD 視窗。

net start MongoDB

③ 移除MongoDB服務開機自啟。

使用管理員身份進入命令視窗,切換到 bin 目錄,執行以下命令:

先停止服務

net stop MongoDB

移除服務

mongod.exe -dbpath "D:\Program Files (x86)\MongoDB\Server\4.2\db" -logpath "D:\Program Files (x86)\MongoDB\Server\4.2\log\mon.log" --remove --serviceName "MongoDB"