一. 安装 mongodb
1. 导入包管理系统使用的公钥 (Import the public key used by the package management system )
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
2. 为MongoDB创建一个列表文件 (Create a list file for MongoDB.)
/etc/apt/sources.list.d/mongodb-org-3.6.list使用适合您的Ubuntu版本的命令创建列表文件:
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
3. 重新加载本地包数据库 (Reload local package database)
发出以下命令重新加载本地包数据库:
sudo apt-get update
4. 安装MongoDB包 (Install the MongoDB packages)
安装最新的稳定版本的MongoDB [Install the latest stable version of MongoDB]
sudo apt-get install -y mongodb-org
二. 运行mongodb服务
MongoDB实例默认存储其数据文件/var/lib/mongodb 及其日志文件/var/log/mongodb,并使用mongodb 用户帐户运行。您可以在中指定备用日志和数据文件目录/etc/mongod.conf。请参阅systemLog.path
并storage.dbPath
获取更多信息。
如果更改运行MongoDB进程的用户,则 必须修改/var/lib/mongodb和 /var/log/mongodb目录的访问控制权限,以使该用户可以访问这些目录。
1. Start MongoDB.
sudo service mongod start
2. Verify(确认) that MongoDB has started successfully
验证mongod
过程已成功启动通过检查日志文件的内容 /var/log/mongodb/mongod.log
对于这样一行
[initandlisten] waiting for connections on port 27017
是mongod
监听的端口。如果您修改了 配置文件中的net.port
设置/etc/mongod.conf
,端口可能会有所不同。
如果您修改了systemLog.path
配置文件选项,请在您为该设置指定的位置查找日志文件。
您可能会在mongod
输出中看到非严重警告。只要你看到上面显示的日志行,你可以放心地忽略这些警告,在你最初的MongoDB评估。
3. Stop(停止) MongoDB.
根据需要,您可以mongod
通过发出以下命令来停止进程: (As needed, you can stop the mongod
process by issuing the following command:
)
sudo service mongod stop
4. Restart(重启) MongoDB.
sudo service mongod restart
5. 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.
三. 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.
sudo service mongod stop
2. Remove Packages.
Remove any MongoDB packages that you had previously installed.
sudo apt-get purge mongodb-org*
3. Remove Data Directories.
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb