使用本教程可以使用.rpm包在RedHatEnterpriseLinux或CentOSLinux版本6和7上安装MongoDB Community Edition 4.0。
本安装指南只支持64位系统.有关更多信息,请参见支持的平台。
MongoDB在自己的存储库中提供了官方支持的包:
Package Name | Description |
---|---|
mongodb-org | 将自动安装下面列出的四个组件包的元包。 |
mongodb-org-server | 包含mongodb,daemon、关联的init脚本和一个配置文件(/etc/mongod.conf)。您可以使用初始化脚本来使用配置文件启动mongod。有关详细信息,请参阅Run MongoDB CommunityEdition。 |
mongodb-org-mongos | 包含mongos守护进程。 |
mongodb-org-shell | 包含芒果壳shell。 |
mongodb-org-tools | 包含下列工具: mongoimport bsondump , mongodump , mongoexport , mongofiles , mongorestore , mongostat , and mongotop . |
Install MongoDB Community Edition 4.0
使用RPM软件包(推荐)
【1. 】配置包管理系统(YUM)Configure the package management system (yum
).
创建一个/etc/yum.pos.d/mongoDB-org-4.0.repo文件,以便您可以使用yum直接安装MongoDB:
Create a /etc/yum.repos.d/mongodb-org-4.0.repo
file so that you can install MongoDB directly using yum
:
$releasever须改成7
https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
NOTE
You can find .repo
files for each release in the repository itself. Odd-numbered minor release versions (e.g. 3.5) are development versions and are unsuitable for production use.
【2】安装MongoDB包 Install the MongoDB packages.
To install the latest stable version of MongoDB, issue the following command:
要安装MongoDB的最新稳定版本,使用以下命令
sudo yum install -y mongodb-org
To install a specific release of MongoDB, specify each component package individually and append the version number to the package name, as in the following example:
要安装MongoDB的特定版本,请分别指定每个组件包,并将版本号附加到包名中,如下面的示例所示
sudo yum install -y mongodb-org-4.0.8 mongodb-org-server-4.0.8 mongodb-org-shell-4.0.8 mongodb-org-mongos-4.0.8 mongodb-org-tools-4.0.8
You can specify any available version of MongoDB. However yum
upgrades the packages when a newer version becomes available. To prevent unintended upgrades, pin the package. To pin a package, add the following exclude
directive to your /etc/yum.conf
file:
您可以指定MongoDB的任何可用版本。但是,当更新版本可用时,yum将升级包。若要防止意外升级,请将包钉住。若要钉住包,请将以下排除指令添加到/etc/yum.conf文件中:
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
Run MongoDB Community Edition运行MongoDB社区版
Prerequisites先决条件
ulimit文件描述符
Most Unix-like operating systems limit the system resources that a session may use. These limits may negatively impact MongoDB operation. See UNIX ulimit Settings for more information.大多数类似Unix的操作系统限制会话可能使用的系统资源。这些限制可能会对MongoDB操作产生负面影响。有关详细信息,请参阅UNIX用户限制设置。
Directory Paths目录路径
To Use Default Directories使用默认目录
By default, MongoDB runs using the mongod
user account and uses the following default directories:
默认情况下,MongoDB使用mongod用户帐户运行,并使用下列默认目录:
/var/lib/mongo
(the data directory)/var/log/mongodb
(the log directory)
➤ If you installed via the package manager,如果您通过包管理器安装,
The default directories are created, and the owner and group for these directories are set to mongod
.
已创建默认目录,并将这些目录的所有者和组设置为mongod。
To Use Non-Default Directories使用非默认目录
To use a data directory and/or log directory other than the default directories:
若要使用数据目录和/或日志目录(默认目录除外),请执行以下操作:
TIP
Depending on your user permission, you may need to use sudo
to perform these operations.
根据用户权限,您可能需要使用sudo来执行这些操作。
-
Create the new directory or directories.创建一个或多个目录。
-
Edit the the configuration file
/etc/mongod.conf
and modify the following fields accordingly:编辑配置文件/etc/mongod.conf并相应地修改以下字段:storage.dbPath
to specify a new data directory path (e.g./some/data/directory
)systemLog.path
to specify a new log file path (e.g./some/log/directory/mongod.log
)
-
Ensure that the user running MongoDB has access to the directory or directories:
确保运行MongoDB的用户能够访问目录:chown -R mongod:mongod <directory>
If you change the user that runs the MongoDB process, you must give the new user access to these directories.如果更改运行MongoDB进程的用户,则必须授予新用户对这些目录的访问权限。
-
Configure SELinux if enforced. 如果强制的话,配置SELinux。See Configure SELinux.
Procedure
1
Start MongoDB.
You can start the mongod
process by issuing the following command:您可以通过发出以下命令启动mongod进程:
sudo service mongod start
2
Verify that MongoDB has started successfully验证MongoDB是否已成功启动
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您可以通过检查/var/log/mongoDB/mongod.log的日志文件的内容来验证Monod进程是否已成功启动
[initandlisten] waiting for connections on port <port>
where <port>
is the port configured in /etc/mongod.conf
, 27017
by default.其中<port>是在/etc/mongod.conf中配置的端口,默认为27017。
You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:
还可以通过发出以下命令,确保MongoDB将在系统重新启动之后启动:
sudo chkconfig mongod on
3
Stop MongoDB.
As needed, you can stop the mongod
process by issuing the following command:
根据需要,您可以通过发出以下命令来停止mongod进程:
sudo service mongod stop
4
Restart MongoDB.
You can restart the mongod
process by issuing the following command:
您可以通过发出以下命令重新启动Monod进程:
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.您可以通过查看/var/log/mongoDB/mongod.log文件中的输出来跟踪错误或重要消息的进程状态。
5
Begin using MongoDB.开始使用MongoDB。
Start a mongo
shell on the same host machine as the mongod
. You can run the mongo
shell without any command-line options to connect to a mongod
that is running on your localhost with default port 27017:
在同一个主机上启动一个mongo shell。您可以在没有任何命令行选项的情况下运行mongoshell来连接到在本地主机上运行的具有默认端口27017的mongo shell:
mongo
For more information on connecting using the mongo
shell, such as to connect to a mongod
instance running on a different host and/or port, see The mongo Shell.
To help you start using MongoDB, MongoDB provides Getting Started Guides in various driver editions. See Getting Started for the available editions.
有关使用mongoshell进行连接的更多信息,如连接到在不同主机和/或端口上运行的monday实例,请参见mongo Shell。为了帮助您开始使用MongoDB,MongoDB提供了各种驱动程序版本的入门指南。有关可用版本,请参见入门。
Uninstall MongoDB Community Edition
To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. The following section guides you through the necessary steps.
WARNING
This process will completely remove MongoDB, its configuration, and all databases. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.
1
Stop MongoDB.
Stop the mongod
process by issuing the following command:
sudo service mongod stop
2
Remove Packages.
Remove any MongoDB packages that you had previously installed.
sudo yum erase $(rpm -qa | grep mongodb-org)
3
Remove Data Directories.¶
Remove MongoDB databases and log files.
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo