在 Linux 上安裝 MongoDB 社群版 4.0


簡介

MongoDB 是一款流行的開源 NoSQL 資料庫管理系統,以其可擴充套件性、靈活性和易用性而聞名。如果您使用的是 Linux 作業系統並希望安裝 MongoDB 社群版 4.0,本文將為您提供詳細的指南,並附帶示例和相應的命令輸出。

先決條件

在繼續安裝之前,請確保您具備以下先決條件:

  • 基於 Linux 的作業系統(例如,Ubuntu、CentOS 或 Debian)。

  • Root 或 sudo 許可權。

  • 活動的網際網路連線。

步驟 1:匯入 MongoDB GPG 金鑰

要開始安裝過程,我們首先需要匯入 MongoDB GPG 金鑰。開啟終端並執行以下命令:

示例

wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -

輸出

OK

步驟 2:建立 MongoDB 儲存庫檔案

接下來,我們需要建立一個 MongoDB 儲存庫檔案,以確保包管理器可以獲取 MongoDB 包。執行以下命令建立儲存庫檔案:

適用於 Ubuntu 18.04

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

適用於 Ubuntu 20.04

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

輸出

No output will be displayed if the command is executed successfully.

步驟 3:更新包管理器

建立儲存庫檔案後,使用以下命令更新包管理器:

示例

sudo apt-get update

輸出

Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1,275 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:7 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2,150 B]
Get:8 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [3,522 B]
Get:9 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [8,988 B]
Get:10 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1,044 B]
Get:11 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [804 B]
Fetched 13.3 MB in 4s (3,549 kB/s)
Reading package lists... Done

步驟 4:安裝 MongoDB 社群版 4.0

現在,是時候安裝 MongoDB 社群版 4.0 了。執行以下命令啟動安裝:

示例

sudo apt-get install -y mongodb-org

輸出

終端將顯示正在下載和安裝的軟體包。安裝完成後,您將看到以下輸出:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  mongodb-org-database mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-org mongodb-org-database mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 100 MB/100 MB of archives.
After this operation, 283 MB of additional disk space will be used.
Get:1 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.0/multiverse amd64 mongodb-org-shell amd64 4.0.0 [6,195 kB]
Get:2 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.0/multiverse amd64 mongodb-org-server amd64 4.0.0 [18.9 MB]
Get:3 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.0/multiverse amd64 mongodb-org-mongos amd64 4.0.0 [9,944 kB]
Get:4 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.0/multiverse amd64 mongodb-org-tools amd64 4.0.0 [113 MB]
Get:5 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.0/multiverse amd64 mongodb-org-database amd64 4.0.0 [22.2 MB]
Get:6 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.0/multiverse amd64 mongodb-org amd64 4.0.0 [6,448 B]
Fetched 100 MB in 10s (10.2 MB/s)      
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 144813 files and directories currently installed.)
Preparing to unpack .../0-mongodb-org-shell_4.0.0_amd64.deb ...
Unpacking mongodb-org-shell (4.0.0) ...
...
...
Setting up mongodb-org-database (4.0.0) ...
Setting up mongodb-org (4.0.0) ...
Processing triggers for ureadahead (0.100.0-21) ...

步驟 5:啟動並驗證 MongoDB

安裝完成後,啟動 MongoDB 服務並將其設定為在系統啟動時自動啟動,使用以下命令:

示例

sudo systemctl start mongod
sudo systemctl enable mongod

輸出

$ sudo systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.

步驟 6:驗證 MongoDB 安裝

為了確保 MongoDB 正確安裝並正在執行,請執行以下命令檢查服務的狀況:

示例

sudo systemctl status mongod

儲存到 grepper

輸出

終端將顯示 MongoDB 服務的狀況。如果它成功執行,您將看到類似以下內容:

mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2023-06-26 10:32:15 UTC; 2 days ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 1234 (mongod)
    Tasks: 27 (limit: 4915)
   Memory: 251.3M
   CGroup: /system.slice/mongod.service
           └─1234 /usr/bin/mongod --config /etc/mongod.conf

Jun 26 10:32:15 server systemd[1]: Started MongoDB Database Server.

結論

恭喜!您已成功在 Linux 系統上安裝了 MongoDB 社群版 4.0。MongoDB 現在可以用於您的資料庫管理需求。請記住探索 MongoDB 文件以瞭解更多關於其功能和特性。

在本文中,我們介紹了分步安裝過程,包括匯入 GPG 金鑰、建立儲存庫檔案、更新包管理器、安裝 MongoDB、啟動服務和驗證安裝。

現在,您可以利用 MongoDB 面向文件的資料庫系統的強大功能,在您的 Linux 環境中構建可擴充套件且靈活的應用程式。編碼愉快!

更新於:2023-07-17

1K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告