在 Ubuntu 上安装 MongoDB Community Edition

在本页面

Overview

使用本教程,可以使用apt软件包 Management 器在 Ubuntu Linux 的 LTS(长期支持)版本上安装 MongoDB 3.6 社区版。

MongoDB Version

本教程将安装 MongoDB 3.6 Community Edition。要安装其他版本的 MongoDB 社区,请使用此页面左上角的版本下拉菜单选择该版本的文档。

Considerations

Platform Support

EOL Notice

  • MongoDB 3.6.4 企业版在x86_64上删除了对 Ubuntu 12.04(“精确”)的支持

MongoDB 3.6 Community Edition 在x86_64体系结构上支持以下 64 位 Ubuntu LTS(长期支持)版本:

  • 16 .04 LTS(“ Xenial”)

  • 14 .04 LTS(“信任”)

MongoDB 仅支持这些平台的 64 位版本。

Ubuntu 上的 MongoDB 3.6 社区版还支持某些平台上的ARM64体系结构。

有关更多信息,请参见Supported Platforms

Windows Subsystem for Linux (WSL) - Unsupported

MongoDB 不支持 Linux 的 Windows 子系统(WSL)。

Production Notes

在生产环境中部署 MongoDB 之前,请考虑Production Notes文档,该文档提供了生产 MongoDB 部署的性能注意事项和配置建议。

MongoDB 官方软件包

要在 Ubuntu 系统上安装 MongoDB 社区,这些说明将使用官方mongodb-org软件包,该软件包由 MongoDB Inc.维护和支持。官方mongodb-org软件包始终包含 MongoDB 的最新版本,可从其自己的专用存储库中获得。

Important

Ubuntu 提供的mongodb软件包不是由 MongoDB Inc.维护,与官方mongodb-org软件包冲突。如果您已经在 Ubuntu 系统上安装了mongodb软件包,则必须**先卸载mongodb软件包,然后再按照这些说明进行操作。

有关官方软件包的完整列表,请参见MongoDB 社区版软件包

安装 MongoDB 社区版

请按照以下步骤使用apt程序包 Management 器安装 MongoDB 社区版。

导入软件包 Management 系统使用的公钥。

在终端上,发出以下命令以从https://www.mongodb.org/static/pgp/server-3.6.asc导入 MongoDB 公共 GPG 密钥:

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

该操作应以OK响应。

为 MongoDB 创建一个列表文件。

为您的 Ubuntu 版本创建列表文件/etc/apt/sources.list.d/mongodb-org-3.6.list。单击适合您的 Ubuntu 版本的选项卡。如果不确定主机正在运行哪个 Ubuntu 版本,请在主机上打开终端或 shell 并执行lsb_release -dc

Ubuntu 16.04 (Xenial)
Ubuntu 14.04 (Trusty)

The following instruction is for Ubuntu 16.04 (Xenial) . For Ubuntu 14.04 (Trusty), click on the appropriate tab.

Create the /etc/apt/sources.list.d/mongodb-org-3.6.list file for Ubuntu 16.04 (Xenial):

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

The following instruction is for Ubuntu 14.04 (Trusty) . For Ubuntu 16.04 (Xenial), click on the appropriate tab.

Create the /etc/apt/sources.list.d/mongodb-org-3.6.list file for Ubuntu 14.04 (Trusty):

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

重新加载本地软件包数据库。

发出以下命令以重新加载本地软件包数据库:

sudo apt-get update

安装 MongoDB 软件包。

安装最新的稳定版 MongoDB。

发出以下命令:

sudo apt-get install -y mongodb-org

安装特定版本的 MongoDB。

要安装特定发行版,必须分别指定每个组件包以及版本号,如以下示例所示:

sudo apt-get install -y mongodb-org=3.6.19 mongodb-org-server=3.6.19 mongodb-org-shell=3.6.19 mongodb-org-mongos=3.6.19 mongodb-org-tools=3.6.19

如果您仅安装mongodb-org=3.6.19并且不包括组件包,则无论您指定哪个版本,都将安装每个 MongoDB 包的最新版本。

固定特定版本的 MongoDB。

尽管您可以指定任何可用的 MongoDB 版本,但是apt-get将在更新版本可用时升级软件包。为防止意外升级,请固定包装。要将 MongoDB 的版本固定为当前安装的版本,请发出以下命令序列:

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

有关在 Ubuntu 上安装 MongoDB 时遇到的故障排除问题的帮助,请参阅我们的troubleshooting指南。

运行 MongoDB 社区版

  • ulimit Considerations

    • 大多数类 Unixos 都限制了会话可能使用的系统资源。这些限制可能会对 MongoDB 的运行产生负面影响。有关更多信息,请参见UNIX ulimit 设置
  • Directories

    • 如果通过程序包 Management 器安装,则在安装过程中将创建数据目录/var/lib/mongodb和日志目录/var/log/mongodb

默认情况下,MongoDB 使用mongodb用户帐户运行。如果您更改运行 MongoDB 进程的用户,则还必须修改对数据和日志目录的权限,以使该用户可以访问这些目录。

  • Configuration File

    • 官方的 MongoDB 软件包包括configuration file(/etc/mongod.conf)。这些设置(例如数据目录和日志目录规范)在启动时生效。也就是说,如果在运行 MongoDB 实例时更改配置文件,则必须重新启动实例以使更改生效。

Procedure

请按照以下步骤在系统上运行 MongoDB Community Edition。这些说明假定您使用的是官方的mongodb-org软件包,而不是 Ubuntu 提供的非官方的mongodb软件包,并且使用的是默认设置。

Init System

要运行和 Managementmongod进程,您将使用 os 的内置init system。最新版本的 Linux 倾向于使用 systemd (使用systemctl命令),而较早版本的 Linux 倾向于使用 System V init (使用service命令)。

如果不确定平台使用哪个初始化系统,请运行以下命令:

ps --no-headers -o comm 1

然后根据结果在下面选择适当的选项卡:

  • systemd-选择下面的“ systemd(systemctl)”标签。

  • init-选择下面的“系统 V 初始化(服务)”标签。

systemd (systemctl)
System V Init (service)

Start MongoDB.

You can start the mongod process by issuing the following command:

sudo systemctl start mongod

If you receive an error similar to the following when starting mongod:

Failed to start mongod.service: Unit mongod.service not found.

Run the following command first:

sudo systemctl daemon-reload

Then run the start command above again.

Verify that MongoDB has started successfully.

sudo systemctl status mongod

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:

sudo systemctl enable mongod

Stop MongoDB.

As needed, you can stop the mongod process by issuing the following command:

sudo systemctl stop mongod

Restart MongoDB.

You can restart the mongod process by issuing the following command:

sudo systemctl restart mongod

You can follow the state of the process for errors or important messages by watching the output in the /var/log/mongodb/mongod.log file.

Begin using MongoDB.

Start a mongo shell on the same host machine as the mongod. Use the --host command line option to specify the localhost address and port that the mongod listens on:

mongo --host 127.0.0.1:27017

Later, to stop MongoDB, press Control+C in the terminal where the mongod instance is running.

Start MongoDB.

Issue the following command to start mongod:

sudo service mongod start

Verify that MongoDB has started successfully

Verify that the mongod process has started successfully:

sudo service mongod status

You can also check the log file for the current status of the mongod process, located at: /var/log/mongodb/mongod.log by default. A running mongod instance will indicate that it is ready for connections with the following line:

[initandlisten] waiting for connections on port 27017

Stop MongoDB.

As needed, you can stop the mongod process by issuing the following command:

sudo service mongod stop

Restart MongoDB.

Issue the following command to restart mongod:

sudo service mongod restart

Begin using MongoDB.

Start a mongo shell on the same host machine as the mongod. Use the --host command line option to specify the localhost address and port that the mongod listens on:

mongo --host 127.0.0.1:27017

Later, to stop MongoDB, press Control+C in the terminal where the mongod instance is running.

卸载 MongoDB 社区版

要从系统中完全删除 MongoDB,必须删除 MongoDB 应用程序本身,配置文件以及任何包含数据和日志的目录。以下部分将指导您完成必要的步骤。

Warning

此过程将“完全”删除 MongoDB,其配置和“所有”数据库。此过程是不可逆的,因此请确保在 continue 操作之前备份所有配置和数据。

Stop MongoDB.

通过发出以下命令来停止mongod进程:

sudo service mongod stop

Remove Packages.

删除以前安装的所有 MongoDB 软件包。

sudo apt-get purge mongodb-org*

删除数据目录。

删除 MongoDB 数据库和日志文件。

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

Additional Information

默认情况下,localhost 绑定

默认情况下,MongoDB 在bindIp设置为127.0.0.1的情况下启动,该绑定到 localhost 网络接口。这意味着mongod仅可以接受来自同一计算机上运行的 Client 端的连接。远程 Client 端将无法连接到mongod,并且mongod将无法初始化replica set,除非将此值设置为有效的网络接口。

可以配置此值:

  • 在带有bindIp的 MongoDB 配置文件中,或者

  • 通过命令行参数--bind_ip

Warning

绑定到非 localhost(例如可公开访问)的 IP 地址之前,请确保已保护群集免受未经授权的访问。有关安全建议的完整列表,请参见Security Checklist。至少考虑enabling authentication加强网络基础设施

有关配置bindIp的更多信息,请参见MongoDB 配置强化

MongoDB 社区版软件包

MongoDB 社区版可从其自己的专用存储库中获得,并且包含以下官方支持的软件包:

Package NameDescription
mongodb-orgmetapackage将自动安装下面列出的四个组件包。
mongodb-org-server包含mongod守护程序以及相关的配置和初始化脚本。
mongodb-org-mongos包含mongos守护程序。
mongodb-org-shell包含mongo shell。
mongodb-org-tools包含以下 MongoDB 工具:mongoimport bsondumpmongodumpmongoexportmongofilesmongoperfmongorestoremongostatmongotop

mongodb-org-server软件包提供了一个初始化脚本,该初始化脚本以/etc/mongod.conf配置文件开头mongod

有关使用此初始化脚本的详细信息,请参见运行 MongoDB 社区版