MongoDB 是一种流行的开源、面向文档的 NoSQL 数据库,提供高性能、可扩展性和灵活性。由于它能够存储和管理大量非结构化数据,因此在现代 Web 应用程序中得到广泛使用。 MongoDB 可以安装在各种操作系统上,包括 Ubuntu 22.04。
在本教程中,我们将逐步完成在 Ubuntu 22.04 上安装和保护 MongoDB 的步骤。我们将介绍 MongoDB 社区版和企业版的安装过程。此外,我们将完成通过身份验证、SSL/TLS 加密和防火墙规则保护 MongoDB 的过程。
在本文中,我们将讨论以下主题:
- 在 Ubuntu 22.04 上安装 MongoDB
- 使用密码保护 MongoDB
- 为 MongoDB 启用身份验证
- 限制对 MongoDB 的访问
- 使用 UFW 在防火墙中打开端口
先决条件
在继续之前,请确保您全新安装了 Ubuntu 22.04,并且是具有 sudo 权限的非 root 用户。另外,请通过运行以下命令确保您的系统是最新的:
sudo apt update and sudo apt upgrade
sudo apt install gnupg2
一旦您的系统是最新的,我们就可以继续安装 MongoDB。
第 1 步:安装 MongoDB
在 Ubuntu 上安装 MongoDB 是一个相对简单的过程。您可以使用 Ubuntu 包管理器安装它,也可以直接从 MongoDB 网站下载它。
以下步骤将在您的 Ubuntu 系统上配置官方 MongoDB PPA 并安装它:
-
导入 MongoDB GPG 密钥:
wget -nc https://www.mongodb.org/static/pgp/server-6.0.asc
cat server-6.0.asc | gpg --dearmor | sudo tee /etc/apt/keyrings/mongodb.gpg >/dev/null
这些命令将导入 MongoDB 存储库的 GPG 密钥,用于验证存储库中包的真实性。
-
将 MongoDB 存储库添加到系统的包管理器中:
sudo sh -c 'echo "deb [ arch=amd64,arm64 signed-by=/etc/apt/keyrings/mongodb.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" >> /etc/apt/sources.list.d/mongo.list'
此命令会将 MongoDB 存储库添加到系统包管理器的存储库列表中。
-
更新可用包的列表:
sudo apt update
此命令将更新可用包的列表以包含 MongoDB 存储库中的包。
-
安装 MongoDB:
sudo apt install mongodb-org
此命令将从 MongoDB 存储库安装最新稳定版本的 MongoDB。
-
启动 MongoDB 服务:
sudo systemctl start mongod
该命令将启动 MongoDB 服务。
上述步骤将在Ubuntu系统上成功安装MongoDB。
接下来,启用 MongoDB 数据库的身份验证非常重要。这将要求用户输入用户名和密码才能访问数据库。如果不启用身份验证,任何有权访问服务器的人都可以查看和修改数据库中的数据。
第 2 步:使用密码保护 MongoDB
默认情况下,MongoDB 不需要密码即可访问数据库。但是,建议设置密码以保护数据库并防止未经授权的访问。
要为 MongoDB 设置密码,请按照下列步骤操作:
- Connect to the MongoDB shell:
mongosh
该命令将打开 MongoDB shell,这是一个用于与数据库交互的命令行界面。
- Switch to the admin database:
use admin
该命令将切换到`admin`
数据库,用于管理数据库中的用户和角色。
db.createUser({
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
})
Replace `admin`
and `password`
以及新用户所需的用户名和密码。该命令将使用`root`
中的角色`admin`
数据库。 root 角色具有对所有数据库资源和功能的完全访问权限。
- 退出 MongoDB shell:
exit
步骤 3:启用身份验证
默认情况下,MongoDB 访问数据库不需要身份验证。但是,建议启用身份验证以保护数据库并防止未经授权的访问。
要启用 MongoDB 身份验证,请按照下列步骤操作:
- Edit the MongoDB configuration file:
sudo nano /etc/mongod.conf
此命令将在 Nano 文本编辑器中打开 MongoDB 配置文件。
- Find the security section in the configuration file and add the following lines:
security:
authorization: enabled
这将启用 MongoDB 的授权功能,该功能要求用户使用用户名和密码进行身份验证才能访问数据库。
- 保存更改并退出文本编辑器。
- Restart the MongoDB service:
sudo systemctl restart mongod
此命令会将更改应用到 MongoDB 配置并重新启动服务。
步骤 4:限制对 MongoDB 的访问
默认情况下,MongoDB 允许来自任何 IP 地址的连接。但是,建议将对数据库的访问限制为特定的 IP 地址或范围以提高安全性。
要限制对 MongoDB 的访问,请按照下列步骤操作:
- Edit the MongoDB configuration file:
sudo nano /etc/mongod.conf
此命令将在 Nano 文本编辑器中打开 MongoDB 配置文件。
- Find the net section in the configuration file and add the following lines:
net:
bindIp: 127.0.0.1,192.168.1.0/24
将 127.0.0.1,192.168.1.0/24 替换为所需的 IP 地址或允许的范围。这将对数据库的访问限制为指定的 IP 地址或范围。
- 保存更改并退出文本编辑器。
- Restart the MongoDB service:
sudo systemctl restart mongod
此命令会将更改应用到 MongoDB 配置并重新启动服务。
第5步:打开防火墙端口(UFW)
要在运行 ufw 的系统上的防火墙中打开 MongoDB 端口 (27017),请执行以下步骤:
- Check the status of the firewall:
sudo ufw status
该命令将显示防火墙的当前状态。如果防火墙未激活,您需要先启动它,然后才能打开 MongoDB 端口。
- If the firewall is not active, start it by running the following command:
sudo ufw enable
此命令将启用防火墙并允许传入系统的连接。
- Open the MongoDB port in the firewall:
sudo ufw allow 27017
此命令将在防火墙中打开 MongoDB 端口 (27017) 并允许传入该端口的连接。
- 检查开放端口列表以验证 MongoDB 端口是否已开放:
sudo ufw status
完成这些步骤后,MongoDB 端口应在防火墙中打开,并且您应该能够从其他系统连接到数据库。
结论
在本文中,我们介绍了如何在 Ubuntu 22.04 上安装和保护 MongoDB。我们介绍了如何安装 MongoDB、设置密码、启用数据库身份验证以及限制对特定 IP 地址或范围的访问。通过执行这些步骤,您可以确保 MongoDB 安装是安全的,并且只有授权用户才能访问。
我希望这可以帮助您了解如何在 Ubuntu 22.04 上安装和保护 MongoDB。如果您还有任何疑问,请随时询问。