如何在 Ubuntu 16.04 上安裝 MongoDB
MongoDB是一個跨平臺的、面向文件的資料庫,它提供高效能、高可用性和易於擴充套件性。MongoDB 基於集合和文件的概念。MongoDB維護者尚未釋出官方的Ubuntu 16.04 MongoDB軟體包。本文介紹了“如何在Ubuntu上安裝MongoDB並在啟動時啟動MongoDB服務”。
新增 MongoDB 倉庫
MongoDB 通常包含在 Ubuntu 軟體包倉庫中。但是,使用官方的 MongoDB 倉庫可以以推薦的方式獲得最新的版本更新。
要執行此過程,我們首先必須使用以下命令匯入官方 MongoDB 倉庫的金鑰:
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
示例輸出應如下所示:
Executing: /tmp/tmp.Qe1RFZgUh2/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 gpg: requesting key EA312927 from hkp server keyserver.ubuntu.com gpg: key EA312927: public key "MongoDB 3.2 Release Signing Key <packaging@mongodb.com>" imported gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1)
要新增 MongoDB 倉庫,請使用以下命令:
$ sudo echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
現在,我們需要使用以下命令更新軟體包列表:
$ sudo apt-get update
安裝和驗證 MongoDB
要安裝 MongoDB,請使用以下命令:
$ sudo apt-get install -y --allow-unauthenticated mongodb-org
示例輸出應如下所示:
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: apport-hooks-elementary contractor javascript-common libgda-5.0-4 libgda-5.0-common libgranite-common libgranite3 libgsignon-glib1 libindicate5 libjs-jquery libnoise-core0 libtagc0 Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools The following NEW packages will be installed: mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools 0 upgraded, 5 newly installed, 0 to remove and 168 not upgraded. Need to get 47.4 MB of archives. After this operation, 218 MB of additional disk space will be used. Get:1 http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2/multiverse amd64 mongodb-org-shell amd64 3.2.6 [5,257 kB] Get:2 http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2/multiverse amd64 mongodb-org-server amd64 3.2.6 [9,541 kB] Get:3 http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2/multiverse amd64 mongodb-org-mongos amd64 3.2.6 [4,337 kB] ...................................................................................
為了在 Ubuntu 16.04 上正確啟動 MongoDB 作為服務,我們還需要建立一個描述服務的單元檔案。單元檔案告訴系統如何管理資源。最常見的單元型別是服務,它決定如何啟動或停止服務。
建立一個名為 mongodb.service 的配置檔案來設定單元檔案,如下所示:
$ sudo nano /etc/systemd/system/mongodb.service
貼上以下程式碼:
[Unit] Description=High-performance, schema-free document-oriented database After=network.target [Service] User=mongodb ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf [Install] WantedBy=multi-user.target
現在使用 **systemctl** 啟動新建立的服務,如下所示:
$ sudo systemctl start mongodb
它通常不會返回任何輸出。現在,我們需要檢查服務是否已正確啟動。要執行此過程,請使用以下命令:
$ sudo systemctl status mongodb
示例輸出應如下所示:
mongodb.service - High-performance, schema-free document-oriented database Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset: Active: active (running) since Fri 2016-05-13 10:33:52 IST; 1min 48s ago Main PID: 7078 (mongod) Tasks: 16 (limit: 512) CGroup: /system.slice/mongodb.service └─7078 /usr/bin/mongod --quiet --config /etc/mongod.conf May 13 10:33:52 linux systemd[1]: Started High-performance, schema-free document lines 1-9/9 (END)
要啟用在系統啟動時自動啟動 MongoDB 的過程,請使用以下命令:
$ sudo systemctl enable mongodb
示例輸出應如下所示:
Created symlink from /etc/systemd/system/multi-user.target.wants/mongodb.service to /etc/systemd/system/mongodb.service.
閱讀本文後,您將能夠了解如何在 Ubuntu 16.04 上安裝 MongoDB。在我們的下一篇文章中,我們將提供更多基於 Linux 的技巧和提示。敬請關注!
廣告