如何在 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 的技巧和提示。敬请关注!

更新于:2020年1月22日

495 次浏览

开启你的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.