On this page
在 Amazon Linux 上安装 MongoDB Community Edition
在本页面
MongoDB Atlas and AWS
MongoDB Atlas是 AWS 上的托管 MongoDB 服务,用于启动,运行和维护 MongoDB 集群。
Overview
使用本教程,可以使用yum
包 Management 器在 Amazon Linux 上安装 MongoDB 3.6 Community Edition。
验证 Linux 发行版
您可以通过在命令行上运行以下命令来验证正在运行的 Linux 发行版:
grep ^NAME /etc/*release
结果应为 Amazon Linux AMI 。如果使用其他 Linux 发行版,请参阅为您的平台安装说明。
MongoDB Version
本教程将安装 MongoDB 3.6 Community Edition。要安装其他版本的 MongoDB 社区,请使用此页面左上角的版本下拉菜单选择该版本的文档。
Considerations
Platform Support
MongoDB 3.6 Community Edition 在x86_64体系结构上支持以下 64 位 Amazon Linux 版本:
- Amazon Linux(2013.03 或更高版本)
MongoDB 仅支持此平台的 64 位版本。
有关更多信息,请参见Supported Platforms。
Production Notes
在生产环境中部署 MongoDB 之前,请考虑Production Notes文档,该文档提供了生产 MongoDB 部署的性能注意事项和配置建议。
安装 MongoDB 社区版
请按照以下步骤使用yum
程序包 Management 器安装 MongoDB 社区版。
配置程序包 Management 系统(yum)。
创建一个/etc/yum.repos.d/mongodb-org-3.6.repo
文件,以便您可以使用yum
直接安装 MongoDB:
在版本 3.0 中进行了更改:MongoDB Linux 软件包位于从 3.0 开始的新存储库中。
对于 MongoDB 3.6
使用以下存储库文件:
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
适用于 3.6 之前的 MongoDB 版本
要从早期的release series(例如 3.4)安装软件包,可以在存储库配置中指定发行系列。例如,要将系统限制为 3.4 版本系列,请创建一个/etc/yum.repos.d/mongodb-org-3.4.repo
文件,以保存 MongoDB 3.4 存储库的以下配置信息:
[mongodb-org-3.4]
name=MongoDB 3.4 Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.4/x86_64/
gpgcheck=0
enabled=1
您也可以直接从MongoDB repository下载.rpm
文件。下载按 Amazon Linux 版本(例如2013.03
),MongoDB release version(例如3.6
)和体系结构(例如x86_64
)进行组织。奇数编号的 MongoDB 发布版本(例如3.7
)是开发版本,不适合生产部署。
安装 MongoDB 软件包。
要安装最新的稳定版 MongoDB,请发出以下命令:
sudo yum install -y mongodb-org
要安装特定版本的 MongoDB,请分别指定每个组件软件包,并将版本号附加到软件包名称中,如以下示例所示:
sudo yum 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 版本。但是,yum
将在更新版本可用时升级软件包。为防止意外升级,请固定包装。要固定包,请在您的/etc/yum.conf
文件中添加以下exclude
指令:
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
运行 MongoDB 社区版
ulimit Considerations
- 大多数类 Unixos 都限制了会话可能使用的系统资源。这些限制可能会对 MongoDB 的运行产生负面影响。有关更多信息,请参见UNIX ulimit 设置。
Directories
- 默认情况下,MongoDB 实例存储:
/var/lib/mongo
中的数据文件其日志文件位于
/var/log/mongodb
如果通过程序包 Management 器安装,则这些默认目录是在安装过程中创建的。
如果通过下载压缩包手动安装,则可以使用mkdir -p <directory>
或sudo mkdir -p <directory>
创建目录,具体取决于将运行 MongoDB 的用户。 (有关mkdir
和sudo
的信息,请参见您的 Linux 手册页.)
默认情况下,MongoDB 使用mongod
用户帐户运行。如果您更改运行 MongoDB 进程的用户,则必须**还修改对/var/lib/mongo
和/var/log/mongodb
目录的权限,以使该用户可以访问这些目录。
要指定其他日志文件目录和数据文件目录,请编辑/etc/mongod.conf
中的systemLog.path和storage.dbPath设置。确保运行 MongoDB 的用户有权访问这些目录。
Procedure
请按照以下步骤运行 MongoDB Community Edition。这些说明假定您使用的是默认设置。
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 初始化(服务)”标签。
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.
You can verify that the mongod process has started successfully by issuing the following command:
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.
You can start the mongod process by issuing the following command:
sudo service mongod start
Verify that MongoDB has started successfully
You can verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log
for a line reading
[initandlisten] waiting for connections on port <port>
where <port>
is the port configured in /etc/mongod.conf
, 27017
by default.
You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:
sudo chkconfig mongod on
Stop MongoDB.
As needed, you can stop the mongod process by issuing the following command:
sudo service mongod stop
Restart MongoDB.
You can restart the mongod process by issuing the following command:
sudo service mongod restart
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.
卸载 MongoDB 社区版
要从系统中完全删除 MongoDB,必须删除 MongoDB 应用程序本身,配置文件以及任何包含数据和日志的目录。以下部分将指导您完成必要的步骤。
Warning
此过程将“完全”删除 MongoDB,其配置和“所有”数据库。此过程是不可逆的,因此请确保在 continue 操作之前备份所有配置和数据。
Stop MongoDB.
通过发出以下命令来停止mongod进程:
sudo service mongod stop
Remove Packages.
删除以前安装的所有 MongoDB 软件包。
sudo yum erase $(sudo rpm -qa | grep mongodb-org)
删除数据目录。
删除 MongoDB 数据库和日志文件。
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo
Additional Information
默认情况下,localhost 绑定
默认情况下,MongoDB 在bindIp设置为127.0.0.1
的情况下启动,该绑定到 localhost 网络接口。这意味着mongod
仅可以接受来自同一计算机上运行的 Client 端的连接。远程 Client 端将无法连接到mongod
,并且mongod
将无法初始化replica set,除非将此值设置为有效的网络接口。
可以配置此值:
Warning
绑定到非 localhost(例如可公开访问)的 IP 地址之前,请确保已保护群集免受未经授权的访问。有关安全建议的完整列表,请参见Security Checklist。至少考虑enabling authentication和加强网络基础设施。
有关配置bindIp的更多信息,请参见MongoDB 配置强化。
MongoDB 社区版软件包
MongoDB 社区版可从其自己的专用存储库中获得,并且包含以下官方支持的软件包:
Package Name | Description |
---|---|
mongodb-org |
metapackage 将自动安装下面列出的四个组件包。 |
mongodb-org-server |
包含mongod守护程序以及相关的配置和初始化脚本。 |
mongodb-org-mongos |
包含mongos守护程序。 |
mongodb-org-shell |
包含mongo shell。 |
mongodb-org-tools |
包含以下 MongoDB 工具:mongoimport bsondump,mongodump,mongoexport,mongofiles,mongoperf,mongorestore,mongostat和mongotop。 |
mongodb-org-server
软件包提供了一个初始化脚本,该初始化脚本以/etc/mongod.conf
配置文件开头mongod。
有关使用此初始化脚本的详细信息,请参见运行 MongoDB 社区版。