按照本教程学习如何在 Ubuntu 20.04 上安装 MongoDB。 根据 mongodb.com,“MongoDB 是一个通用的、基于文档的分布式数据库,为现代应用程序开发人员和云时代构建……它是一个可扩展、灵活的 NoSQL 文档数据库平台,旨在克服关系数据库方法和其他 NoSQL 解决方案的局限性。“
阅读 MongoDB 的 5 大功能。
在 Ubuntu 20.04 上安装 MongoDB
MongoDB 有社区版和企业版两种版本;
- MongoDB 社区是 MongoDB 的可用和免费使用版本。
- MongoDB Enterprise 作为 MongoDB Enterprise Advanced 订阅的一部分提供,包括对 MongoDB 部署的全面支持。 MongoDB Enterprise 还添加了以企业为中心的功能,例如 LDAP 和 Kerberos 支持、磁盘加密和审计。
在本教程中,我们将学习如何在 Ubuntu 20.04 上安装 MongoDB 社区版。
在 Ubuntu 20.04 上安装 MongoDB 社区版
在 Ubuntu 20.04 上安装 MongoDB APT Repo
为了能够在 Ubuntu 20.04 上安装最新稳定版本的 MongoDB,您需要安装 MongoDB APT 存储库列表。
在撰写本文时,MongoDB 4.4 是当前稳定的 LTS 发行版。 请务必相应地替换以下命令中的版本号。
首先安装 MongoDB APT repo 签名密钥;
apt install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
接下来,安装MongoDB APT Repo;
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
更新包缓存;
apt update
在 Ubuntu 20.04 上安装最新版本的 MongoDB 社区版
存储库列表到位后,运行以下命令在 Ubuntu 20.04 上安装最新版本的 MongoDB。
apt install -y mongodb-org
在 Ubuntu 20.04 上运行 MongoDB
安装后,MongoDB 会创建一个名为的 systemd 单元文件, mongod.service
, 在下面 /lib/systemd/system/
.
该服务单元文件可用于管理 MongoDB。
启动MongoDB服务,执行命令;
systemctl start mongod
检查状态;
systemctl status mongod
● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled) Active: active (running) since Tue 2021-03-09 16:56:41 UTC; 2min 8s ago Docs: https://docs.mongodb.org/manual Main PID: 6618 (mongod) Memory: 60.4M CGroup: /system.slice/mongod.service └─6618 /usr/bin/mongod --config /etc/mongod.conf Mar 09 16:56:41 ubuntu20 systemd[1]: Started MongoDB Database Server.
使其在启动时启动;
systemctl enable mongod
您可以通过分别执行以下命令来重新启动或停止;
systemctl restart mongod
systemctl stop mongod
在 Ubuntu 上连接到 MongoDB
默认情况下,MongoDB 默认在本地主机上侦听 TCP 端口 27017。
apt install net-tools
netstat -altnp | grep :27
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 6618/mongod
为了连接到它,你需要登录到运行 MongoDB 的同一台主机上,然后简单地执行下面的命令;
mongo
上面的命令将启动一个交互式 MongoDB shell;
MongoDB shell version v4.4.4 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("9d73e6f2-1a6e-4ea7-8887-a71f81dc1cd9") } MongoDB server version: 4.4.4 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see https://docs.mongodb.com/ Questions? Try the MongoDB Developer Community Forums https://community.mongodb.com --- ... --- >
要在登录 MongoDB 交互式 shell 时获取帮助,请运行 help
命令。
help
> help db.help() help on db methods db.mycoll.help() help on collection methods sh.help() sharding helpers rs.help() replica set helpers help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use <db_name> set current database db.mycoll.find() list objects in collection mycoll db.mycoll.find( { a : 1 } ) list objects in mycoll where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell >
在 Ubuntu 20.04 上卸载 MongoDB CE
如果由于某些原因您想在 Ubuntu 20.04 上卸载 MongoDB 并完全删除它,您只需通过运行以下命令来停止并清除它;
systemctl stop mongod
apt remove --purge --auto-remove mongodb-org
删除MongoDB数据库;
rm -rf /var/lib/mongodb
这就是在 Ubuntu 20.04 上安装 MongoDB 是多么简单。
通过以下链接了解如何启用 MongoDB 身份验证;
在 MongoDB 上启用身份验证
参考
在 Ubuntu 上安装 MongoDB 社区版
进一步阅读
MongoDB 入门
安装 LibModsecurity Apache 在 Ubuntu 20.04 上
更新/更改 Kibana 可视化索引模式
在 Linux 版 Teams 中的屏幕共享期间请求控制
在 Ubuntu 上安装 Arkime (Moloch) Full Packet Capture 工具