An earlier version of this tutorial was written by Melissa Anderson.
本教程的早期版本由Melissa Anderson编写。
介绍 (Introduction)
MongoDB, also known as Mongo, is an open-source document database used in many modern web applications. It is classified as a NoSQL database because it does not rely on a traditional table-based relational database structure.
MongoDB ,也称为Mongo ,是许多现代Web应用程序中使用的开源文档数据库。 它被归类为NoSQL数据库,因为它不依赖于传统的基于表的关系数据库结构。
Instead, it uses JSON-like documents with dynamic schemas, meaning that, unlike relational databases, MongoDB does not require a predefined schema before you add data to a database. You can alter the schema at any time and as often as is necessary without having to set up a new database with an updated schema.
相反,它使用具有动态模式的类似JSON的文档,这意味着与关系数据库不同,MongoDB在将数据添加到数据库之前不需要预定义的模式。 您可以随时根据需要更改架构,而不必使用更新的架构设置新数据库。
In this tutorial you’ll install MongoDB on an Ubuntu 20.04 server, test it, and learn how to manage it as a systemd
service.
在本教程中,您将在Ubuntu 20.04服务器上安装MongoDB,对其进行测试,并学习如何将其作为systemd
服务进行管理。
先决条件 (Prerequisites)
To follow this tutorial, you will need:
要遵循本教程,您将需要:
One Ubuntu 20.04 server. This server should have a non-root administrative user and a firewall configured with UFW. Set this up by following our initial server setup guide for Ubuntu 20.04.
一台Ubuntu 20.04服务器。 此服务器应具有非root用户管理权限,并应使用UFW配置防火墙。 请遵循我们针对Ubuntu 20.04的初始服务器设置指南进行设置 。
第1步-安装MongoDB (Step 1 — Installing MongoDB)
Ubuntu’s official package repositories include a stable version of MongoDB. However, as of this writing, the version of MongoDB available from the default Ubuntu repositories is 3.6, while the latest stable release is 4.4.
Ubuntu的官方软件包存储库包括一个稳定的MongoDB版本。 但是,在撰写本文时,默认Ubuntu存储库中可用的MongoDB版本是3.6 ,而最新的稳定版本是4.4 。
To obtain the most recent version of this software, you must include MongoDB’s dedicated package repository to your APT sources. Then, you’ll be able to install mongodb-org
, a meta-package that always points to the latest version of MongoDB.
要获取此软件的最新版本,您必须在APT来源中包含MongoDB的专用软件包存储库。 然后,您将能够安装mongodb-org
,这是一个始终指向MongoDB最新版本的元软件包。
To start, import the public GPG key for the latest stable version of MongoDB. You can find the appropriate key file by navigating to the MongoDB key server and finding the file that includes the latest stable version number and ends in .asc
. For example, if you want to install version 4.4 of MongoDB, you’d look for the file named server-4.4.asc.
首先,导入用于最新稳定版本MongoDB的公共GPG密钥。 您可以通过导航到MongoDB密钥服务器并找到包含最新的稳定版本号并以.asc
结尾的文件来找到适当的密钥文件。 例如,如果要安装MongoDB的版本4.4,则将查找名为server-4.4.asc的文件。
Right-click on the file, and select Copy link address. Then, paste that link into the following curl
command, replacing the highlighted URL:
右键点击该文件,然后选择复制链接地址 。 然后,将该链接粘贴到以下curl
命令中,替换突出显示的URL:
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt键添加-
cURL is a command line tool available on many operating systems used to transfer data. It reads whatever data is stored at the URL passed to it and prints the content to the system’s output. In the following example, cURL prints the content of the GPG key file and then pipes it into the following sudo apt-key add -
command, thereby adding the GPG key to your list of trusted keys.
cURL是在许多用于传输数据的操作系统上可用的命令行工具。 它读取传递给它的URL上存储的所有数据,并将内容打印到系统的输出中。 在以下示例中,cURL打印GPG密钥文件的内容,然后将其通过管道传递到以下sudo apt-key add -
命令中,从而将GPG密钥添加到您的受信任密钥列表中。
Also, note that this curl
command uses the options -fsSL
which, together, essentially tell cURL to fail silently. This means that if for some reason cURL isn’t able to contact the GPG server or the GPG server is down, it won’t accidentally add the resulting error code to your list of trusted keys.
另外,请注意,该curl
命令使用-fsSL
选项,这些选项一起实际上告诉cURL静默失败。 这意味着,如果由于某种原因cURL无法联系GPG服务器或GPG服务器已关闭,则不会将结果错误代码意外添加到您的受信任密钥列表中。
This command will return OK
if the key was added successfully:
如果成功添加了密钥,此命令将返回OK
:
Output
OK
If you’d like to double check that the key was added correctly, you can do so with the following command:
如果您想再次检查是否正确添加了密钥,可以使用以下命令进行:
- apt-key list apt键列表
This will return the MongoDB key somewhere in the output:
这将在输出中的某处返回MongoDB键:
Output
/etc/apt/trusted.gpg
--------------------
pub rsa4096 2019-05-28 [SC] [expires: 2024-05-26]
2069 1EEC 3521 6C63 CAF6 6CE1 6564 08E3 90CF B1F5
uid [ unknown] MongoDB 4.4 Release Signing Key <packaging@mongodb.com>
. . .
At this point, your APT installation still doesn’t know where to find the mongodb-org
package you need to install the latest version of MongoDB.
此时,您的APT安装仍不知道在哪里可以找到安装最新版本的MongoDB所需的mongodb-org
软件包。
There are two places on your server where APT looks for online sources of packages to download and install: the sources.list
file and the sources.list.d
directory. sources.list
is a file that lists active sources of APT data, with one source per line and the most preferred sources listed first. The sources.list.d
directory allows you to add such sources.list
entries as separate files.
APT在服务器上的两个位置上查找要下载和安装的软件包的联机源: sources.list
文件和sources.list.d
目录。 sources.list
是一个文件,列出了APT数据的活动源,每行一个源,最优先的源列在最前面。 sources.list.d
目录允许您将此类sources.list
条目添加为单独的文件。
Run the following command, which creates a file in the sources.list.d
directory named mongodb-org-4.4.list
. The only content in this file is a single line reading deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse
:
运行以下命令,这将在sources.list.d
目录中创建一个名为mongodb-org-4.4.list
。 该文件中的唯一内容是一行,一行内容为deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse
:
- echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list 回声“ deb [arch = amd64,arm64] https://repo.mongodb.org/apt/ubuntu focus / mongodb-org / 4.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
This single line tells APT everything it needs to know about what the source is and where to find it:
这一行告诉APT它需要知道一切有关源是什么以及在哪里可以找到的所有信息:
deb
: This means that the source entry references a regular Debian architecture. In other cases, this part of the line might readdeb-src
, which means the source entry represents a Debian distribution’s source code.deb
:这意味着源条目引用了常规的Debian体系结构。 在其他情况下,该行的该部分可能显示为deb-src
,这意味着源条目代表Debian发行版的源代码。[ arch=amd64,arm64 ]
: This specifies which architectures the APT data should be downloaded to. In this case, it specifies theamd64
andarm64
architectures.[ arch=amd64,arm64 ]
:这指定应将APT数据下载到哪些体系结构。 在这种情况下,它指定了amd64
和arm64
体系结构。https://repo.mongodb.org/apt/ubuntu
: This is a URI representing the location where the APT data can be found. In this case, the URI points to the HTTPS address where the official MongoDB repository is located.https://repo.mongodb.org/apt/ubuntu
:这是一个URI,代表可以在其中找到APT数据的位置。 在这种情况下,URI指向官方MongoDB存储库所在的HTTPS地址。focal/mongodb-org/4.4
: Ubuntu repositories can contain several different releases. This specifies that you only want version4.4
of themongodb-org
package available for thefocal
release of Ubuntu (“Focal Fossa” being the code name of Ubuntu 20.04).focal/mongodb-org/4.4
:Ubuntu存储库可以包含几个不同的发行版。 这指定您只希望可将mongodb-org
软件包的4.4
版本用于Ubuntu的focal
发行版(“ Focal Fossa”是Ubuntu 20.04的代号)。multiverse
: This part points APT to one of the four main Ubuntu repositories. In this case, it’s pointing to themultiverse
repository.multiverse
:这部分将APT指向四个主要的Ubuntu存储库之一。 在这种情况下,它指向multiverse
存储库 。
After running this command, update your server’s local package index so APT knows where to find the mongodb-org
package:
运行此命令后,更新服务器的本地软件包索引,以便APT知道在哪里可以找到mongodb-org
软件包:
- sudo apt update sudo apt更新
Following that, you can install MongoDB:
之后,您可以安装MongoDB:
- sudo apt install mongodb-org sudo apt安装mongodb-org
When prompted, press Y
and then ENTER
to confirm that you want to install the package.
出现提示时,请按Y
,然后按ENTER
以确认您要安装该软件包。
When the command finishes, MongoDB will be installed on your system. However it isn’t yet ready to use. Next, you’ll start MongoDB and confirm that it’s working correctly.
命令完成后,MongoDB将安装在您的系统上。 但是,它尚未准备就绪。 接下来,您将启动MongoDB并确认其正常运行。
第2步-启动MongoDB服务并测试数据库 (Step 2 — Starting the MongoDB Service and Testing the Database)
The installation process described in the previous step automatically configures MongoDB to run as a daemon controlled by systemd
, meaning you can manage MongoDB using the various systemctl
commands. However, this installation procedure doesn’t automatically start the service.
上一步中描述的安装过程会自动将MongoDB配置为由systemd
控制的守护程序运行,这意味着您可以使用各种systemctl
命令来管理MongoDB。 但是,此安装过程不会自动启动服务。
Run the following systemctl
command to start the MongoDB service:
运行以下systemctl
命令以启动MongoDB服务:
- sudo systemctl start mongod.service sudo systemctl启动mongod.service
Then check the service’s status. Notice that this command doesn’t include .service
in the service file definition. systemctl
will append this suffix to whatever argument you pass automatically if it isn’t already present, so it isn’t necessary to include it:
然后检查服务的状态。 请注意,此命令在服务文件定义中不包括.service
。 如果尚不存在,则systemctl
会将后缀附加到您自动传递的任何参数上,因此不必将其包括在内:
- sudo systemctl status mongod sudo systemctl status mongod
This command will return output like the following, indicating that the service is up and running:
该命令将返回如下所示的输出,指示服务已启动并正在运行:
Output
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2020-06-09 12:57:06 UTC; 2s ago
Docs: https://docs.mongodb.org/manual
Main PID: 37128 (mongod)
Memory: 64.8M
CGroup: /system.slice/mongod.service
└─37128 /usr/bin/mongod --config /etc/mongod.conf
After confirming that the service is running as expected, enable the MongoDB service to start up at boot:
确认服务正在按预期运行后,启用MongoDB服务以在启动时启动:
- sudo systemctl enable mongod sudo systemctl启用mongod
You can further verify that the database is operational by connecting to the database server and executing a diagnostic command. The following command will connect to the database and output its current version, server address, and port. It will also return the result of MongoDB’s internal connectionStatus
command:
您可以通过连接到数据库服务器并执行诊断命令来进一步验证数据库是否可运行。 以下命令将连接到数据库并输出其当前版本,服务器地址和端口。 它还将返回MongoDB的内部connectionStatus
命令的结果:
- mongo --eval 'db.runCommand({ connectionStatus: 1 })' mongo --eval'db.runCommand({connectionStatus:1})'
connectionStatus
will check and return the status of the database connection. A value of 1
for the ok
field in the response indicates that the server is working as expected:
connectionStatus
将检查并返回数据库连接的状态。 响应中ok
字段的值为1
表示服务器正在按预期方式工作:
Output
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1dc7d67a-0af5-4394-b9c4-8a6db3ff7e64") }
MongoDB server version: 4.4.0
{
"authInfo" : {
"authenticatedUsers" : [ ],
"authenticatedUserRoles" : [ ]
},
"ok" : 1
}
Also, note that the database is running on port 27017
on 127.0.0.1
, the local loopback address representing localhost. This is MongoDB’s default port number.
另外,请注意,数据库正在127.0.0.1
端口27017
上运行,本地回送地址表示localhost 。 这是MongoDB的默认端口号。
Next, we’ll look at how to manage the MongoDB server instance with systemd
.
接下来,我们将研究如何使用systemd
管理MongoDB服务器实例。
第3步-管理MongoDB服务 (Step 3 — Managing the MongoDB Service)
As mentioned previously, the installation process described in Step 1 configures MongoDB to run as a systemd
service. This means that you can manage it using standard systemctl
commands as you would with other Ubuntu system services.
如前所述,步骤1中描述的安装过程将MongoDB配置为作为systemd
服务运行。 这意味着您可以像使用其他Ubuntu系统服务一样使用标准的systemctl
命令对其进行管理。
As mentioned previously, the systemctl status
command checks the status of the MongoDB service:
如前所述, systemctl status
命令检查MongoDB服务的状态:
- sudo systemctl status mongod sudo systemctl status mongod
You can stop the service anytime by typing:
您可以随时输入以下命令来停止服务:
- sudo systemctl stop mongod sudo systemctl停止mongod
To start the service when it’s stopped, run:
要在服务停止时启动它,请运行:
- sudo systemctl start mongod sudo systemctl启动mongod
You can also restart the server when it’s already running:
您还可以在服务器已经运行时重新启动它:
- sudo systemctl restart mongod sudo systemctl重新启动mongod
In Step 2, you enabled MongoDB to start automatically with the server. If you ever wish to disable this automatic startup, type:
在步骤2中,您使MongoDB能够从服务器自动启动。 如果您希望禁用此自动启动,请键入:
- sudo systemctl disable mongod sudo systemctl禁用mongod
Then to re-enable it to start up at boot, run the enable
command again:
然后要重新启用它以在启动时启动,请再次运行enable
命令:
- sudo systemctl enable mongod sudo systemctl启用mongod
For more information on how to manage systemd
services, check out Systemd Essentials: Working with Services, Units, and the Journal.
有关如何管理systemd
服务的更多信息,请查看Systemd Essentials:使用服务,单元和日志 。
结论 (Conclusion)
In this tutorial, you added the official MongoDB repository to your APT instance, and installed the latest version of MongoDB. You then tested Mongo’s functionality and practiced some systemctl
commands.
在本教程中,您将官方的MongoDB存储库添加到了APT实例,并安装了最新版本的MongoDB。 然后,您测试了Mongo的功能并练习了一些systemctl
命令。
As an immediate next step, we strongly recommend that you harden your MongoDB installation’s security by following our guide on How To Secure MongoDB on Ubuntu 20.04. Once it’s secured, you could then configure MongoDB to accept remote connections.
下一步,我们强烈建议您遵循有关如何在Ubuntu 20.04上保护MongoDB的指南来加强MongoDB安装的安全性。 一旦安全,就可以将MongoDB配置为接受远程连接 。
You can find more tutorials on how to configure and use MongoDB in these DigitalOcean community articles. We also encourage you to check out the official MongoDB documentation, as it’s a great resource on the possibilities that MongoDB provides.
在这些DigitalOcean社区文章中,您可以找到有关如何配置和使用MongoDB的更多教程。 我们也鼓励您查看MongoDB的官方文档 ,因为它是有关MongoDB提供的可能性的重要资源。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-20-04